1 | <?php |
||
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) |
||
143 | 2 | ||
144 | 1 | /** |
|
145 | 1 | * @param Document $document |
|
146 | 1 | * @return ParserAbstract |
|
147 | * @throws Reader\NoAccurateParserException |
||
148 | */ |
||
149 | public function getAccurateParser(Document $document) |
||
161 | } |
||
162 |