flashBag = resolve('flash'); $this->translator = resolve('translator'); NavigationManager::instance()->setContext('Vdlp.RssFetcher', 'rssfetcher', 'sources'); } /** * Fetches RSS items from source * * @throws ApplicationException * @return array */ public function onFetch(): array { try { $source = Source::query()->findOrFail($this->params[0]); if ($source instanceof Source && !$source->getAttribute('is_enabled')) { throw new SourceNotEnabledException( $this->translator->trans('vdlp.rssfetcher::lang.source.source_not_enabled') ); } RssFetcher::instance()->fetch((int) $this->params[0]); $this->flashBag->success($this->translator->trans('vdlp.rssfetcher::lang.source.items_fetch_success')); } catch (SourceNotEnabledException $e) { $this->flashBag->warning($e->getMessage()); } catch (Exception $e) { throw new ApplicationException( $this->translator->trans('vdlp.rssfetcher::lang.source.items_fetch_fail', [ 'error' => $e->getMessage() ]) ); } return $this->listRefresh(); } // @codingStandardsIgnoreStart /** * @return array */ public function index_onBulkFetch(): array { foreach ($this->getCheckedIds() as $sourceId) { if (!$source = Source::query()->find($sourceId)) { continue; } if (!$source->getAttribute('is_enabled')) { continue; } try { RssFetcher::instance()->fetch((int) $source->getKey()); } catch (Exception $e) { $this->flashBag->error($e->getMessage()); } } return $this->listRefresh(); } /** * @return array * @throws Exception */ public function index_onDelete(): array { foreach ($this->getCheckedIds() as $sourceId) { if (!$source = Source::query()->find($sourceId)) { continue; } $source->delete(); } return $this->listRefresh(); } // @codingStandardsIgnoreEnd /** * Check checked ID's from POST request. * * @return array */ private function getCheckedIds(): array { if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds) ) { return $checkedIds; } return []; } }