Completed
Pull Request — master (#284)
by Alex
03:09 queued 01:41
created

ReadCommand::formatDateInterval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
73
        return 0;
74
    }
75
76
    /**
77
     * @param ItemInterface $item
78
     * @param OutputInterface $output
79
     */
80
    protected function handleMedias(ItemInterface $item, OutputInterface $output)
81
    {
82
        /** @var \FeedIo\Feed\Item\MediaInterface $media */
83
        foreach ($item->getMedias() as $media) {
84
            $output->writeln("media found : {$media->getUrl()}");
85
        }
86
    }
87
88
    /**
89
     * @param int $interval
90
     * @return \DateInterval
91
     */
92
    protected function formatDateInterval(int $interval): string
93
    {
94
        $zero = new \DateTime('@0');
95
        $diff = $zero->diff(new \DateTime("@{$interval}"));
96
        return $diff->format('%a days, %h hours, %i minutes, %s seconds');
97
    }
98
99
    /**
100
     * @param $url
101
     * @return \FeedIo\Reader\Result
102
     */
103
    public function readFeed($url): \FeedIo\Reader\Result
104
    {
105
        $feedIo = Factory::create()->getFeedIo();
106
107
        return $feedIo->read($url);
108
    }
109
110
    /**
111
     * @param InputInterface $input
112
     * @return int|null
113
     */
114
    public function getLimit(InputInterface $input)
115
    {
116
        if ($input->hasOption('count')) {
117
            return intval($input->getOption('count'));
118
        }
119
120
        return null;
121
    }
122
}
123