ExportCacheClearCommand::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 Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use TreeHouse\IoBundle\Export\FeedExporter;
11
use TreeHouse\IoBundle\Export\FeedType\FeedTypeInterface;
12
13
class ExportCacheClearCommand extends Command
14
{
15
    /**
16
     * @var FeedExporter
17
     */
18
    protected $exporter;
19
20
    /**
21
     * @param FeedExporter $exporter
22
     */
23
    public function __construct(FeedExporter $exporter)
24
    {
25
        parent::__construct();
26
27
        $this->exporter = $exporter;
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    protected function configure()
34
    {
35
        $this->setName('io:export:cache:clear');
36
        $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.');
37
        $this->addOption('where', null, InputOption::VALUE_OPTIONAL, 'Limit the cache to a specific set of the query, use <comment>x</comment> as root alias');
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $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...learCommand::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...
46
47
        $output->writeln(sprintf('Clearing cache for types: <info>%s</info>', implode(', ', $input->getArgument('type'))));
48
49
        foreach ($types as $type) {
50
            $builder = $type->getQueryBuilder('x');
51
            if ($input->getOption('where')) {
52
                $builder->andWhere($input->getOption('where'));
53
            }
54
55
            foreach ($builder->getQuery()->iterate() as list($item)) {
56
                $output->writeln(sprintf('Clearing cache for <comment>%s</comment>', $this->itemToString($item)));
57
                $this->exporter->clearCache($item, [$type]);
58
59
                $builder->getEntityManager()->detach($item);
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param array $types
66
     *
67
     * @return FeedTypeInterface[]
68
     */
69 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...
70
    {
71
        if (empty($types)) {
72
            return $this->exporter->getTypes();
73
        }
74
75
        $result = [];
76
        foreach ($types as &$type) {
77
            $result[] = $this->exporter->getType($type);
78
        }
79
80
        return $result;
81
    }
82
83
    /**
84
     * @param object $item
85
     *
86
     * @return string
87
     */
88
    private function itemToString($item)
89
    {
90
        if (method_exists($item, '__toString')) {
91
            return (string) $item;
92
        }
93
94
        if (method_exists($item, 'getId')) {
95
            return $item->getId();
96
        }
97
98
        return spl_object_hash($item);
99
    }
100
}
101