FeedReaderManager::unsetFeedReader()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 11
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jellyfish\Feed;
6
7
use Jellyfish\Feed\Exception\FeedReaderNotFoundException;
8
9
use function array_key_exists;
10
use function sprintf;
11
12
class FeedReaderManager implements FeedReaderManagerInterface
13
{
14
    /**
15
     * @var \Jellyfish\Feed\FeedReaderInterface[]
16
     */
17
    protected $feedReaders;
18
19
    public function __construct()
20
    {
21
        $this->feedReaders = [];
22
    }
23
24
    /**
25
     * @param string $identifier
26
     *
27
     * @return bool
28
     */
29
    public function existsFeedReader(string $identifier): bool
30
    {
31
        return array_key_exists($identifier, $this->feedReaders);
32
    }
33
34
    /**
35
     * @param string $identifier
36
     * @param \Jellyfish\Feed\FeedReaderInterface $feedReader
37
     *
38
     * @return \Jellyfish\Feed\FeedReaderManagerInterface
39
     */
40
    public function setFeedReader(string $identifier, FeedReaderInterface $feedReader): FeedReaderManagerInterface
41
    {
42
        $this->feedReaders[$identifier] = $feedReader;
43
44
        return $this;
45
    }
46
47
    /**
48
     * @param string $identifier
49
     *
50
     * @return \Jellyfish\Feed\FeedReaderManagerInterface
51
     *
52
     * @throws \Jellyfish\Feed\Exception\FeedReaderNotFoundException
53
     */
54
    public function unsetFeedReader(string $identifier): FeedReaderManagerInterface
55
    {
56
        if (!$this->existsFeedReader($identifier)) {
57
            throw new FeedReaderNotFoundException(
58
                sprintf('Feed reader with identifier "%s" not found.', $identifier)
59
            );
60
        }
61
62
        unset($this->feedReaders[$identifier]);
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param string $identifier
69
     *
70
     * @return \Jellyfish\Feed\FeedReaderInterface
71
     *
72
     * @throws \Jellyfish\Feed\Exception\FeedReaderNotFoundException
73
     */
74
    public function getFeederReader(string $identifier): FeedReaderInterface
75
    {
76
        if (!$this->existsFeedReader($identifier)) {
77
            throw new FeedReaderNotFoundException(
78
                sprintf('Feed reader with identifier "%s" not found.', $identifier)
79
            );
80
        }
81
82
        return $this->feedReaders[$identifier];
83
    }
84
85
    /**
86
     * @param string $identifier
87
     *
88
     * @return string
89
     *
90
     * @throws \Jellyfish\Feed\Exception\FeedReaderNotFoundException
91
     */
92
    public function readFromFeedReader(string $identifier): string
93
    {
94
        return $this->getFeederReader($identifier)->read();
95
    }
96
}
97