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\Command; |
12
|
|
|
|
13
|
|
|
use FeedIo\Factory; |
14
|
|
|
use FeedIo\Feed\ItemInterface; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ReadCommand |
23
|
|
|
* @codeCoverageIgnore |
24
|
|
|
*/ |
25
|
|
|
class ReadCommand extends Command |
26
|
|
|
{ |
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
$this->setName('read') |
30
|
|
|
->setDescription('reads a feed') |
31
|
|
|
->addArgument( |
32
|
|
|
'url', |
33
|
|
|
InputArgument::REQUIRED, |
34
|
|
|
'Please provide the feed\' URL' |
35
|
|
|
) |
36
|
|
|
->addOption('count', 'c', InputOption::VALUE_OPTIONAL) |
37
|
|
|
; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
41
|
|
|
{ |
42
|
|
|
$url = $input->getArgument('url'); |
43
|
|
|
$feed = $this->readFeed($url); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$output->writeln("<info>{$feed->getTitle()}</info>"); |
46
|
|
|
|
47
|
|
|
$limit = $this->getLimit($input); |
48
|
|
|
|
49
|
|
|
/** @var \FeedIo\Feed\ItemInterface $item */ |
50
|
|
|
foreach ($feed as $i => $item) { |
51
|
|
|
$lastModified = $item->getLastModified() ?: new \DateTime(); |
52
|
|
|
$output->writeln("<info>{$lastModified->format(\DateTime::ATOM)} : {$item->getTitle()}</info>"); |
53
|
|
|
$output->writeln("{$item->getDescription()}"); |
54
|
|
|
|
55
|
|
|
$this->handleMedias($item, $output); |
56
|
|
|
if (! is_null($limit) && $limit === $i+1) { |
57
|
|
|
break; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return 0; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param ItemInterface $item |
66
|
|
|
* @param OutputInterface $output |
67
|
|
|
*/ |
68
|
|
|
protected function handleMedias(ItemInterface $item, OutputInterface $output) |
69
|
|
|
{ |
70
|
|
|
/** @var \FeedIo\Feed\Item\MediaInterface $media */ |
71
|
|
|
foreach ($item->getMedias() as $media) { |
72
|
|
|
$output->writeln("media found : {$media->getUrl()}"); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $url |
78
|
|
|
* @return \FeedIo\FeedInterface |
79
|
|
|
*/ |
80
|
|
|
public function readFeed($url) |
81
|
|
|
{ |
82
|
|
|
$feedIo = Factory::create()->getFeedIo(); |
83
|
|
|
|
84
|
|
|
return $feedIo->read($url)->getFeed(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param InputInterface $input |
89
|
|
|
* @return int|null |
90
|
|
|
*/ |
91
|
|
|
public function getLimit(InputInterface $input) |
92
|
|
|
{ |
93
|
|
|
if ($input->hasOption('count')) { |
94
|
|
|
return intval($input->getOption('count')); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.