RedismanagerController   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 192
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A _construct() 0 6 1
A indexAction() 0 6 1
A gridAction() 0 5 1
A flushDbAction() 0 11 3
A massAction() 0 11 3
B flushByKeyAction() 0 33 6
A flushAllAction() 0 7 2
A _prepareKey() 0 8 2
A _isAllowed() 0 5 1
A _flushDb() 0 15 2
A _getHelper() 0 7 2
1
<?php
2
/**
3
 * Redis Management Module
4
 *
5
 * PHP Version 5
6
 *
7
 * @category  Steverobbins
8
 * @package   Steverobbins_Redismanager
9
 * @author    Steve Robbins <[email protected]>
10
 * @copyright 2014 Steve Robbins
11
 * @license   http://creativecommons.org/licenses/by/3.0/deed.en_US Creative Commons Attribution 3.0 Unported License
12
 * @link      https://github.com/steverobbins/Magento-Redismanager
13
 */
14
15
/**
16
 * Handle view of configured redis services
17
 *
18
 * @category  Steverobbins
19
 * @package   Steverobbins_Redismanager
20
 * @author    Steve Robbins <[email protected]>
21
 * @copyright 2014 Steve Robbins
22
 * @license   http://creativecommons.org/licenses/by/3.0/deed.en_US Creative Commons Attribution 3.0 Unported License
23
 * @link      https://github.com/steverobbins/Magento-Redismanager
24
 */
25
class Steverobbins_Redismanager_Adminhtml_RedismanagerController
26
    extends Mage_Adminhtml_Controller_Action
27
{
28
    /**
29
     * Cached helper
30
     *
31
     * @var Steverobbins_Redismanager_Helper_Data
32
     */
33
    protected $_helper;
34
35
    /**
36
     * Set title
37
     *
38
     * @return void
39
     */
40
    protected function _construct()
41
    {
42
        parent::_construct();
43
        $this->_title($this->__('System'))
44
            ->_title($this->__('Redis Management'));
45
    }
46
    
47
    /**
48
     * Manager page
49
     *
50
     * @return void
51
     */
52
    public function indexAction()
53
    {
54
        $this->loadLayout();
55
        $this->_setActiveMenu('system/redismanager');
56
        $this->renderLayout();
57
    }
58
    
59
    /**
60
     * Bring in grid HTML with ajax
61
     *
62
     * @return void
63
     */
64
    public function gridAction()
65
    {
66
        $this->loadLayout()
67
            ->renderLayout();
68
    }
69
70
    /**
71
     * Flush a Redis DB
72
     *
73
     * @return void
74
     */
75
    public function flushDbAction()
76
    {
77
        $id       = $this->getRequest()->getParam('id');
78
        $services = $this->_getHelper()->getServices();
79
        if ($id === false || !isset($services[$id])) {
80
            Mage::getSingleton('core/session')->addError($this->__('Unable to flush Redis database'));
81
        } else {
82
            $this->_flushDb($services[$id]);
83
        }
84
        $this->_redirect('*/*');
85
    }
86
87
    /**
88
     * Process multiple services
89
     *
90
     * @return void
91
     */
92
    public function massAction()
93
    {
94
        $services = $this->_getHelper()->getServices();
95
        $ids      = $this->getRequest()->getPost('service');
96
        if (count($ids)) {
97
            foreach ($this->getRequest()->getPost('service') as $id) {
98
                $this->_flushDb($services[$id]);
99
            }
100
        }
101
        $this->_redirect('*/*');
102
    }
103
104
    /**
105
     * Flush matching keys
106
     *
107
     * @return void
108
     */
109
    public function flushByKeyAction()
110
    {
111
        $keys       = $this->getRequest()->getPost('redisKeys');
112
        $clearCount = 0;
113
        if ($keys) {
114
            $keys       = explode("\n", $keys);
115
            $keys       = array_map(array($this, '_prepareKey'), $keys);
116
            $helper     = $this->_getHelper();
117
            $services   = $helper->getServices();
118
            foreach ($services as $service) {
119
                $redis = $this->_getHelper()->getRedisInstance(
120
                    $service['host'],
121
                    $service['port'],
122
                    $service['password'],
123
                    $service['db']
124
                )->getRedis();
125
                $matched = array();
126
                foreach ($keys as $key) {
127
                    if ($key !== false) {
128
                        $matched = array_merge($matched, $redis->keys($key));
129
                    }
130
                }
131
                if (count($matched)) {
132
                    $clearCount += $redis->del($matched);
133
                }
134
            }
135
        }
136
        Mage::getSingleton('core/session')->addSuccess($this->__(
137
            '%s key(s) cleared',
138
            $clearCount
139
        ));
140
        $this->_redirect('*/*');
141
    }
142
143
    /**
144
     * Flushes all services
145
     *
146
     * @return void
147
     */
148
    public function flushAllAction()
149
    {
150
        $flushThis = $this->getRequest()->getParam('host', null);
151
        if (is_array($this->_getHelper()->flushAll($flushThis))) {
152
            $this->_redirect('*/*');
153
        }
154
    }
155
156
    /**
157
     * Prepare keys for search
158
     *
159
     * @param  string $key
160
     * @return boolean|string
161
     */
162
    protected function _prepareKey($key)
163
    {
164
        $key = trim($key);
165
        if (empty($key)) {
166
            return false;
167
        }
168
        return '*' . $key . '*';
169
    }
170
171
    /**
172
     * ACL check
173
     *
174
     * @return bool
175
     */
176
    protected function _isAllowed()
177
    {
178
        return Mage::getSingleton('admin/session')
179
            ->isAllowed('admin/system/redismanager');
180
    }
181
182
    /**
183
     * Flush a db
184
     *
185
     * @param  array $service
186
     * @return void
187
     */
188
    protected function _flushDb(array $service)
189
    {
190
        try {
191
            $redis = $this->_getHelper()->getRedisInstance(
192
                $service['host'],
193
                $service['port'],
194
                $service['password'],
195
                $service['db']
196
            );
197
            $redis->clean(Zend_Cache::CLEANING_MODE_ALL);
198
            Mage::getSingleton('core/session')->addSuccess($this->__('%s database flushed.', $service['name']));
199
        } catch (Exception $e) {
200
            Mage::getSingleton('core/session')->addError($e->getMessage());
201
        }
202
    }
203
204
    /**
205
     * Get helper
206
     *
207
     * @return Steverobbins_Redismanager_Helper_Data
208
     */
209
    protected function _getHelper()
210
    {
211
        if (is_null($this->_helper)) {
212
            $this->_helper = Mage::helper('redismanager');
213
        }
214
        return $this->_helper;
215
    }
216
}
217