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\Command\Check\CheckerAbstract; |
14
|
|
|
use FeedIo\Command\Check\CountChecker; |
15
|
|
|
use FeedIo\Command\Check\HistoryChecker; |
16
|
|
|
use FeedIo\Factory; |
17
|
|
|
use FeedIo\Feed\ItemInterface; |
18
|
|
|
use FeedIo\FeedInterface; |
19
|
|
|
use Symfony\Component\Console\Command\Command; |
20
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
21
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
22
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
23
|
|
|
use Symfony\Component\Console\Input\InputOption; |
24
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
25
|
|
|
|
26
|
|
|
class CheckCommand extends Command |
27
|
|
|
{ |
28
|
|
|
protected function configure() |
29
|
|
|
{ |
30
|
|
|
$this->setName('check') |
31
|
|
|
->setDescription('checks if a feed gets correctly updated') |
32
|
|
|
->addArgument( |
33
|
|
|
'url', |
34
|
|
|
InputArgument::REQUIRED, |
35
|
|
|
'Please provide the feed\' URL' |
36
|
|
|
) |
37
|
|
|
; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
41
|
|
|
{ |
42
|
|
|
$this->configureOutput($output); |
43
|
|
|
$url = $input->getArgument('url'); |
44
|
|
|
|
45
|
|
|
$feedIo = Factory::create()->getFeedIo(); |
46
|
|
|
$feed = $feedIo->read($url)->getFeed(); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$output->writeln("<info>first access to {$feed->getTitle()}</info>"); |
49
|
|
|
|
50
|
|
|
$failures = 0; |
51
|
|
|
|
52
|
|
|
$count = count($feed); |
53
|
|
|
if( $count == 0) { |
54
|
|
|
$failures++; |
55
|
|
|
$output->writeln("<error>empty feed</error>"); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$output->writeln("<info>found {$count} items</info>"); |
59
|
|
|
|
60
|
|
|
$lastModifiedDates = []; |
61
|
|
|
/** @var \FeedIo\Feed\ItemInterface $item */ |
62
|
|
|
foreach ($feed as $i => $item) { |
63
|
|
|
$lastModifiedDates[] = $item->getLastModified(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
sort($lastModifiedDates); |
67
|
|
|
|
68
|
|
|
$first = current($lastModifiedDates); |
69
|
|
|
|
70
|
|
|
/** @var \DateTime $last */ |
71
|
|
|
$last = end($lastModifiedDates); |
72
|
|
|
|
73
|
|
|
$normalDateFlow = true; |
74
|
|
|
if ($last > $first) { |
75
|
|
|
$output->writeln("<info>first item was published on {$first->format(\DateTime::ATOM)}</info>"); |
76
|
|
|
$output->writeln("<info>last item was published on {$last->format(\DateTime::ATOM)}</info>"); |
77
|
|
|
} else { |
78
|
|
|
$output->writeln("<warn>All items have the same date</warn>"); |
79
|
|
|
$normalDateFlow = false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($normalDateFlow) { |
83
|
|
|
$pick = intval($count / 2); |
84
|
|
|
$lastModified = $lastModifiedDates[$pick]; |
85
|
|
|
} else { |
86
|
|
|
$lastModified = $last->sub(new \DateInterval('-1 days')); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$secondHit = $feedIo->readSince($url, $lastModified)->getFeed(); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$count = count($secondHit); |
92
|
|
|
if( $count == 0) { |
93
|
|
|
$failures++; |
94
|
|
|
$output->writeln("<error>empty feed</error>"); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$output->writeln("<info>found {$count} items</info>"); |
98
|
|
|
return $failures; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function configureOutput(OutputInterface $output): void |
102
|
|
|
{ |
103
|
|
|
$outputStyle = new OutputFormatterStyle('black', 'magenta', ['bold']); |
104
|
|
|
$output->getFormatter()->setStyle('warn', $outputStyle); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
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.