Nexcessnet_Turpentine_Varnish_ManagementController   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 236
Duplicated Lines 20.34 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 25
lcom 0
cbo 0
dl 48
loc 236
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A indexAction() 0 9 1
A flushPartialAction() 24 24 4
A flushContentTypeAction() 24 24 4
A flushAllAction() 0 16 3
A applyConfigAction() 0 20 3
A saveConfigAction() 0 22 3
A getConfigAction() 0 19 2
A switchNavigationAction() 0 39 4
A _isAllowed() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Nexcess.net Turpentine Extension for Magento
5
 * Copyright (C) 2012  Nexcess.net L.L.C.
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 */
21
22
class Nexcessnet_Turpentine_Varnish_ManagementController
23
    extends Mage_Adminhtml_Controller_Action {
24
25
    /**
26
     * Management index action, displays buttons/forms for config and cache
27
     * management
28
     *
29
     * @return null
30
     */
31
    public function indexAction() {
32
        $this->_title($this->__('System'))
33
            ->_title(Mage::helper('turpentine')->__('Varnish Management'));
34
        $this->loadLayout()
35
            ->_setActiveMenu('system/turpentine')
36
            ->_addContent($this->getLayout()
37
                ->createBlock('turpentine/management'))
38
            ->renderLayout();
39
    }
40
41
    /**
42
     * Full flush action, flushes all Magento URLs in Varnish cache
43
     *
44
     * @return null
45
     */
46
    public function flushAllAction() {
47
        Mage::dispatchEvent('turpentine_varnish_flush_all');
48
        $result = Mage::getModel('turpentine/varnish_admin')->flushAll();
49
        foreach ($result as $name => $value) {
50
            if ($value === true) {
51
                $this->_getSession()
52
                    ->addSuccess(Mage::helper('turpentine/data')
53
                        ->__('Flushed Varnish cache for: ').$name);
54
            } else {
55
                $this->_getSession()
56
                    ->addError(Mage::helper('turpentine/data')
57
                        ->__('Error flushing Varnish cache on: ').$name);
58
            }
59
        }
60
        $this->_redirect('*/cache');
61
    }
62
63
    /**
64
     * Partial flush action, flushes Magento URLs matching "pattern" in POST
65
     * data
66
     *
67
     * @return null
68
     */
69 View Code Duplication
    public function flushPartialAction() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
70
        $postData = $this->getRequest()->getPost();
71
        if ( ! isset($postData['pattern'])) {
72
            $this->_getSession()->addError($this->__('Missing URL post data'));
73
        } else {
74
            $pattern = $postData['pattern'];
75
            Mage::dispatchEvent('turpentine_varnish_flush_partial',
76
                array('pattern' => $pattern));
77
            $result = Mage::getModel('turpentine/varnish_admin')
78
                ->flushUrl($pattern);
79
            foreach ($result as $name => $value) {
80
                if ($value === true) {
81
                    $this->_getSession()
82
                        ->addSuccess(Mage::helper('turpentine/data')
83
                            ->__('Flushed matching URLs for: ').$name);
84
                } else {
85
                    $this->_getSession()
86
                        ->addError(Mage::helper('turpentine/data')
87
                            ->__('Error flushing matching URLs on: ').$name);
88
                }
89
            }
90
        }
91
        $this->_redirect('*/cache');
92
    }
93
94
    /**
95
     * Flush objects by content type (ctype in POST)
96
     *
97
     * @return null
98
     */
99 View Code Duplication
    public function flushContentTypeAction() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
        $postData = $this->getRequest()->getPost();
101
        if ( ! isset($postData['ctype'])) {
102
            $this->_getSession()->addError($this->__('Missing URL post data'));
103
        } else {
104
            $ctype = $postData['ctype'];
105
            Mage::dispatchEvent('turpentine_varnish_flush_content_type',
106
                array('ctype' => $ctype));
107
            $result = Mage::getModel('turpentine/varnish_admin')
108
                ->flushContentType($ctype);
109
            foreach ($result as $name => $value) {
110
                if ($value === true) {
111
                    $this->_getSession()
112
                        ->addSuccess(Mage::helper('turpentine/data')
113
                            ->__('Flushed matching content-types for: ').$name);
114
                } else {
115
                    $this->_getSession()
116
                        ->addError(Mage::helper('turpentine/data')
117
                            ->__('Error flushing matching content-types on: ').$name);
118
                }
119
            }
120
        }
121
        $this->_redirect('*/cache');
122
    }
123
124
    /**
125
     * Load the current VCL in varnish and activate it
126
     *
127
     * @return null
128
     */
129
    public function applyConfigAction() {
130
        Mage::dispatchEvent('turpentine_varnish_apply_config');
131
        $helper = Mage::helper('turpentine');
132
        $result = Mage::getModel('turpentine/varnish_admin')->applyConfig($helper
133
            ->shouldStripVclWhitespace('apply')
134
        );
135
        foreach ($result as $name => $value) {
136
            if ($value === true) {
137
                $this->_getSession()
138
                    ->addSuccess($helper
139
                        ->__('VCL successfully applied to '.$name));
140
            } else {
141
                $this->_getSession()
142
                    ->addError($helper
143
                        ->__(sprintf('Failed to apply the VCL to %s: %s',
144
                            $name, $value)));
145
            }
146
        }
147
        $this->_redirect('*/cache');
148
    }
149
150
    /**
151
     * Save the config to the configured file action
152
     *
153
     * @return null
154
     */
155
    public function saveConfigAction() {
156
        $cfgr = Mage::getModel('turpentine/varnish_admin')->getConfigurator();
157
        if (is_null($cfgr)) {
158
            $this->_getSession()->addError(
159
                $this->__('Failed to load configurator') );
160
        } else {
161
            Mage::dispatchEvent('turpentine_varnish_save_config',
162
                array('cfgr' => $cfgr));
163
            $result = $cfgr->save($cfgr->generate(
164
                Mage::helper('turpentine')->shouldStripVclWhitespace('save') ));
165
            if ($result[0]) {
166
                $this->_getSession()
167
                    ->addSuccess(Mage::helper('turpentine')
168
                        ->__('The VCL file has been saved.'));
169
            } else {
170
                $this->_getSession()
171
                    ->addError(Mage::helper('turpentine')
172
                        ->__('Failed to save the VCL file: '.$result[1]['message']));
173
            }
174
        }
175
        $this->_redirect('*/cache');
176
    }
177
178
    /**
179
     * Present the generated config for download
180
     *
181
     * @return $this
182
     */
183
    public function getConfigAction() {
184
        $cfgr = Mage::getModel('turpentine/varnish_admin')
185
            ->getConfigurator();
186
        if (is_null($cfgr)) {
187
            $this->_getSession()->addError($this->__('Failed to load configurator'));
188
            $this->_redirect('*/cache');
189
        } else {
190
            $vcl = $cfgr->generate(
191
                Mage::helper('turpentine')->shouldStripVclWhitespace('download') );
192
            $this->getResponse()
193
                ->setHttpResponseCode(200)
194
                ->setHeader('Content-Type', 'text/plain', true)
195
                ->setHeader('Content-Length', strlen($vcl))
196
                ->setHeader('Content-Disposition',
197
                    'attachment; filename=default.vcl')
198
                ->setBody($vcl);
199
            return $this;
200
        }
201
    }
202
203
    /**
204
     * Activate or deactivate the Varnish bypass
205
     *
206
     * @return void
207
     */
208
    public function switchNavigationAction() {
209
        $type = $this->getRequest()->get('type');
210
        if (is_null($type)) {
211
            $this->_redirect('noRoute');
212
            return;
213
        }
214
215
        $cookieName     = Mage::helper('turpentine')->getBypassCookieName();
216
        $cookieModel    = Mage::getModel('core/cookie');
217
        $adminSession   = Mage::getSingleton('adminhtml/session');
218
219
        switch ($type) {
220
            case 'default':
221
                $cookieModel->set(
222
                    $cookieName,
223
                    Mage::helper('turpentine/varnish')->getSecretHandshake(),
224
                    null, // period
225
                    null, // path
226
                    null, // domain
227
                    false, // secure
228
                    true ); // httponly
229
                $adminSession->addSuccess(Mage::helper('turpentine/data')
230
                    ->__('The Varnish bypass cookie has been successfully added.'));
231
            break;
232
233
            case 'varnish':
234
                $cookieModel->delete($cookieName);
235
                $adminSession->addSuccess(Mage::helper('turpentine/data')
236
                    ->__('The Varnish bypass cookie has been successfully removed.'));
237
            break;
238
239
            default:
240
                $adminSession->addError(Mage::helper('turpentine/data')
241
                    ->__('The given navigation type is not supported!'));
242
            break;
243
        }
244
245
        $this->_redirectReferer();
246
    }
247
248
    /**
249
     * Check if a visitor is allowed access to this controller/action(?)
250
     *
251
     * @return boolean
252
     */
253
    protected function _isAllowed() {
254
        return Mage::getSingleton('admin/session')
255
            ->isAllowed('system/turpentine');
256
    }
257
}
258