Completed
Pull Request — release/3.0 (#204)
by
unknown
04:44
created

FeedIo::resetFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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