Completed
Push — master ( 7381e1...0d723c )
by Korvin
02:16
created

CacheCommand::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.4285
ccs 0
cts 9
cp 0
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Buttress\Concrete\Console\Command;
4
5
use Buttress\Concrete\Console\Command\Manager\CommandManager;
6
use Buttress\Concrete\Console\Command\Manager\DefinitionFactory;
7
use Buttress\Concrete\CommandBus\Command\Cache\Clear;
8
use Buttress\Concrete\Locator\Site;
9
use Buttress\Concrete\Route\RouteCollector;
10
use League\CLImate\Argument\Manager;
11
use League\CLImate\CLImate;
12
use League\CLImate\Util\Output;
13
use League\CLImate\Util\Writer\Buffer;
14
use League\Tactician\CommandBus;
15
16
class CacheCommand implements Command
17
{
18
19
    /** @var \League\CLImate\CLImate */
20
    private $cli;
21
22
    /** @var \League\Tactician\CommandBus */
23
    private $bus;
24
25
    public function __construct(CLImate $cli, CommandBus $bus)
26
    {
27
        $this->cli = $cli;
28
        $this->bus = $bus;
29
    }
30
31
    /**
32
     * Handles `c5 cache:clear`
33
     */
34
    public function clear(Site $site)
35
    {
36
        list($clear) = $commands = $this->getCommands($site);
37
38
        $clear->parse();
39
40
        if ($clear->get('quiet')) {
41
            $this->cli->output->defaultTo('buffer');
42
        }
43
44
        // Create a new commandbus command
45
        $command = new Clear();
46
47
        // Send that command
48
        $this->bus->handle($command);
49
    }
50
51
    /**
52
     * Get the command definitions this command provides
53
     *
54
     * @param \Buttress\Concrete\Locator\Site|null $site
55
     * @return \League\CLImate\Argument\Manager[] A list containing a manager for each command this instance offers
56
     */
57
    public function getCommands(Site $site)
58
    {
59
        $commands = [];
60
        if ($this->enabled($site)) {
61
            $clear = new CommandManager('cache:clear');
62
            $clear->description('Clear the site cache');
63
            $clear->add([
64
                'quiet' => [
65
                    'prefix' => 'q',
66
                    'longPrefix' => 'quiet',
67
                    'noValue' => true
68
                ]
69
            ]);
70
71
            $commands[] = $clear;
72
        }
73
74
        return $commands;
75
    }
76
77
    /**
78
     * Register command routes
79
     *
80
     * @param \Buttress\Concrete\Route\RouteCollector $collector
81
     * @param \Buttress\Concrete\Locator\Site|null $site
82
     */
83
    public function registerRoutes(RouteCollector $collector, Site $site = null)
84
    {
85
        if ($this->enabled($site)) {
86
            $collector->addRoute('cache:clear', [$this, 'clear']);
87
            $collector->addRoute('clear:cache', [$this, 'clear']);
88
        }
89
    }
90
91
    /**
92
     * Check if we should be enabled
93
     *
94
     * @param \Buttress\Concrete\Locator\Site $site
95
     * @return bool
96
     */
97
    private function enabled(Site $site = null)
98
    {
99
        return ($site && version_compare($site->getVersion(), '5.5.0') > 0);
100
    }
101
}
102