Passed
Push — master ( c7807f...0cb380 )
by Samuel
02:52
created

ApcuCacheInfoCommand::getRows()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 1
dl 0
loc 16
ccs 13
cts 13
cp 1
crap 2
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Command;
13
14
use CacheTool\Util\Formatter;
15
use Symfony\Component\Console\Helper\Table;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class ApcuCacheInfoCommand extends AbstractCommand
20
{
21
    protected static $apcFix = [
22
        'stime' => "start_time",
23
        'atime' => "access_time",
24
        'mtime' => "modification_time",
25
        'ctime' => "creation_time",
26
        'dtime' => "deletion_time",
27
28
        'nslots' => "num_slots",
29
        'nhits' => "num_hits",
30
        'nmisses' => "num_misses",
31
        'ninserts' => "num_inserts",
32
        'nentries' => "num_entries",
33
        'nexpunges' => "expunges",
34
        "num_expunges" => "expunges",
35
36
        'key' => "info",
37
    ];
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 20
    protected function configure()
43
    {
44
        $this
45 20
            ->setName('apcu:cache:info')
46 20
            ->setDescription('Shows APCu user & system cache information')
47 20
            ->setHelp('');
48 20
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 1
    protected function execute(InputInterface $input, OutputInterface $output): int
54
    {
55 1
        $this->ensureExtensionLoaded('apcu');
56
57 1
        $info = $this->getCacheTool()->apcu_cache_info(true);
58
59 1
        $this->normalize($info);
60
61 1
        if (empty($info)) {
62
            throw new \RuntimeException("Could not fetch info from APCu");
63
        }
64
65 1
        $table = new Table($output);
66 1
        $table->setHeaders(['Name', 'Info']);
67 1
        $table->setRows($this->getRows($info));
68 1
        $table->render();
69
70 1
        return 0;
71
    }
72
73
    /**
74
     * @param  array $info
75
     * @return array
76
     */
77 1
    protected function getRows($info)
78
    {
79
        // missing: cache_list, deleted_list, slot_distribution
80
        return [
81 1
            ['Slots', $info['num_slots']],
82 1
            ['TTL', $info['ttl']],
83 1
            ['Hits', number_format($info['num_hits'])],
84 1
            ['Misses', number_format($info['num_misses'])],
85 1
            ['Inserts', number_format($info['num_inserts'])],
86 1
            ['Expunges', number_format($info['expunges'])],
87 1
            ['Start time', Formatter::date($info['start_time'], 'U')],
88 1
            ['Memory size', Formatter::bytes($info['mem_size'])],
89 1
            ['Entries', number_format($info['num_entries'])],
90 1
            ['File upload progress', ini_get('apcu.rfc1867') ? 'Yes' : 'No'],
91 1
            ['Memory type', $info['memory_type']],
92 1
            ['Locking type', $info['locking_type'] ?? 'Not Supported'],
93
        ];
94
    }
95
96
    /**
97
     * Fix inconsistencies between APC and APCu
98
     *
99
     * @param  array  &$array
100
     */
101 2
    protected function normalize(array &$array)
102
    {
103 2
        foreach ($array as $key => $value) {
104 2
            if (array_key_exists($key, self::$apcFix)) {
105
                unset($array[$key]);
106
                $array[self::$apcFix[$key]] = $value;
107
            }
108
        }
109 2
    }
110
}
111