BlogFeedConsumer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 77
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
B refreshFeed() 0 24 3
A processFeed() 0 20 3
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);
0 ignored issues
show
Bug introduced by
It seems like $feed defined by $this->processFeed($feed) on line 43 can also be of type object<Zend\Feed\Reader\Entry\Atom>; however, Acelaya\Website\Feed\Ser...Consumer::processFeed() does only seem to accept object<Zend\Feed\Reader\Feed\FeedInterface>, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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