Completed
Push — issue/60 ( 77529b...558c8f )
by Alex
02:56
created

ReadCommand::execute()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 13
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
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 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
        foreach( $feed as $i => $item ) {
45
            $output->writeln("<info>{$item->getLastModified()->format(\DateTime::ATOM)} : {$item->getTitle()}</info>");
46
            $output->writeln("{$item->getDescription()}");
47
48
            if ( ! is_null($limit) && $limit === $i+1 )
49
                break;
50
        }
51
    }
52
53
    /**
54
     * @param string $url
55
     * @return \FeedIo\FeedInterface
56
     */
57
    public function readFeed($url)
58
    {
59
        $feedIo = Factory::create()->getFeedIo();
60
61
        return $feedIo->read($url)->getFeed();
62
    }
63
64
    /**
65
     * @param InputInterface $input
66
     * @return int|null
67
     */
68
    public function getLimit(InputInterface $input)
69
    {
70
        if ( $input->hasOption('count') ) {
71
            return intval($input->getOption('count'));
72
        }
73
74
        return null;
75
    }
76
}
77
78