IndexCommand::execute()   B
last analyzed

Complexity

Conditions 9
Paths 40

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 30
nc 40
nop 2
dl 0
loc 44
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the InMemoryList package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
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 InMemoryList\Command;
12
13
use Symfony\Component\Console\Helper\Table;
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class IndexCommand extends BaseCommand
19
{
20
    /**
21
     * IndexCommand constructor.
22
     *
23
     * @param null  $driver
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $driver is correct as it would always require null to be passed?
Loading history...
24
     * @param array $parameters
25
     */
26
    public function __construct($driver = null, array $parameters = [])
27
    {
28
        parent::__construct(
29
            'iml_cache_index',
30
            $driver,
31
            $parameters
32
        );
33
    }
34
35
    protected function configure()
36
    {
37
        $this
38
            ->setName('iml:cache:index')
39
            ->setDescription('Get all data stored in cache.')
40
            ->setHelp('This command displays in a table all data stored in cache.')
41
            ->addArgument('from', InputArgument::OPTIONAL, 'Type date from you wish to display data. Eg: 20-06-2017')
42
            ->addArgument('to', InputArgument::OPTIONAL, 'Type date to you wish to display data. Eg: now')
43
        ;
44
    }
45
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $cache = $this->createClient($this->driver, $this->parameters);
49
50
        $from = $input->getArgument('from') ? new \DateTime($input->getArgument('from')) : null;
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('from') can also be of type string[]; however, parameter $time of DateTime::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $from = $input->getArgument('from') ? new \DateTime(/** @scrutinizer ignore-type */ $input->getArgument('from')) : null;
Loading history...
51
        $to = $input->getArgument('to') ? new \DateTime($input->getArgument('to')) : null;
52
        $index = $cache->getRepository()->getIndexInRangeDate($from, $to);
53
54
        if ($index && count($index)) {
55
            ksort($index);
56
            $table = new Table($output);
57
            $table->setHeaders(['#', 'List', 'Created on', 'Chunks', 'Chunk size', 'Headers', 'Ttl', 'Expires on', 'Items']);
58
59
            $counter = 0;
60
            foreach ($index as $item) {
61
                $item = $item;
62
                $listUuid = $item['uuid'];
63
64
                $headers = (is_array($item['headers']) and count($item['headers'])) ? $this->implodeArrayToAString($item['headers']) : 'empty';
65
66
                /** @var \DateTimeImmutable $created_on */
67
                $created_on = $item['created_on'];
68
                $expire_date = ($item['ttl']) ? $created_on->add(new \DateInterval('PT'.$item['ttl'].'S'))->format('Y-m-d H:i:s') : '<fg=red>never</>';
69
70
                $table->setRow(
71
                    $counter,
72
                    [
73
                        $counter + 1,
74
                        '<fg=yellow>'.$listUuid.'</>',
75
                        $created_on->format('Y-m-d H:i:s'),
76
                        $item['chunks'],
77
                        $item['chunk-size'],
78
                        $headers,
79
                        $item['ttl'],
80
                        $expire_date,
81
                        $item['size'],
82
                    ]
83
                );
84
                ++$counter;
85
            }
86
87
            $table->render();
88
        } else {
89
            $output->writeln('<fg=red>['.$this->driver.'] Empty Index.</>');
90
        }
91
    }
92
93
    /**
94
     * @param array $input
95
     *
96
     * @return string
97
     */
98
    protected function implodeArrayToAString(array $input)
99
    {
100
        $output = implode(', ', array_map(
101
            function ($v, $k) {
102
                if (is_array($v)) {
103
                    return $k.'[]='.implode('&'.$k.'[]=', $v);
104
                } else {
105
                    return $k.'='.$v;
106
                }
107
            },
108
            $input,
109
            array_keys($input)
110
        ));
111
112
        return $output;
113
    }
114
}
115