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

ReadCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 7
dl 0
loc 37
ccs 0
cts 29
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
B execute() 0 21 5
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
}