Completed
Push — master ( a775f9...497487 )
by Alex
07:59
created

Reader::addParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo;
12
13
use FeedIo\ParserAbstract;
14
use FeedIo\Adapter\ClientInterface;
15
use FeedIo\Reader\Document;
16
use FeedIo\Reader\ReadErrorException;
17
use FeedIo\Reader\Result;
18
use FeedIo\Reader\NoAccurateParserException;
19
use Psr\Log\LoggerInterface;
20
21
/**
22
 * Consumes feeds and return corresponding Result instances
23
 *
24
 * Depends on :
25
 *  - FeedIo\Adapter\ClientInterface
26
 *  - Psr\Log\LoggerInterface
27
 *
28
 * A Reader instance MUST have at least one parser added with the addParser() method to read feeds
29
 * It will throw a NoAccurateParserException if it cannot find a suitable parser for the feed.
30
 */
31
class Reader
32
{
33
    /**
34
     * @var \FeedIo\Adapter\ClientInterface;
35
     */
36
    protected $client;
37
38
    /**
39
     * @var \Psr\Log\LoggerInterface
40
     */
41
    protected $logger;
42
43
    /**
44
     * @var array
45
     */
46
    protected $parsers = array();
47
48
    /**
49
     * @param ClientInterface $client
50 12
     * @param LoggerInterface $logger
51
     */
52 12
    public function __construct(ClientInterface $client, LoggerInterface $logger)
53 12
    {
54 12
        $this->client = $client;
55
        $this->logger = $logger;
56
    }
57
58
    /**
59
     * @param  Parser $parser
60 10
     * @return $this
61
     */
62 10
    public function addParser(ParserAbstract $parser)
63 10
    {
64
        $this->logger->debug("new parser added : ".get_class($parser->getStandard()));
65 10
        $this->parsers[] = $parser;
66
67
        return $this;
68
    }
69
70
    /**
71
     * adds a filter to every parsers
72
     *
73
     * @param \FeedIo\FilterInterface $filter
74 1
     * @return $this
75
     */
76 1
    public function addFilter(FilterInterface $filter)
77 1
    {
78 1
        foreach ($this->parsers as $parser) {
79
            $parser->addFilter($filter);
80 1
        }
81
82
        return $this;
83
    }
84
85
    /**
86
     * Reset filters on every parsers
87
     * @return $this
88
     */
89
    public function resetFilters()
90
    {
91
        foreach ($this->parsers as $parser) {
92
            $parser->resetFilters();
93
        }
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param $url
100 2
     * @param  FeedInterface         $feed
101
     * @param  \DateTime             $modifiedSince
102 2
     * @return \FeedIo\Reader\Result
103
     * @throws ReadErrorException
104
     */
105
    public function read($url, FeedInterface $feed, \DateTime $modifiedSince = null)
106
    {
107 1
        $this->logger->debug("start reading {$url}");
108 1
        if (is_null($modifiedSince)) {
109
            $this->logger->notice("no 'modifiedSince' parameter given, setting it to 01/01/1970");
110 2
            $modifiedSince = new \DateTime('@0');
111
        }
112 2
113 2
        try {
114 1
            $response = $this->client->getResponse($url, $modifiedSince);
115
116 1
            $this->logger->debug("response ok, now turning it into a DomDocument");
117
            $document = new Document($response->getBody());
118
            $this->parseDocument($document, $feed);
119
120
            $this->logger->info("{$url} successfully parsed");
121
122
            return new Result($document, $feed, $modifiedSince, $response, $url);
123
        } catch (\Exception $e) {
124
            $this->logger->warning("{$url} read error : {$e->getMessage()}");
125
            throw new ReadErrorException($e);
126 3
        }
127
    }
128 3
129 3
    /**
130 2
     * @param Document $document
131 2
     * @param FeedInterface $feed
132 2
     * @return FeedInterface
133
     * @throws Parser\UnsupportedFormatException
134
     * @throws Reader\NoAccurateParserException
135 3
     */
136
    public function parseDocument(Document $document, FeedInterface $feed)
137 2
    {
138 2
        $parser = $this->getAccurateParser($document);
139 2
        $this->logger->debug("accurate parser : ".get_class($parser));
140
141 2
        return $parser->parse($document, $feed);
142
    }
143 2
144 1
    /**
145 1
     * @param  Document                     $document
146 1
     * @return ParserAbstract
147
     * @throws Reader\NoAccurateParserException
148
     */
149
    public function getAccurateParser(Document $document)
150
    {
151
        foreach ($this->parsers as $parser) {
152
            if ($parser->getStandard()->canHandle($document)) {
153
                return $parser;
154
            }
155
        }
156
157 1
        $message = 'No parser can handle this stream';
158
        $this->logger->error($message);
159 1
        throw new NoAccurateParserException($message);
160 1
    }
161
}
162