PhpfastcacheGetCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
/**
4
 *
5
 * This file is part of phpFastCache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author PastisD https://github.com/PastisD
13
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
14
 *
15
 */
16
declare(strict_types=1);
17
18
namespace Phpfastcache\Bundle\Command;
19
20
use Phpfastcache\Bundle\Service\Phpfastcache;
21
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputArgument;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Output\OutputInterface;
26
use Symfony\Component\Console\Style\SymfonyStyle;
27
28
class PhpfastcacheGetCommand extends Command
29
{
30
    /**
31
     * @var \Phpfastcache\Bundle\Service\Phpfastcache
32
     */
33
    protected $phpfastcache;
34
35
    /**
36
     * @var
37
     */
38
    protected $parameters;
39
40
    /**
41
     * PhpfastcacheCommand constructor.
42
     * @param \Phpfastcache\Bundle\Service\Phpfastcache $phpfastcache
43
     * @param $parameters
44
     */
45
    public function __construct(Phpfastcache $phpfastcache, $parameters)
46
    {
47
        $this->phpfastcache = $phpfastcache;
48
        $this->parameters = $parameters;
49
50
        parent::__construct();
51
    }
52
53
    protected function configure()
54
    {
55
        $this
56
          ->setName('phpfastcache:get')
57
          ->setDescription('Get phpfastcache cache value')
58
          ->addArgument(
59
            'driver',
60
            InputArgument::REQUIRED,
61
            'Cache name to clear'
62
          )
63
          ->addArgument(
64
            'key',
65
            InputArgument::REQUIRED,
66
            'Cache key'
67
          );
68
    }
69
70
    /**
71
     * @param \Symfony\Component\Console\Input\InputInterface $input
72
     * @param \Symfony\Component\Console\Output\OutputInterface $output
73
     * @return int|null|void
74
     */
75
    protected function execute(InputInterface $input, OutputInterface $output)
76
    {
77
        $io = new SymfonyStyle($input, $output);
78
        $caches = $this->parameters;
79
80
        $driver = $input->getArgument('driver');
81
        $cacheKey = $input->getArgument('key');
82
83
        if (\array_key_exists($driver, $caches[ 'drivers' ])) {
84
            $io->section($driver);
85
            $cacheItem = $this->phpfastcache->get($driver)->getItem($cacheKey);
86
87
            if($cacheItem->isHit()){
88
                $cacheItemValue = $cacheItem->get();
89
                if(!\is_object($cacheItemValue)){
90
                    ob_start();
91
                    var_dump($cacheItemValue);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($cacheItemValue); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
92
                    $content = ob_get_contents();
93
                    ob_end_clean();
94
                    $io->write('<bg=green;fg=black>[HIT]</> ' . $content);
95
                }else{
96
                    $io->write('<bg=green;fg=black>[HIT]</> (object) ' . \get_class($cacheItemValue));
97
                }
98
                $io->write('This item will expires in <fg=green>' . $cacheItem->getTtl() .'</> second(s)');
99
            }else{
100
                $io->write('<bg=yellow;fg=red>[MISS]</> The cache item "' . $cacheKey . '" does not (yet) exists !');
101
            }
102
        } else {
103
            $io->error("Cache instance {$driver} does not exists");
104
        }
105
    }
106
}