Completed
Push — master ( 796f18...c2ba48 )
by Markus
20s
created

FeedReaderManager::setFeedReader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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