|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Acelaya\Website\Feed\Service; |
|
5
|
|
|
|
|
6
|
|
|
use Acelaya\Website\Feed\BlogOptions; |
|
7
|
|
|
use Doctrine\Common\Cache; |
|
8
|
|
|
use Zend\Feed\Reader\Feed\FeedInterface; |
|
9
|
|
|
use Zend\Feed\Reader\Http\ClientInterface; |
|
10
|
|
|
use Zend\Feed\Reader\Reader; |
|
11
|
|
|
|
|
12
|
|
|
class BlogFeedConsumer implements BlogFeedConsumerInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var BlogOptions |
|
16
|
|
|
*/ |
|
17
|
|
|
private $blogOptions; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Cache\Cache |
|
20
|
|
|
*/ |
|
21
|
|
|
private $feedCache; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Cache\ClearableCache |
|
24
|
|
|
*/ |
|
25
|
|
|
private $viewsCache; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
ClientInterface $httpClient, |
|
29
|
|
|
Cache\Cache $feedCache, |
|
30
|
|
|
Cache\ClearableCache $viewsCache, |
|
31
|
|
|
BlogOptions $blogOptions |
|
32
|
|
|
) { |
|
33
|
|
|
Reader::setHttpClient($httpClient); |
|
34
|
|
|
$this->blogOptions = $blogOptions; |
|
35
|
|
|
$this->feedCache = $feedCache; |
|
36
|
|
|
$this->viewsCache = $viewsCache; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function refreshFeed(): array |
|
40
|
|
|
{ |
|
41
|
|
|
$cacheId = $this->blogOptions->getCacheKey(); |
|
42
|
|
|
$feed = Reader::import($this->blogOptions->getFeed()); |
|
43
|
|
|
$feed = $this->processFeed($feed); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
// If no feed has been cached yet, cache current one and return |
|
46
|
|
|
if (! $this->feedCache->contains($cacheId)) { |
|
47
|
|
|
$this->viewsCache->deleteAll(); |
|
48
|
|
|
$this->feedCache->save($cacheId, $feed); |
|
49
|
|
|
return $feed; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Check if the last feed has changed, otherwise, return |
|
53
|
|
|
$cachedFeed = $this->feedCache->fetch($cacheId); |
|
54
|
|
|
if ($cachedFeed[0]['link'] === $feed[0]['link']) { |
|
55
|
|
|
return $feed; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// If the feed has changed, clear all cached elements so that views are refreshed, and cache feed too |
|
59
|
|
|
$this->viewsCache->deleteAll(); |
|
60
|
|
|
$this->feedCache->save($cacheId, $feed); |
|
61
|
|
|
return $feed; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param FeedInterface $feed |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function processFeed(FeedInterface $feed): array |
|
69
|
|
|
{ |
|
70
|
|
|
$data = []; |
|
71
|
|
|
$count = 0; |
|
72
|
|
|
|
|
73
|
|
|
/** @var FeedInterface $entry */ |
|
74
|
|
|
foreach ($feed as $entry) { |
|
75
|
|
|
if ($count >= $this->blogOptions->getElementsToDisplay()) { |
|
76
|
|
|
break; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$data[] = [ |
|
80
|
|
|
'title' => $entry->getTitle(), |
|
81
|
|
|
'link' => $entry->getLink(), |
|
82
|
|
|
]; |
|
83
|
|
|
$count += 1; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $data; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.