Completed
Push — master ( 35e61c...496f6c )
by Iurii
01:33
created

Cache::cmdGetCache()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 0
1
<?php
2
3
/**
4
 * @package CLI
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2018, Iurii Makukh <[email protected]>
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\cli\controllers;
11
12
use gplcart\core\Cache as CoreCache;
13
14
/**
15
 * Handles commands related to system cache
16
 */
17
class Cache extends Base
18
{
19
20
    /**
21
     * Cache class instance
22
     * @var \gplcart\core\Cache $cache
23
     */
24
    protected $cache;
25
26
    /**
27
     * @param CoreCache $cache
28
     */
29
    public function __construct(CoreCache $cache)
30
    {
31
        parent::__construct();
32
33
        $this->cache = $cache;
34
    }
35
36
    /**
37
     * Callback for "cache-get" command
38
     */
39
    public function cmdGetCache()
40
    {
41
        $id = $this->getParam(0);
42
43
        if (empty($id)) {
44
            $this->errorAndExit($this->text('Invalid command'));
45
        }
46
47
        $result = $this->cache->get($id);
48
49
        if (!isset($result)) {
50
            $this->errorAndExit($this->text('Invalid ID'));
51
        }
52
53
        $this->outputFormat($result, 'json');
54
        $this->output();
55
    }
56
57
    /**
58
     * Callback for "cache-clear" command
59
     */
60
    public function cmdClearCache()
61
    {
62
        $id = $this->getParam(0);
63
        $all = $this->getParam('all');
64
65
        if (!isset($id) && empty($all)) {
66
            $this->errorAndExit($this->text('Invalid command'));
67
        }
68
69
        $result = false;
70
71
        if (isset($id)) {
72
            $pattern = $this->getParam('pattern');
73
            if (isset($pattern)) {
74
                $result = $this->cache->clear($id, array('pattern' => $pattern));
75
            } else {
76
                $result = $this->cache->clear($id);
77
            }
78
        } else if ($all) {
79
            $result = $this->cache->clear(null);
80
        }
81
82
        if (!$result) {
83
            $this->errorAndExit($this->text('An error occurred'));
84
        }
85
86
        $this->output();
87
    }
88
89
}
90