CacheClearController::indexAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
namespace mxdiModule\Controller;
3
4
use mxdiModule\Traits\ServiceTrait;
5
use Zend\Cache\Storage\FlushableInterface;
6
use Zend\Cache\Storage\StorageInterface;
7
use Zend\Mvc\Controller\AbstractConsoleController;
8
9
class CacheClearController extends AbstractConsoleController
10
{
11
    use ServiceTrait;
12
13
    /** @var StorageInterface */
14
    protected $storage;
15
16
    /**
17
     * @param StorageInterface $storage
18
     */
19 4
    public function __construct(StorageInterface $storage)
20
    {
21 4
        $this->storage = $storage;
22 4
    }
23
24
    /**
25
     * Clear the cache.
26
     *
27
     * @return int
28
     */
29 3
    public function indexAction()
30
    {
31 3
        if ($this->params('fqcn')) {
32 1
            return $this->flushOne($this->params('fqcn'));
33
        }
34
35 2
        return $this->flushAll();
36
    }
37
38
    /**
39
     * @param string $fqcn
40
     * @return int
41
     */
42 1
    private function flushOne($fqcn)
43
    {
44 1
        $this->storage->removeItem($this->getHash($fqcn));
45 1
        return 0;
46
    }
47
48
    /**
49
     * @return int
50
     */
51 2
    private function flushAll()
52
    {
53 2
        if (!$this->storage instanceof FlushableInterface) {
54 1
            $this->console->writeLine(sprintf('%s adapter is not flushable', get_class($this->storage)));
55 1
            return -1;
56
        }
57
58 1
        $this->storage->flush();
59
60 1
        return 0;
61
    }
62
}
63