Completed
Push — issue/98 ( 297abd )
by Alex
03:51 queued 03:51
created

Reader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
     * @param LoggerInterface $logger
51
     */
52 12
    public function __construct(ClientInterface $client, LoggerInterface $logger)
53
    {
54 12
        $this->client = $client;
55 12
        $this->logger = $logger;
56 12
    }
57
58
    /**
59
     * @param  Parser $parser
60
     * @return $this
61
     */
62 12
    public function addParser(ParserAbstract $parser)
63
    {
64 12
        $this->logger->debug("new parser added : ".get_class($parser->getStandard()));
65 12
        $this->parsers[] = $parser;
66
67 12
        return $this;
68
    }
69
70
    /**
71
     * adds a filter to every parsers
72
     *
73
     * @param \FeedIo\FilterInterface $filter
74
     * @return $this
75
     */
76 2
    public function addFilter(FilterInterface $filter)
77
    {
78 2
        foreach ($this->parsers as $parser) {
79 2
            $parser->addFilter($filter);
80 2
        }
81
82 2
        return $this;
83
    }
84
85
    /**
86
     * Reset filters on every parsers
87
     * @return $this
88
     */
89 1
    public function resetFilters()
90
    {
91 1
        foreach ($this->parsers as $parser) {
92 1
            $parser->resetFilters();
93 1
        }
94
95 1
        return $this;
96
    }
97
98
    /**
99
     * @param $url
100
     * @param  FeedInterface         $feed
101
     * @param  \DateTime             $modifiedSince
102
     * @return \FeedIo\Reader\Result
103
     * @throws ReadErrorException
104
     */
105 4
    public function read($url, FeedInterface $feed, \DateTime $modifiedSince = null)
106
    {
107 4
        $this->logger->debug("start reading {$url}");
108 4
        if (is_null($modifiedSince)) {
109 2
            $this->logger->notice("no 'modifiedSince' parameter given, setting it to 01/01/1970");
110 2
            $modifiedSince = new \DateTime('@0');
111 2
        }
112
113
        try {
114 4
            $response = $this->client->getResponse($url, $modifiedSince);
115
116 3
            $this->logger->debug("response ok, now turning it into a DomDocument");
117 3
            $document = new Document($response->getBody());
118 3
            $this->parseDocument($document, $feed);
119
120 3
            $this->logger->info("{$url} successfully parsed");
121
122 3
            return new Result($document, $feed, $modifiedSince, $response, $url);
123 1
        } catch (\Exception $e) {
124 1
            $this->logger->warning("{$url} read error : {$e->getMessage()}");
125 1
            throw new ReadErrorException($e);
126 1
        }
127
    }
128
129
    /**
130
     * @param Document $document
131
     * @param FeedInterface $feed
132
     * @return FeedInterface
133
     * @throws Parser\UnsupportedFormatException
134
     * @throws Reader\NoAccurateParserException
135
     */
136 2
    public function parseDocument(Document $document, FeedInterface $feed)
137
    {
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
144
    /**
145
     * @param  Document                     $document
146
     * @return ParserAbstract
147
     * @throws Reader\NoAccurateParserException
148
     */
149 4
    public function getAccurateParser(Document $document)
150
    {
151 4
        foreach ($this->parsers as $parser) {
152 3
            if ($parser->getStandard()->canHandle($document)) {
153 3
                return $parser;
154
            }
155 2
        }
156
157 1
        $message = 'No parser can handle this stream';
158 1
        $this->logger->error($message);
159 1
        throw new NoAccurateParserException($message);
160
    }
161
}
162