Completed
Push — master ( e4a74d...e7258c )
by Alex
01:42
created

ReadCommand::execute()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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