GetCommand::getEntry()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 4
nop 2
dl 0
loc 25
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace PhpCache\Commands;
4
5
use PhpCache\CacheClient\CacheClient;
6
use PhpCache\ServiceManager\ServiceManager;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * Description of PhpCache.
15
 *
16
 * @author dude920228
17
 */
18
class GetCommand extends Command
19
{
20
    private $serviceManager;
21
22
    public function __construct($config, $name = null)
23
    {
24
        parent::__construct($name);
25
        $this->serviceManager = new ServiceManager($config);
26
    }
27
28
    protected function configure()
29
    {
30
        $this->setName('get');
31
        $this->setHelp('This command allows you to get entries by key');
32
        $this->addArgument('key', InputArgument::OPTIONAL, 'Key for the cache entry');
33
    }
34
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $this->getEntry($input, $output);
38
    }
39
40
    private function getEntry(InputInterface $input, OutputInterface $output)
41
    {
42
        $key = $input->getArgument('key');
43
        /* @var $client CacheClient */
44
        $client = $this->serviceManager->get(CacheClient::class);
45
        $table = (new Table($output))->setHeaders(['KEY', 'VALUE']);
46
        if (!is_null($key)) {
47
            $value = $client->get($key);
48
            if ($value === false) {
49
                $output->writeln('<comment>No entry found for key: '.$key.'</comment>');
50
51
                return;
52
            }
53
            $table->setRows([[$key, $value]]);
54
            $table->render();
55
56
            return;
57
        }
58
        $entries = $client->getEntries();
59
        $op = [];
60
        foreach ($entries as $key => $value) {
61
            $op[] = [$key, $value];
62
        }
63
        $table->setRows($op);
64
        $table->render();
65
    }
66
}
67