Completed
Push — develop ( 7e22f8...0be30d )
by Tom
04:37
created

ViewCommand::inject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
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
            if (version_compare(phpversion(), '7.0', '>=')) {
94
                $cacheData = unserialize($cacheData, false);
95
            } else {
96
                $cacheData = unserialize($cacheData);
97
            }
98
            if ($cacheData !== false) {
99
                $cacheData = json_encode($cacheData, JSON_PRETTY_PRINT);
100
            }
101
        }
102
103
        $output->writeln($cacheData);
104
    }
105
}
106