-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace helpers; | ||
|
||
use SimplePie; | ||
|
||
/** | ||
* Helper class for obtaining feeds | ||
*/ | ||
class FeedReader { | ||
/** @var SimplePie */ | ||
private $simplepie; | ||
|
||
/** | ||
* @param ?string $cacheLocation | ||
* @param int $cacheTimeout | ||
*/ | ||
public function __construct(SimplePie $simplepie, WebClient $webClient, $cacheLocation = null, $cacheTimeout = 1800) { | ||
$this->simplepie = $simplepie; | ||
|
||
// initialize simplepie feed loader | ||
if ($cacheLocation !== null) { | ||
$this->simplepie->set_cache_location($cacheLocation); | ||
$this->simplepie->set_cache_duration($cacheTimeout); | ||
} | ||
|
||
// abuse set_curl_options since there is no sane way to pass data to SimplePie\File | ||
$this->simplepie->set_curl_options([ | ||
WebClient::class => $webClient | ||
]); | ||
|
||
$this->simplepie->set_file_class(SimplePieFileGuzzle::class); | ||
$this->simplepie->set_autodiscovery_level(SIMPLEPIE_LOCATOR_AUTODISCOVERY | SIMPLEPIE_LOCATOR_LOCAL_EXTENSION | SIMPLEPIE_LOCATOR_LOCAL_BODY); | ||
$this->simplepie->set_useragent($webClient->getUserAgent()); | ||
} | ||
|
||
/** | ||
* Load the feed for provided URL using SimplePie. | ||
* | ||
* @param string $url URL of the feed | ||
* | ||
* @return array | ||
*/ | ||
public function load($url) { | ||
@$this->simplepie->set_feed_url($url); | ||
// fetch items | ||
@$this->simplepie->init(); | ||
|
||
// on error retry with force_feed | ||
if (@$this->simplepie->error()) { | ||
@$this->simplepie->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE); | ||
@$this->simplepie->force_feed(true); | ||
@$this->simplepie->init(); | ||
} | ||
|
||
// check for error | ||
if (@$this->simplepie->error()) { | ||
throw new \Exception($this->simplepie->error()); | ||
} | ||
|
||
return [ | ||
// save fetched items | ||
'items' => $this->simplepie->get_items(), | ||
'htmlUrl' => @$this->simplepie->get_link(), | ||
'spoutTitle' => $this->simplepie->get_title(), | ||
]; | ||
} | ||
|
||
/** | ||
* Get the URL of the feed’s logo. | ||
* | ||
* @return ?string | ||
*/ | ||
public function getImageUrl() { | ||
return $this->simplepie->get_image_url(); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function __destruct() { | ||
$this->simplepie->__destruct(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters