11<?php
22/**
33* COPYRIGHT NOTICE
4- * Copyright (c) 2022 Neue Rituale GbR
4+ * Copyright (c) 2023 Neue Rituale GbR
55* @author NR <code@neuerituale.com>
66*/
77
88namespace ProcessWire;
99
10- use Exception;
1110use Lukaswhite\PodcastFeedParser\Exceptions\FileNotFoundException;
1211use Lukaswhite\PodcastFeedParser\Exceptions\InvalidXmlException;
1312use Lukaswhite\PodcastFeedParser\Parser;
1413use Lukaswhite\PodcastFeedParser\Podcast;
15- use PDO;
16- use PDOException;
17-
18- require_once(/*NoCompile*/__DIR__ . '/vendor/autoload.php');
1914
2015/**
16+ * @method void processPodcast(WireData $feed, Podcast $podcast)
17+ * @method array filterDbFeedInput(string $feedUrl, Podcast $podcast)
18+ * @method array filterDbFeedUpdate(int $id, Podcast $podcast)
2119 * @method Parser getFeedParserInstance($args = null)
2220 */
23-
24- class ProcessPodcastSubscriptions extends Process implements Module, ConfigurableModule
25- {
21+ class ProcessPodcastSubscriptions extends Process implements Module, ConfigurableModule {
2622
2723 const dbTableName = 'podcast_subscriptions';
2824 const logFileName = 'podcast-subscriptions';
2925 const SCHEMA_VERSION = 2;
3026 private ?WireArray $feedsCache = null;
3127
32- public static function getModuleInfo() {
33- return array(
28+ public static function getModuleInfo(): array {
29+ return [
3430 'title' => 'Process Podcast Subscriptions',
35- 'version' => 102 ,
31+ 'version' => 103 ,
3632 'summary' => 'Subscribe Podcast RSS feed and save as new page',
3733 'icon' => 'clock-o',
3834 'requires' => ['LazyCron'],
@@ -44,7 +40,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
4440 'parent' => 'admin',
4541 'title' => __('Podcasts'),
4642 ],
47- ) ;
43+ ] ;
4844 }
4945
5046 /**
@@ -72,7 +68,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
7268 $this->subscriptionLinks = $subscriptionLinks;
7369
7470 // find hookname and init lazy cron hook
75- $hookName = @ $this->timeFuncs[$this->cronSchedule];
71+ $hookName = $this->timeFuncs[$this->cronSchedule] ?? false ;
7672 if($hookName && $this->modules->isInstalled('LazyCron')) $this->addHook('LazyCron::' . $hookName, $this, 'updateAllFeeds');
7773 }
7874
@@ -108,7 +104,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
108104 $feedUrl = $this->input->post('feed_url', 'url');
109105 try {
110106 $this->addFeed($feedUrl);
111- } catch(Exception $exception) {
107+ } catch(\ Exception $exception) {
112108 $this->error($exception->getMessage());
113109 }
114110 }
@@ -171,16 +167,16 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
171167 public function addFeed(string $feedUrl = '') : Podcast {
172168
173169 // check url
174- if(empty($feedUrl)) throw new Exception($this->_('Empty feed url'));
170+ if(empty($feedUrl)) throw new \ Exception($this->_('Empty feed url'));
175171
176172 // duplication check
177173 $checkStatement = $this->database->prepare('SELECT * FROM ' . self::dbTableName . ' WHERE feed_url=:feedUrl');
178174 $checkStatement->execute(['feedUrl' => $feedUrl]);
179- if($checkStatement->fetchColumn(0)) throw new Exception($this->_('Feed already exists'));
175+ if($checkStatement->fetchColumn(0)) throw new \ Exception($this->_('Feed already exists'));
180176
181177 // check feed
182178 $podcast = $this->fetchAndParseFeed($feedUrl);
183- if(!$podcast) throw new Exception($this->_('Invalid feed, no type found.'));
179+ if(!$podcast) throw new \ Exception($this->_('Invalid feed, no type found.'));
184180
185181 // add to db
186182 $addStatement = $this->database->prepare('INSERT INTO ' . self::dbTableName . ' (title,description,artwork_url,feed_url,media_count) VALUE (:title,:description,:artwork_url,:feed_url,:media_count)');
@@ -208,7 +204,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
208204
209205 // find feed
210206 $feed = $this->getFeeds('id=' . $id)->first();
211- if(!$feed) throw new Exception($this->_('Invalid feed id'));
207+ if(!$feed) throw new \ Exception($this->_('Invalid feed id'));
212208 $podcast = $this->fetchAndParseFeed($feed->feed_url);
213209
214210 // Update feed in db and the wireData for return
@@ -258,7 +254,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
258254 // Get from database
259255 $statement = $this->database->prepare('SELECT * FROM ' . self::dbTableName);
260256 $statement->execute();
261- $items = $statement->fetchAll(PDO::FETCH_CLASS, Feed::class);
257+ $items = $statement->fetchAll(\ PDO::FETCH_CLASS, Feed::class);
262258
263259 if(!is_array($items)) {
264260 $this->feedsCache = null;
@@ -276,14 +272,14 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
276272 * Delete feed by id
277273 * @param int $id
278274 * @return bool
279- * @throws WireException|Exception
275+ * @throws WireException|\ Exception
280276 */
281277 public function deleteFeed(int $id) : bool {
282278 $feed = $this->getFeeds('id=' . $id)->first();
283- if(!$feed) throw new Exception($this->_('Invalid feed id'));
279+ if(!$feed) throw new \ Exception($this->_('Invalid feed id'));
284280
285281 $statement = $this->database->prepare('DELETE FROM '.self::dbTableName.' WHERE id=:id');
286- $statement->bindValue('id', $feed->id, PDO::PARAM_INT);
282+ $statement->bindValue('id', $feed->id, \ PDO::PARAM_INT);
287283 return $statement->execute();
288284 }
289285
@@ -292,7 +288,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
292288 * @param array $meta
293289 * @return Feed
294290 */
295- public function updateFeedMeta(Feed $feed, array $meta = []) {
291+ public function updateFeedMeta(Feed $feed, array $meta = []): Feed {
296292 $json = count($meta)
297293 ? json_encode($meta, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)
298294 : 'null'
@@ -316,7 +312,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
316312 public function fetchAndParseFeed($feedUrl) : ?Podcast {
317313
318314 $fileContent = $this->files->fileGetContents($feedUrl);
319- if(!$fileContent) throw new Exception($this->_('Invalid feed url'));
315+ if(!$fileContent) throw new \ Exception($this->_('Invalid feed url'));
320316
321317 libxml_use_internal_errors(true);
322318 $parser = $this->getFeedParserInstance();
@@ -343,7 +339,6 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
343339 *
344340 * @param string $feedUrl
345341 * @param Podcast $podcast
346- * @param int $id
347342 * @return array
348343 * @throws WireException
349344 */
@@ -382,7 +377,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
382377 * @return array
383378 * @throws WireException
384379 */
385- public function processMetaInput() {
380+ public function processMetaInput(): array {
386381
387382 $meta = [];
388383
@@ -405,7 +400,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
405400 public function install() {
406401
407402 // Create Database
408- $this-> wire('database') ->exec("
403+ wire()->database ->exec("
409404 CREATE TABLE ".self::dbTableName." (
410405 `id` int(11) NOT NULL,
411406 `title` tinytext NOT NULL,
@@ -435,8 +430,8 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
435430 */
436431 public function uninstall() {
437432 try {
438- $this-> wire('database') ->exec('DROP TABLE ' . self::dbTableName);
439- } catch(Exception $exception) {
433+ wire()->database ->exec('DROP TABLE ' . self::dbTableName);
434+ } catch(\ Exception $exception) {
440435 $this->error($exception->getMessage());
441436 }
442437 }
@@ -511,7 +506,7 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
511506 try {
512507 $updatedRows = $this->database->exec($sql);
513508 return $updatedRows !== false;
514- } catch (PDOException $e) {
509+ } catch (\ PDOException $e) {
515510 if (isset($e->errorInfo[1]) && in_array($e->errorInfo[1], [1060, 1061, 1091])) {
516511 // 1060 (column already exists), 1061 (duplicate key name), and 1091 (can't drop index) are errors that
517512 // can be safely ignored here; the most likely issue would be that this update has already been applied
@@ -560,11 +555,25 @@ class ProcessPodcastSubscriptions extends Process implements Module, Configurabl
560555 * Get Parser Instance
561556 * @param $args
562557 * @return Parser
558+ * @throws \Exception
563559 */
564- public function ___getFeedParserInstance($args = null) {
560+ public function ___getFeedParserInstance($args = null): Parser {
561+ $this->loadFeedParserLib();
565562 return new Parser($args);
566563 }
567564
565+ /**
566+ * Load PodcastFeedParser Library
567+ * @return void
568+ * @throws \Exception
569+ */
570+ public function loadFeedParserLib() {
571+ if(!class_exists("\Lukaswhite\PodcastFeedParser\Parser")) {
572+ if(!file_exists(__DIR__ . '/vendor/autoload.php')) throw new \Exception("Please install the PodcastFeedParser library via `composer install` in the ProcessPodcastSubscriptions module directory.");
573+ require_once(/*NoCompile*/__DIR__ . '/vendor/autoload.php');
574+ }
575+ }
576+
568577}
569578
570579class Feed extends WireData {
0 commit comments