Completed
Push — issue/96 ( 542eb1...939878 )
by Alex
02:21
created

FeedIo::read()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 11
cp 0.8182
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 3
crap 3.054
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\Filter\ModifiedSince;
14
use FeedIo\Reader;
15
use FeedIo\Reader\FixerSet;
16
use FeedIo\Reader\FixerAbstract;
17
use FeedIo\Rule\DateTimeBuilder;
18
use FeedIo\Adapter\ClientInterface;
19
use FeedIo\Standard\Atom;
20
use FeedIo\Standard\Rss;
21
use FeedIo\Standard\Rdf;
22
use Psr\Log\LoggerInterface;
23
24
/**
25
 * This class acts as a facade. It provides methods to access feed-io main features
26
 *
27
 * <code>
28
 *   // $client is a \FeedIo\Adapter\ClientInterface instance, $logger a \Psr\Log\LoggerInterface
29
 *   $feedIo = new FeedIo($client, $logger);
30
 *
31
 *   // read a feed. Output is a Result instance
32
 *   $result = $feedIo->read('http://somefeed.org/feed.rss');
33
 *
34
 *   // use the feed
35
 *   $feed = $result->getFeed();
36
 *   echo $feed->getTitle();
37
 *
38
 *   // and its items
39
 *   foreach ( $feed as $item ) {
40
 *       echo $item->getTitle();
41
 *       echo $item->getDescription();
42
 *   }
43
 *
44
 * </code>
45
 *
46
 * <code>
47
 *   // build the feed to publish
48
 *   $feed = new \FeedIo\Feed;
49
 *   $feed->setTitle('title');
50
 *   // ...
51
 *
52
 *   // add items to it
53
 *   $item = new \FeedIo\Feed\Item
54
 *   $item->setTitle('my great post');
55
 *
56
 *   // want to publish a media ? no problem
57
 *   $media = new \FeedIo\Feed\Item\Media
58
 *   $media->setUrl('http://yourdomain.tld/medias/some-podcast.mp3');
59
 *   $media->setType('audio/mpeg');
60
 *
61
 *   // add it to the item
62
 *   $item->addMedia($media);
63
 *
64
 *   // add the item to the feed (almost there)
65
 *   $feed->add($item);
66
 *
67
 *   // format it in atom
68
 *   $feedIo->toAtom($feed);
69
 * </code>
70
 *
71
 */
72
class FeedIo
73
{
74
75
    /**
76
     * @var \FeedIo\Reader
77
     */
78
    protected $reader;
79
80
    /**
81
     * @var \FeedIo\Rule\DateTimeBuilder
82
     */
83
    protected $dateTimeBuilder;
84
85
    /**
86
     * @var \Psr\Log\LoggerInterface
87
     */
88
    protected $logger;
89
90
    /**
91
     * @var array
92
     */
93
    protected $standards;
94
95
    /**
96
     * @var \FeedIo\Reader\FixerSet
97
     */
98
    protected $fixerSet;
99
100
    /**
101
     * @param \FeedIo\Adapter\ClientInterface $client
102
     * @param \Psr\Log\LoggerInterface        $logger
103
     */
104 8
    public function __construct(ClientInterface $client, LoggerInterface $logger)
105
    {
106 8
        $this->logger = $logger;
107 8
        $this->dateTimeBuilder = new DateTimeBuilder($logger);
108 8
        $this->setReader(new Reader($client, $logger));
109 8
        $this->loadCommonStandards();
110 8
        $this->loadFixerSet();
111 8
    }
112
113
    /**
114
     * Loads main standards (RSS, RDF, Atom) in current object's attributes
115
     *
116
     * @return $this
117
     */
118 8
    protected function loadCommonStandards()
119
    {
120 8
        $standards = $this->getCommonStandards();
121 8
        foreach ($standards as $name => $standard) {
122 8
            $this->addStandard($name, $standard);
123 8
        }
124
125 8
        return $this;
126
    }
127
128
    /**
129
     * adds a filter to the reader
130
     *
131
     * @param \FeedIo\FilterInterface $filter
132
     * @return $this
133
     */
134 1
    public function addFilter(FilterInterface $filter)
135
    {
136 1
        $this->getReader()->addFilter($filter);
137
138 1
        return $this;
139
    }
140
141
    /**
142
     * Returns main standards
143
     *
144
     * @return array
145
     */
146 8
    public function getCommonStandards()
147
    {
148
        return array(
149 8
            'atom' => new Atom($this->dateTimeBuilder),
150 8
            'rss' => new Rss($this->dateTimeBuilder),
151 8
            'rdf' => new Rdf($this->dateTimeBuilder),
152 8
        );
153
    }
154
155
    /**
156
     * @param  string                   $name
157
     * @param  \FeedIo\StandardAbstract $standard
158
     * @return $this
159
     */
160 8
    public function addStandard($name, StandardAbstract $standard)
161
    {
162 8
        $name = strtolower($name);
163 8
        $this->standards[$name] = $standard;
164 8
        $parser = $this->newParser($standard->getSyntaxFormat(), $standard);
165 8
        $this->reader->addParser($parser);
166
167 8
        return $this;
168
    }
169
170
    /**
171
     * @param string $format
172
     * @param StandardAbstract $standard
173
     * @return object
174
     */
175 7
    public function newParser($format, StandardAbstract $standard)
176
    {
177 7
        $reflection = new \ReflectionClass("FeedIo\\Parser\\{$format}Parser");
178
179 7
        if ( ! $reflection->isSubclassOf('FeedIo\ParserAbstract') ) {
180
            throw new \InvalidArgumentException();
181
        }
182
183 7
        return $reflection->newInstanceArgs([$standard, $this->logger]);
184
    }
185
186
    /**
187
     * @return \FeedIo\Reader\FixerSet
188
     */
189 1
    public function getFixerSet()
190
    {
191 1
        return $this->fixerSet;
192
    }
193
194
    /**
195
     * @return $this
196
     */
197 7
    protected function loadFixerSet()
198
    {
199 7
        $this->fixerSet = new FixerSet();
200 7
        $fixers = $this->getBaseFixers();
201
202 7
        foreach ($fixers as $fixer) {
203 7
            $this->addFixer($fixer);
204 7
        }
205
206 7
        return $this;
207
    }
208
209
    /**
210
     * @param  FixerAbstract $fixer
211
     * @return $this
212
     */
213 7
    public function addFixer(FixerAbstract $fixer)
214
    {
215 7
        $fixer->setLogger($this->logger);
216 7
        $this->fixerSet->add($fixer);
217
218 7
        return $this;
219
    }
220
221
    /**
222
     * @return array
223
     */
224 7
    public function getBaseFixers()
225
    {
226
        return array(
227 7
            new Reader\Fixer\LastModified(),
228 7
            new Reader\Fixer\PublicId(),
229
230 7
        );
231
    }
232
233
    /**
234
     * @param array $formats
235
     * @return $this
236
     */
237 1
    public function addDateFormats(array $formats)
238
    {
239 1
        foreach( $formats as $format ) {
240 1
            $this->getDateTimeBuilder()->addDateFormat($format);
241 1
        }
242
243 1
        return $this;
244
    }
245
246
    /**
247
     * @return \FeedIo\Rule\DateTimeBuilder
248
     */
249 2
    public function getDateTimeBuilder()
250
    {
251 2
        return $this->dateTimeBuilder;
252
    }
253
254
    /**
255
     * @return \FeedIo\Reader
256
     */
257 2
    public function getReader()
258
    {
259 2
        return $this->reader;
260
    }
261
262
    /**
263
     * @param \FeedIo\Reader
264
     * @return $this
265
     */
266 8
    public function setReader(Reader $reader)
267
    {
268 8
        $this->reader = $reader;
269
270 8
        return $this;
271
    }
272
273
    /**
274
     * @param $url
275
     * @param  FeedInterface         $feed
276
     * @param  \DateTime             $modifiedSince
277
     * @return \FeedIo\Reader\Result
278
     */
279 1
    public function read($url, FeedInterface $feed = null, \DateTime $modifiedSince = null)
280
    {
281 1
        if (is_null($feed)) {
282 1
            $feed = new Feed();
283 1
        }
284
285 1
        if ($modifiedSince instanceof \DateTime) {
286
            $this->addFilter(new ModifiedSince($modifiedSince));
287
        }
288
289 1
        $this->logAction($feed, "read access : $url into a feed instance");
290 1
        $result = $this->reader->read($url, $feed, $modifiedSince);
291
292 1
        $this->fixerSet->correct($result->getFeed());
293
294 1
        return $result;
295
    }
296
297
    /**
298
     * @param $url
299
     * @param  \DateTime             $modifiedSince
300
     * @return \FeedIo\Reader\Result
301
     */
302 1
    public function readSince($url, \DateTime $modifiedSince)
303
    {
304 1
        return $this->read($url, new Feed(), $modifiedSince);
305
    }
306
307
    /**
308
     * @return $this
309
     */
310
    public function resetFilters()
311
    {
312
        $this->getReader()->resetFilters();
313
314
        return $this;
315
    }
316
317
    /**
318
     * @param  FeedInterface $feed
319
     * @param  string        $standard Standard's name
320
     * @return \DomDocument
321
     */
322 1
    public function format(FeedInterface $feed, $standard)
323
    {
324 1
        $this->logAction($feed, "formatting a feed in $standard format");
325
326 1
        $formatter = new Formatter($this->getStandard($standard), $this->logger);
327
328 1
        return $formatter->toDom($feed);
329
    }
330
331
    /**
332
     * @param  \FeedIo\FeedInterface $feed
333
     * @return \DomDocument
334
     */
335 1
    public function toRss(FeedInterface $feed)
336
    {
337 1
        return $this->format($feed, 'rss');
338
    }
339
340
    /**
341
     * @param  \FeedIo\FeedInterface $feed
342
     * @return \DomDocument
343
     */
344 1
    public function toAtom(FeedInterface $feed)
345
    {
346 1
        return $this->format($feed, 'atom');
347
    }
348
349
    /**
350
     * @param  string                   $name
351
     * @return \FeedIo\StandardAbstract
352
     * @throws \OutOfBoundsException
353
     */
354 2
    public function getStandard($name)
355
    {
356 2
        $name = strtolower($name);
357 2
        if (array_key_exists($name, $this->standards)) {
358 1
            return $this->standards[$name];
359
        }
360
361 1
        throw new \OutOfBoundsException("no standard found for $name");
362
    }
363
364
    /**
365
     * @param  \FeedIo\FeedInterface $feed
366
     * @param  string                $message
367
     * @return $this
368
     */
369 1
    protected function logAction(FeedInterface $feed, $message)
370
    {
371 1
        $class = get_class($feed);
372 1
        $this->logger->debug("$message (feed class : $class)");
373
374 1
        return $this;
375
    }
376
}
377