Completed
Push — issue/96 ( 5d401e...7b0d36 )
by Alex
02:40
created

FeedIo::resetFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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