CacheClearController::flushOne()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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