Completed
Pull Request — master (#38)
by Boris
04:23 queued 02:11
created

ApcBinLoadCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
nc 1
cc 1
eloc 7
nop 0
crap 1
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 Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class ApcBinLoadCommand extends AbstractCommand
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 4
    protected function configure()
24
    {
25 4
        $this
26 4
            ->setName('apc:bin:load')
27 4
            ->setDescription('Load a binary dump into the APC file and user variables')
28 4
            ->addOption('--file', '-f', InputOption::VALUE_OPTIONAL, "File to read binary data from")
29 4
            ->addOption('--no-verification', '-a', InputOption::VALUE_NONE, "Don't perform MD5 & CRC32 verification before loading data")
30 4
            ->setHelp('');
31 4
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38
        $this->ensureExtensionLoaded('apc');
39
40
        $file = $input->getOption('file');
41
        $noVerification = $input->getOption('no-verification');
42
43
        if (!$file) {
44
            $file = 'php://stdin';
45
        } else {
46
            if (!is_file($file) || !is_readable($file)) {
47
                throw new \InvalidArgumentException(sprintf("Could not read from file: %s", $file));
48
            }
49
        }
50
51
        $dump = file_get_contents($file);
52
        $flags = 0;
53
54
        if (!$noVerification) {
55
            $flags = APC_BIN_VERIFY_MD5 | APC_BIN_VERIFY_CRC32;
56
        }
57
58
        $success = $this->getCacheTool()->apc_bin_load($dump, $flags);
59
60
        if ($output->isVerbose()) {
61
            if ($success) {
62
                $output->writeln("<comment>Load was successful</comment>");
63
            } else {
64
                $output->writeln("<comment>Load was unsuccessful</comment>");
65
            }
66
        }
67
68
        return $success ? 0 : 1;
69
    }
70
}
71