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 FeedIo\Reader\Result\UpdateStats; |
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ReadCommand |
24
|
|
|
* @codeCoverageIgnore |
25
|
|
|
*/ |
26
|
|
|
class ReadCommand extends Command |
27
|
|
|
{ |
28
|
|
|
protected function configure() |
29
|
|
|
{ |
30
|
|
|
$this->setName('read') |
31
|
|
|
->setDescription('reads a feed') |
32
|
|
|
->addArgument( |
33
|
|
|
'url', |
34
|
|
|
InputArgument::REQUIRED, |
35
|
|
|
'Please provide the feed\' URL' |
36
|
|
|
) |
37
|
|
|
->addOption('count', 'c', InputOption::VALUE_OPTIONAL) |
38
|
|
|
; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
42
|
|
|
{ |
43
|
|
|
$url = $input->getArgument('url'); |
44
|
|
|
$result = $this->readFeed($url); |
45
|
|
|
$feed = $result->getFeed(); |
46
|
|
|
|
47
|
|
|
$output->writeln("<info>{$feed->getTitle()}</info>"); |
48
|
|
|
|
49
|
|
|
$limit = $this->getLimit($input); |
50
|
|
|
|
51
|
|
|
/** @var \FeedIo\Feed\ItemInterface $item */ |
52
|
|
|
foreach ($feed as $i => $item) { |
53
|
|
|
$lastModified = $item->getLastModified() ?: new \DateTime(); |
54
|
|
|
$output->writeln("<info>{$lastModified->format(\DateTime::ATOM)} : {$item->getTitle()}</info>"); |
55
|
|
|
$output->writeln("{$item->getDescription()}"); |
56
|
|
|
|
57
|
|
|
$this->handleMedias($item, $output); |
58
|
|
|
if (! is_null($limit) && $limit === $i+1) { |
59
|
|
|
break; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$output->writeln("<info>feed last modified: {$feed->getLastModified()->format(\DATE_ATOM)}</info>"); |
64
|
|
|
$nextUpdate = $result->getNextUpdate(); |
65
|
|
|
$output->writeln("<info>computed next update: {$nextUpdate->format(\DATE_ATOM)}</info>"); |
66
|
|
|
|
67
|
|
|
$updateStats = $result->getUpdateStats(); |
68
|
|
|
|
69
|
|
|
$output->writeln("minimum interval between items: {$this->formatDateInterval($updateStats->getMinInterval())}"); |
70
|
|
|
$output->writeln("median interval: {$this->formatDateInterval($updateStats->getMedianInterval())}"); |
71
|
|
|
$output->writeln("average interval: {$this->formatDateInterval($updateStats->getAverageInterval())}"); |
72
|
|
|
$output->writeln("maximum interval: {$this->formatDateInterval($updateStats->getMaxInterval())}"); |
73
|
|
|
|
74
|
|
|
return 0; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ItemInterface $item |
79
|
|
|
* @param OutputInterface $output |
80
|
|
|
*/ |
81
|
|
|
protected function handleMedias(ItemInterface $item, OutputInterface $output) |
82
|
|
|
{ |
83
|
|
|
/** @var \FeedIo\Feed\Item\MediaInterface $media */ |
84
|
|
|
foreach ($item->getMedias() as $media) { |
85
|
|
|
$output->writeln("media found : {$media->getUrl()}"); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param int $interval |
91
|
|
|
* @return \DateInterval |
92
|
|
|
*/ |
93
|
|
|
protected function formatDateInterval(int $interval): string |
94
|
|
|
{ |
95
|
|
|
$zero = new \DateTime('@0'); |
96
|
|
|
$diff = $zero->diff(new \DateTime("@{$interval}")); |
97
|
|
|
return $diff->format('%a days, %h hours, %i minutes, %s seconds'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $url |
102
|
|
|
* @return \FeedIo\Reader\Result |
103
|
|
|
*/ |
104
|
|
|
public function readFeed($url): \FeedIo\Reader\Result |
105
|
|
|
{ |
106
|
|
|
$feedIo = Factory::create()->getFeedIo(); |
107
|
|
|
|
108
|
|
|
return $feedIo->read($url); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param InputInterface $input |
113
|
|
|
* @return int|null |
114
|
|
|
*/ |
115
|
|
|
public function getLimit(InputInterface $input) |
116
|
|
|
{ |
117
|
|
|
if ($input->hasOption('count')) { |
118
|
|
|
return intval($input->getOption('count')); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|