Completed
Push — master ( e7258c...bf186f )
by Alex
14s
created

ReadCommand::execute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 15
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 3
nop 2
crap 20
1
<?php
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
class ReadCommand extends Command
22
{
23
    protected function configure()
24
    {
25
        $this->setName('read')
26
            ->setDescription('reads a feed')
27
            ->addArgument(
28
                'url',
29
                InputArgument::REQUIRED,
30
                'Please provide the feed\' URL'
31
            )
32
            ->addOption('count', 'c', InputOption::VALUE_OPTIONAL)
33
        ;
34
    }
35
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $url = $input->getArgument('url');
39
        $feed = $this->readFeed($url);
40
41
        $output->writeln("<info>{$feed->getTitle()}</info>");
42
43
        $limit = $this->getLimit($input);
44
45
        /** @var \FeedIo\Feed\ItemInterface $item */
46
        foreach ($feed as $i => $item) {
47
            $output->writeln("<info>{$item->getLastModified()->format(\DateTime::ATOM)} : {$item->getTitle()}</info>");
48
            $output->writeln("{$item->getDescription()}");
49
50
            $this->handleMedias($item, $output);
51
            if (! is_null($limit) && $limit === $i+1) {
52
                break;
53
            }
54
        }
55
    }
56
57
    /**
58
     * @param ItemInterface $item
59
     * @param OutputInterface $output
60
     */
61
    protected function handleMedias(ItemInterface $item, OutputInterface $output)
62
    {
63
        /** @var \FeedIo\Feed\Item\MediaInterface $media */
64
        foreach ($item->getMedias() as $media) {
65
            $output->writeln("media found : {$media->getUrl()}");
66
        }
67
    }
68
69
    /**
70
     * @param string $url
71
     * @return \FeedIo\FeedInterface
72
     */
73
    public function readFeed($url)
74
    {
75
        $feedIo = Factory::create()->getFeedIo();
76
77
        return $feedIo->read($url)->getFeed();
78
    }
79
80
    /**
81
     * @param InputInterface $input
82
     * @return int|null
83
     */
84
    public function getLimit(InputInterface $input)
85
    {
86
        if ($input->hasOption('count')) {
87
            return intval($input->getOption('count'));
88
        }
89
90
        return null;
91
    }
92
}
93