Completed
Push — master ( bf186f...fded84 )
by Alex
05:17
created

ReadCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 8
dl 0
loc 72
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A execute() 0 20 4
A handleMedias() 0 7 2
A readFeed() 0 6 1
A getLimit() 0 8 2
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