ExportCreateCommand::getTypes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 0
cts 7
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace TreeHouse\IoBundle\Command;
4
5
use PK\CommandExtraBundle\Command\Command;
6
use Symfony\Component\Console\Helper\ProgressBar;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use TreeHouse\IoBundle\Event\ExportFeedEvent;
13
use TreeHouse\IoBundle\Export\ExportEvents;
14
use TreeHouse\IoBundle\Export\FeedExporter;
15
use TreeHouse\IoBundle\Export\FeedType\FeedTypeInterface;
16
17
class ExportCreateCommand extends Command
18
{
19
    /**
20
     * @var FeedExporter
21
     */
22
    protected $exporter;
23
24
    /**
25
     * @param FeedExporter $exporter
26
     */
27
    public function __construct(FeedExporter $exporter)
28
    {
29
        parent::__construct();
30
31
        $this->exporter = $exporter;
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    protected function configure()
38
    {
39
        $this->setName('io:export:create');
40
        $this->addArgument('type', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The type(s) to export feeds for. If left empty, feeds for all known types are exported.');
41
        $this->addOption('force', 'f', InputOption::VALUE_NONE, 'Whether to force generating of the export and ignore cached versions');
42
43
        $this->isSingleProcessed();
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $types = $this->getTypes($input->getArgument('type'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('type') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type null or string; however, TreeHouse\IoBundle\Comma...eateCommand::getTypes() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52
53
        $progress = new ProgressBar($output);
54
        $progress->setFormat('verbose');
55
56
        $this->setupProgressListeners($this->exporter->getDispatcher(), $progress, $output);
57
58
        foreach ($types as $type) {
59
            $this->exporter->exportFeed($type, $input->getOption('force'));
60
        }
61
    }
62
63
    /**
64
     * @param EventDispatcherInterface $dispatcher
65
     * @param ProgressBar              $progress
66
     * @param OutputInterface          $output
67
     */
68
    protected function setupProgressListeners(EventDispatcherInterface $dispatcher, ProgressBar $progress, OutputInterface $output)
69
    {
70
        $dispatcher->addListener(
71
            ExportEvents::PRE_EXPORT_FEED,
72
            function (ExportFeedEvent $event) use ($progress, $output) {
73
                $output->writeln(
74
                    sprintf(
75
                        'Exporting feed for <info>%s</info> to <info>%s</info>',
76
                        $event->getType()->getName(),
77
                        $event->getFile()
78
                    )
79
                );
80
81
                $progress->start($event->getTotal());
82
            }
83
        );
84
85
        $dispatcher->addListener(
86
            ExportEvents::POST_EXPORT_ITEM,
87
            function () use ($progress) {
88
                $progress->advance(1);
89
            }
90
        );
91
92
        $dispatcher->addListener(
93
            ExportEvents::POST_EXPORT_FEED,
94
            function () use ($progress) {
95
                $progress->finish();
96
            }
97
        );
98
    }
99
100
    /**
101
     * @param array $types
102
     *
103
     * @return FeedTypeInterface[]
104
     */
105 View Code Duplication
    protected function getTypes(array $types)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        if (empty($types)) {
108
            return $this->exporter->getTypes();
109
        }
110
111
        $result = [];
112
        foreach ($types as &$type) {
113
            $result[] = $this->exporter->getType($type);
114
        }
115
116
        return $result;
117
    }
118
}
119