Completed
Push — develop ( 0be30d...8eb8d7 )
by Tom
04:10
created

ViewCommand::execute()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
rs 8.439
cc 6
eloc 16
nc 7
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Cache;
4
5
use Magento\Framework\App\CacheInterface;
6
use Magento\PageCache\Model\Cache\Type as FullPageCache;
7
use N98\Magento\Command\AbstractMagentoCommand;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Validator\Exception\RuntimeException;
13
14
class ViewCommand extends AbstractMagentoCommand
15
{
16
    /**
17
     * @var CacheInterface
18
     */
19
    private $cache;
20
21
    /**
22
     * @var FullPageCache
23
     */
24
    private $fpc;
25
26
    /**
27
     * @param CacheInterface $cache
28
     * @param FullPageCache $fpc
29
     */
30
    public function inject(
31
        CacheInterface $cache,
32
        FullPageCache $fpc
33
    ) {
34
        $this->cache = $cache;
35
        $this->fpc = $fpc;
36
    }
37
38
    /**
39
     * @return void
40
     */
41
    protected function configure()
42
    {
43
        $this
44
            ->setName('cache:view')
45
            ->addArgument(
46
                'id',
47
                InputArgument::REQUIRED,
48
                'Cache-ID'
49
            )
50
            ->addOption(
51
                'fpc',
52
                null,
53
                InputOption::VALUE_NONE,
54
                'Use full page cache instead of core cache'
55
            )
56
            ->addOption(
57
                'unserialize',
58
                null,
59
                InputOption::VALUE_NONE,
60
                'Unserialize output'
61
            )
62
            ->setDescription('Prints a cache entry');
63
    }
64
65
    /**
66
     * @param InputInterface $input
67
     * @param OutputInterface $output
68
     * @return int|void
69
     * @throws RuntimeException
70
     */
71
    protected function execute(InputInterface $input, OutputInterface $output)
72
    {
73
        $this->detectMagento($output, true);
74
        if (!$this->initMagento()) {
75
            return;
76
        }
77
78
        /** CacheInterface|FullPageCache $cacheInstance */
79
        if ($input->hasOption('fpc') && $input->getOption('fpc')) {
80
            $cacheInstance = $this->fpc;
81
        } else {
82
            $cacheInstance = $this->cache;
83
        }
84
85
        $cacheId = $input->getArgument('id');
86
        $cacheData = $cacheInstance->load($cacheId);
87
        if ($cacheData === false) {
88
            $output->writeln('Cache id <info>' . $cacheId . '</info> does not exist (anymore)');
89
            return;
90
        }
91
92
        if ($input->getOption('unserialize')) {
93
            $cacheData = $this->decorateSerialized($cacheData);
94
        }
95
96
        $output->writeln($cacheData);
97
    }
98
99
    private function decorateSerialized($serialized)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
100
    {
101
        if (version_compare(phpversion(), '7.0', '>=')) {
102
            $unserialized = unserialize($serialized, false);
103
        } else {
104
            $unserialized = unserialize($serialized);
105
        }
106
107
        if ($unserialized === false) {
108
            $buffer = $serialized;
109
        } else {
110
            $buffer = json_encode($unserialized, JSON_PRETTY_PRINT);
111
        }
112
113
        return $buffer;
114
    }
115
}
116