Completed
Push — issue/36 ( 9c4b3b )
by Alex
03:58
created

ReadCommand::execute()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 0
cts 17
cp 0
rs 8.7624
cc 5
eloc 13
nc 6
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
        $feedIo = Factory::create()->getFeedIo();
39
40
        $feed = $feedIo->read($url)->getFeed();
41
        $output->writeln("<info>{$feed->getTitle()}</info>");
42
43
        $limit = null;
44
        if ( $input->hasOption('count') ) {
45
            $limit = intval($input->getOption('count'));
46
        }
47
48
        foreach( $feed as $i => $item ) {
49
            $output->writeln("<info>{$item->getLastModified()->format(\DateTime::ATOM)} : {$item->getTitle()}</info>");
50
            $output->writeln("{$item->getDescription()}");
51
52
            if ( ! is_null($limit) && $limit === $i+1 )
53
                break;
54
        }
55
    }
56
}