Completed
Pull Request — master (#158)
by Alex
06:05 queued 02:56
created

FeedIo::logAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php declare(strict_types=1);
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\Result;
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 FeedIo\Async\Reader as AsyncReader;
21
use FeedIo\Async\CallbackInterface;
22
use FeedIo\FeedInterface;
23
use Psr\Log\LoggerInterface;
24
use FeedIo\Http\ResponseBuilder;
25
use Psr\Http\Message\ResponseInterface;
26
27
/**
28
 * This class acts as a facade. It provides methods to access feed-io main features
29
 *
30
 * <code>
31
 *   // $client is a \FeedIo\Adapter\ClientInterface instance, $logger a \Psr\Log\LoggerInterface
32
 *   $feedIo = new FeedIo($client, $logger);
33
 *
34
 *   // read a feed. Output is a Result instance
35
 *   $result = $feedIo->read('http://somefeed.org/feed.rss');
36
 *
37
 *   // use the feed
38
 *   $feed = $result->getFeed();
39
 *   echo $feed->getTitle();
40
 *
41
 *   // and its items
42
 *   foreach ( $feed as $item ) {
43
 *       echo $item->getTitle();
44
 *       echo $item->getDescription();
45
 *   }
46
 *
47
 * </code>
48
 *
49
 * <code>
50
 *   // build the feed to publish
51
 *   $feed = new \FeedIo\Feed;
52
 *   $feed->setTitle('title');
53
 *   // ...
54
 *
55
 *   // add items to it
56
 *   $item = new \FeedIo\Feed\Item
57
 *   $item->setTitle('my great post');
58
 *
59
 *   // want to publish a media ? no problem
60
 *   $media = new \FeedIo\Feed\Item\Media
61
 *   $media->setUrl('http://yourdomain.tld/medias/some-podcast.mp3');
62
 *   $media->setType('audio/mpeg');
63
 *
64
 *   // add it to the item
65
 *   $item->addMedia($media);
66
 *
67
 *   // add the item to the feed (almost there)
68
 *   $feed->add($item);
69
 *
70
 *   // format it in atom
71
 *   $feedIo->toAtom($feed);
72
 * </code>
73
 *
74
 */
75
class FeedIo
76
{
77
78
    /**
79
     * @var \FeedIo\Reader
80
     */
81
    protected $reader;
82
83
    /**
84
     * @var \FeedIo\Rule\DateTimeBuilder
85
     */
86
    protected $dateTimeBuilder;
87
88
    /**
89
     * @var \Psr\Log\LoggerInterface
90
     */
91
    protected $logger;
92
93
    /**
94
     * @var array
95
     */
96
    protected $standards;
97
98
    /**
99
     * @var \FeedIo\Reader\FixerSet
100
     */
101
    protected $fixerSet;
102 11
103
    /**
104 11
     * @param \FeedIo\Adapter\ClientInterface $client
105 11
     * @param \Psr\Log\LoggerInterface        $logger
106 11
     */
107 11
    public function __construct(ClientInterface $client, LoggerInterface $logger)
108 11
    {
109 11
        $this->logger = $logger;
110
        $this->dateTimeBuilder = new DateTimeBuilder($logger);
111
        $this->setReader(new Reader($client, $logger));
112
        $this->loadCommonStandards();
113
        $this->loadFixerSet();
114
    }
115
116 11
    /**
117
     * Loads main standards (RSS, RDF, Atom) in current object's attributes
118 11
     *
119 11
     * @return FeedIo
120 11
     */
121 11
    protected function loadCommonStandards() : FeedIo
122
    {
123 11
        $standards = $this->getCommonStandards();
124
        foreach ($standards as $name => $standard) {
125
            $this->addStandard($name, $standard);
126
        }
127
128
        return $this;
129
    }
130
131
    /**
132 2
     * adds a filter to the reader
133
     *
134 2
     * @param \FeedIo\FilterInterface $filter
135
     * @return FeedIo
136 2
     */
137
    public function addFilter(FilterInterface $filter) : FeedIo
138
    {
139
        $this->getReader()->addFilter($filter);
140
141
        return $this;
142
    }
143
144 11
    /**
145
     * Returns main standards
146 11
     *
147
     * @return array
148 11
     */
149
    public function getCommonStandards() : array
150
    {
151
        $loader = new Loader();
152
153
        return $loader->getCommonStandards($this->getDateTimeBuilder());
154
    }
155
156 11
    /**
157
     * @param  string                   $name
158 11
     * @param  \FeedIo\StandardAbstract $standard
159 11
     * @return FeedIo
160 11
     */
161 11
    public function addStandard(string $name, StandardAbstract $standard) : FeedIo
162
    {
163 11
        $name = strtolower($name);
164
        $this->standards[$name] = $standard;
165
        $parser = $this->newParser($standard->getSyntaxFormat(), $standard);
166
        $this->reader->addParser($parser);
167
168
        return $this;
169
    }
170
171 10
    /**
172
     * @param string $format
173 10
     * @param StandardAbstract $standard
174
     * @return ParserAbstract
175 10
     */
176
    public function newParser(string $format, StandardAbstract $standard) : ParserAbstract
177
    {
178
        $reflection = new \ReflectionClass("FeedIo\\Parser\\{$format}Parser");
179 10
180
        if (! $reflection->isSubclassOf('FeedIo\ParserAbstract')) {
181
            throw new \InvalidArgumentException();
182
        }
183
184
        return $reflection->newInstanceArgs([$standard, $this->logger]);
185 1
    }
186
187 1
    /**
188
     * @return \FeedIo\Reader\FixerSet
189
     */
190
    public function getFixerSet() : FixerSet
191
    {
192
        return $this->fixerSet;
193 10
    }
194
195 10
    /**
196 10
     * @return FeedIo
197
     */
198 10
    protected function loadFixerSet() : FeedIo
199 10
    {
200 10
        $this->fixerSet = new FixerSet();
201
        $fixers = $this->getBaseFixers();
202 10
203
        foreach ($fixers as $fixer) {
204
            $this->addFixer($fixer);
205
        }
206
207
        return $this;
208
    }
209 10
210
    /**
211 10
     * @param  FixerAbstract $fixer
212 10
     * @return FeedIo
213
     */
214 10
    public function addFixer(FixerAbstract $fixer) : FeedIo
215
    {
216
        $fixer->setLogger($this->logger);
217
        $this->fixerSet->add($fixer);
218
219
        return $this;
220 10
    }
221
222
    /**
223 10
     * @return array
224 10
     */
225
    public function getBaseFixers() : array
226 10
    {
227
        return array(
228
            new Reader\Fixer\LastModified(),
229
            new Reader\Fixer\PublicId(),
230
        );
231
    }
232
233 1
    /**
234
     * @param array $formats
235 1
     * @return FeedIo
236 1
     */
237 1
    public function addDateFormats(array $formats) : FeedIo
238
    {
239 1
        foreach ($formats as $format) {
240
            $this->getDateTimeBuilder()->addDateFormat($format);
241
        }
242
243
        return $this;
244
    }
245 11
246
    /**
247 11
     * @return \FeedIo\Rule\DateTimeBuilder
248
     */
249
    public function getDateTimeBuilder() : DateTimeBuilder
250
    {
251
        return $this->dateTimeBuilder;
252
    }
253 4
254
    /**
255 4
     * @return \FeedIo\Reader
256
     */
257
    public function getReader() : Reader
258
    {
259
        return $this->reader;
260
    }
261
262 11
    /**
263
     * @param \FeedIo\Reader $reader
264 11
     * @return FeedIo
265
     */
266 11
    public function setReader(Reader $reader) : FeedIo
267
    {
268
        $this->reader = $reader;
269
270
        return $this;
271
    }
272
273
    /**
274
     * @param iterable $requests
275 2
     * @param CallbackInterface $callback
276
     * @param string $feedClass
277 2
     */
278 1
    public function readAsync(iterable $requests, CallbackInterface $callback, string $feedClass = '\FeedIo\Feed') : void
279 1
    {
280
        $reader = new AsyncReader($this->reader, $this->reader->getClient(), $callback, $feedClass);
0 ignored issues
show
Compatibility introduced by
$this->reader->getClient() of type object<FeedIo\Adapter\ClientInterface> is not a sub-type of object<FeedIo\Adapter\Guzzle\Client>. It seems like you assume a concrete implementation of the interface FeedIo\Adapter\ClientInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
281 2
282 1
        $reader->process($requests);
283 1
    }
284
285 2
    /**
286 2
     * @param  string                $url
287
     * @param  FeedInterface         $feed
288 2
     * @param  \DateTime             $modifiedSince
289
     * @return \FeedIo\Reader\Result
290 2
     */
291
    public function read(string $url, FeedInterface $feed = null, \DateTime $modifiedSince = null) : Result
292
    {
293
        if (is_null($feed)) {
294
            $feed = new Feed();
295
        }
296
297
        if ($modifiedSince instanceof \DateTime) {
298 1
            $this->addFilter(new ModifiedSince($modifiedSince));
299
        }
300 1
301
        $this->logAction($feed, "read access : $url into a feed instance");
302
        $result = $this->reader->read($url, $feed, $modifiedSince);
303
304
        $this->fixerSet->correct($result->getFeed());
305
306 1
        return $result;
307
    }
308 1
309
    /**
310 1
     * @param  string                $url
311
     * @param  \DateTime             $modifiedSince
312
     * @return \FeedIo\Reader\Result
313
     */
314
    public function readSince(string $url, \DateTime $modifiedSince) : Result
315
    {
316
        return $this->read($url, new Feed(), $modifiedSince);
317
    }
318 1
319
    /**
320 1
     * @return FeedIo
321
     */
322 1
    public function resetFilters() : FeedIo
323
    {
324 1
        $this->getReader()->resetFilters();
325
326
        return $this;
327
    }
328
329
    /**
330
     * Get a PSR-7 compliant response for the given feed
331 1
     *
332
     * @param \FeedIo\FeedInterface $feed
333 1
     * @param string $standard
334
     * @param int $maxAge
335
     * @param bool $public
336
     * @return ResponseInterface
337
     */
338
    public function getPsrResponse(FeedInterface $feed, string $standard, int $maxAge = 600, bool $public = true) : ResponseInterface
339
    {
340 1
        $this->logAction($feed, "creating a PSR 7 Response in $standard format");
341
342 1
        $formatter = $this->getStandard($standard)->getFormatter();
343
        $responseBuilder = new ResponseBuilder($maxAge, $public);
344
345
        return $responseBuilder->createResponse($standard, $formatter, $feed);
346
    }
347
348
    /**
349 1
     * @param  FeedInterface $feed
350
     * @param  string        $standard Standard's name
351 1
     * @return string
352
     */
353
    public function format(FeedInterface $feed, string $standard) : string
354
    {
355
        $this->logAction($feed, "formatting a feed in $standard format");
356
357
        $formatter = $this->getStandard($standard)->getFormatter();
358
359
        return $formatter->toString($feed);
360 2
    }
361
362 2
    /**
363 2
     * @param  \FeedIo\FeedInterface $feed
364 1
     * @return string
365
     */
366
    public function toRss(FeedInterface $feed) : string
367 1
    {
368
        return $this->format($feed, 'rss');
369
    }
370
371
    /**
372
     * @param  \FeedIo\FeedInterface $feed
373
     * @return string
374
     */
375 2
    public function toAtom(FeedInterface $feed) : string
376
    {
377 2
        return $this->format($feed, 'atom');
378 2
    }
379
380 2
    /**
381
     * @param  \FeedIo\FeedInterface $feed
382
     * @return string
383
     */
384
    public function toJson(FeedInterface $feed) : string
385
    {
386
        return $this->format($feed, 'json');
387
    }
388
389
390
    /**
391
     * @param  string                   $name
392
     * @return \FeedIo\StandardAbstract
393
     * @throws \OutOfBoundsException
394
     */
395
    public function getStandard(string $name) : StandardAbstract
396
    {
397
        $name = strtolower($name);
398
        if (array_key_exists($name, $this->standards)) {
399
            return $this->standards[$name];
400
        }
401
402
        throw new \OutOfBoundsException("no standard found for $name");
403
    }
404
405
    /**
406
     * @param  \FeedIo\FeedInterface $feed
407
     * @param  string                $message
408
     * @return FeedIo
409
     */
410
    protected function logAction(FeedInterface $feed, string $message) : FeedIo
411
    {
412
        $class = get_class($feed);
413
        $this->logger->debug("$message (feed class : $class)");
414
415
        return $this;
416
    }
417
}
418