Completed
Push — issue/88 ( df2ba2 )
by Alex
03:09
created

FeedIo::getFixerSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
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\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();
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
        $this->reader->addParser(
165 8
                            new Parser($standard, $this->logger)
166 8
                        );
167
168 8
        return $this;
169
    }
170
171
    /**
172
     * @return \FeedIo\Reader\FixerSet
173
     */
174 1
    public function getFixerSet()
175
    {
176 1
        return $this->fixerSet;
177
    }
178
179
    /**
180
     * @return $this
181
     */
182 7
    protected function loadFixerSet()
183
    {
184 7
        $this->fixerSet = new FixerSet();
185 7
        $fixers = $this->getBaseFixers();
186
187 7
        foreach ($fixers as $fixer) {
188 7
            $this->addFixer($fixer);
189 7
        }
190
191 7
        return $this;
192
    }
193
194
    /**
195
     * @param  FixerAbstract $fixer
196
     * @return $this
197
     */
198 7
    public function addFixer(FixerAbstract $fixer)
199
    {
200 7
        $fixer->setLogger($this->logger);
201 7
        $this->fixerSet->add($fixer);
202
203 7
        return $this;
204
    }
205
206
    /**
207
     * @return array
208
     */
209 7
    public function getBaseFixers()
210
    {
211
        return array(
212 7
            new Reader\Fixer\LastModified(),
213 7
            new Reader\Fixer\PublicId(),
214
215 7
        );
216
    }
217
218
    /**
219
     * @param array $formats
220
     * @return $this
221
     */
222 1
    public function addDateFormats(array $formats)
223
    {
224 1
        foreach( $formats as $format ) {
225 1
            $this->getDateTimeBuilder()->addDateFormat($format);
226 1
        }
227
228 1
        return $this;
229
    }
230
231
    /**
232
     * @return \FeedIo\Rule\DateTimeBuilder
233
     */
234 2
    public function getDateTimeBuilder()
235
    {
236 2
        return $this->dateTimeBuilder;
237
    }
238
239
    /**
240
     * @return \FeedIo\Reader
241
     */
242 2
    public function getReader()
243
    {
244 2
        return $this->reader;
245
    }
246
247
    /**
248
     * @param \FeedIo\Reader
249
     * @return $this
250
     */
251 8
    public function setReader(Reader $reader)
252
    {
253 8
        $this->reader = $reader;
254
255 8
        return $this;
256
    }
257
258
    /**
259
     * @param $url
260
     * @param  FeedInterface         $feed
261
     * @param  \DateTime             $modifiedSince
262
     * @return \FeedIo\Reader\Result
263
     */
264 1
    public function read($url, FeedInterface $feed = null, \DateTime $modifiedSince = null)
265
    {
266 1
        if (is_null($feed)) {
267 1
            $feed = new Feed();
268 1
        }
269
270 1
        if ($modifiedSince instanceof \DateTime) {
271
            $this->addFilter(new ModifiedSince($modifiedSince));
272
        }
273
274 1
        $this->logAction($feed, "read access : $url into a feed instance");
275 1
        $result = $this->reader->read($url, $feed, $modifiedSince);
276
277 1
        $this->fixerSet->correct($result->getFeed());
278
279 1
        return $result;
280
    }
281
282
    /**
283
     * @param $url
284
     * @param  \DateTime             $modifiedSince
285
     * @return \FeedIo\Reader\Result
286
     */
287 1
    public function readSince($url, \DateTime $modifiedSince)
288
    {
289 1
        return $this->read($url, new Feed(), $modifiedSince);
290
    }
291
292
    /**
293
     * @return $this
294
     */
295
    public function resetFilters()
296
    {
297
        $this->getReader()->resetFilters();
298
299
        return $this;
300
    }
301
302
    /**
303
     * @param  FeedInterface $feed
304
     * @param  string        $standard Standard's name
305
     * @return \DomDocument
306
     */
307 1
    public function format(FeedInterface $feed, $standard)
308
    {
309 1
        $this->logAction($feed, "formatting a feed in $standard format");
310
311 1
        $formatter = new Formatter($this->getStandard($standard), $this->logger);
312
313 1
        return $formatter->toDom($feed);
314
    }
315
316
    /**
317
     * @param  \FeedIo\FeedInterface $feed
318
     * @return \DomDocument
319
     */
320 1
    public function toRss(FeedInterface $feed)
321
    {
322 1
        return $this->format($feed, 'rss');
323
    }
324
325
    /**
326
     * @param  \FeedIo\FeedInterface $feed
327
     * @return \DomDocument
328
     */
329 1
    public function toAtom(FeedInterface $feed)
330
    {
331 1
        return $this->format($feed, 'atom');
332
    }
333
334
    /**
335
     * @param  string                   $name
336
     * @return \FeedIo\StandardAbstract
337
     * @throws \OutOfBoundsException
338
     */
339 2
    public function getStandard($name)
340
    {
341 2
        $name = strtolower($name);
342 2
        if (array_key_exists($name, $this->standards)) {
343 1
            return $this->standards[$name];
344
        }
345
346 1
        throw new \OutOfBoundsException("no standard found for $name");
347
    }
348
349
    /**
350
     * @param  \FeedIo\FeedInterface $feed
351
     * @param  string                $message
352
     * @return $this
353
     */
354 1
    protected function logAction(FeedInterface $feed, $message)
355
    {
356 1
        $class = get_class($feed);
357 1
        $this->logger->debug("$message (feed class : $class)");
358
359 1
        return $this;
360
    }
361
}
362