CacheHandler::handleClear()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Buttress\Concrete\CommandBus\Handler;
4
5
use Buttress\Concrete\Client\Connection\Connection;
6
use Buttress\Concrete\Client\Connection\LegacyConnection;
7
use Buttress\Concrete\Client\Connection\ModernConnection;
8
use Buttress\Concrete\CommandBus\Command\Cache\Clear;
9
use Buttress\Concrete\Locator\Site;
10
use Cache;
11
use Concrete\Core\Site\Service;
12
use League\CLImate\CLImate;
13
use Loader;
14
15
class CacheHandler
16
{
17
    /** @var \Buttress\Concrete\Locator\Site */
18
    private $site;
19
20
    /** @var \League\CLImate\CLImate */
21
    private $cli;
22
23
    /** @var \Buttress\Concrete\Client\Connection\Connection */
24
    private $connection;
25
26
    public function __construct(Connection $connection, CLImate $cli, Site $site)
27
    {
28
        $this->connection = $connection;
29
        $this->cli = $cli;
30
        $this->site = $site;
31
    }
32
33
    /**
34
     * Handle the Clear command
35
     * @param \Buttress\Concrete\CommandBus\Command\Cache\Clear $clear
36
     */
37
    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...
38
    {
39
        if ($this->connection instanceof ModernConnection) {
40
            $this->clearModern($this->connection);
41
        } else {
42
            $this->clearLegacy($this->connection);
0 ignored issues
show
Compatibility introduced by
$this->connection of type object<Buttress\Concrete...\Connection\Connection> is not a sub-type of object<Buttress\Concrete...ction\LegacyConnection>. It seems like you assume a concrete implementation of the interface Buttress\Concrete\Client\Connection\Connection to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
43
        }
44
    }
45
46
    /**
47
     * Clear cache for a ModernConnection
48
     * @param \Buttress\Concrete\Client\Connection\ModernConnection $connection
49
     */
50
    private function clearModern(ModernConnection $connection)
51
    {
52
        $app = $connection->getApplication();
53
54
        if (class_exists(Service::class)) {
55
            $site = $app->make(Service::class)->getDefault()->getSiteName();
56
        } else {
57
            $site = $app['config']['concrete.site'];
58
        }
59
60
        $app->clearCaches();
61
        $this->notify($site);
62
    }
63
64
    /**
65
     * Clear cache for a LegacyConnection
66
     * @param LegacyConnection $connection
67
     */
68
    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...
69
    {
70
        $site = SITE;
71
        Loader::library('cache');
72
        $cache = new Cache;
73
        $cache->flush();
74
        $this->notify($site);
75
    }
76
77
    private function notify($site)
78
    {
79
        $this->cli->info(
80
            sprintf('Cleared cache on <bold>%s</bold>', $site)
81
        );
82
    }
83
84
}
85