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

CacheHandler::confirm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
rs 9.6666
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Buttress\Concrete\CommandBus\Handler;
4
5
use Buttress\Concrete\Client\Connection\Connection;
6
use Buttress\Concrete\Client\Connection\ModernConnection;
7
use Buttress\Concrete\CommandBus\Command\Cache\Clear;
8
use Buttress\Concrete\Locator\Site;
9
use Concrete\Core\Site\Service;
10
use League\CLImate\CLImate;
11
12
class CacheHandler
13
{
14
    /** @var \Buttress\Concrete\Locator\Site */
15
    private $site;
16
17
    /** @var \League\CLImate\CLImate */
18
    private $cli;
19
20
    /** @var \Buttress\Concrete\Client\Connection\Connection */
21
    private $connection;
22
23
    public function __construct(Connection $connection, CLImate $cli, Site $site)
24
    {
25
        $this->connection = $connection;
26
        $this->cli = $cli;
27
        $this->site = $site;
28
    }
29
30
    /**
31
     * Handle the Clear command
32
     * @param \Buttress\Concrete\CommandBus\Command\Cache\Clear $clear
33
     */
34
    public function handleClear(Clear $clear)
0 ignored issues
show
Unused Code introduced by
The parameter $clear is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
    {
36
37
        if ($this->connection instanceof ModernConnection) {
38
            $this->clearModern($this->connection);
39
        } else {
40
            $this->clearLegacy($this->connection);
0 ignored issues
show
Documentation introduced by
$this->connection is of type object<Buttress\Concrete...\Connection\Connection>, but the function expects a object<Buttress\Concrete...ndler\LegacyConnection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
        }
42
    }
43
44
    /**
45
     * Clear cache for a ModernConnection
46
     * @param \Buttress\Concrete\Client\Connection\ModernConnection $connection
47
     */
48
    private function clearModern(ModernConnection $connection)
49
    {
50
        $app = $connection->getApplication();
51
        $site = $app->make(Service::class)->getDefault()->getSiteName();
52
53
        if ($this->confirm($site)) {
54
            $app->clearCaches();
55
            $this->notify($site);
56
        } else {
57
            $this->cli->dim('okay.');
58
        }
59
    }
60
61
    /**
62
     * Clear cache for a LegacyConnection
63
     * @param \Buttress\Concrete\CommandBus\Handler\LegacyConnection $connection
64
     */
65
    private function clearLegacy(LegacyConnection $connection)
0 ignored issues
show
Unused Code introduced by
The parameter $connection is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        $site = SITE;
68
        if ($this->confirm($site)) {
69
            Loader::library('cache');
70
            $cache = new Cache;
71
            $cache->flush();
72
            $this->notify($site);
73
        } else {
74
            $this->cli->dim('okay.');
75
        }
76
    }
77
78
    private function confirm($site)
79
    {
80
        /** @var \League\CLImate\TerminalObject\Dynamic\Confirm $input */
81
        $input = $this->cli->confirm(
82
            sprintf('Really clear the cache on <bold>%s</bold>?', $site)
83
        );
84
85
        return strtolower($input->accept(['Yes', 'No'], true)->prompt()) === 'yes';
86
    }
87
88
    private function notify($site)
89
    {
90
        $this->cli->info(
91
            sprintf('Cleared cache on <bold>%s</bold>', $site)
92
        );
93
    }
94
95
}
96