adminSystemConfigChangedSection()   B
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 10
nop 1
dl 0
loc 32
rs 8.4746
c 0
b 0
f 0
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_Model_Observer_Varnish extends Varien_Event_Observer {
23
    /**
24
     * Check sentinel flags and set headers/cookies as needed
25
     *
26
     * Events: http_response_send_before
27
     *
28
     * @param  mixed $eventObject
29
     * @return null
30
     */
31 View Code Duplication
    public function setCacheFlagHeader($eventObject) {
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...
32
        $response = $eventObject->getResponse();
33
        if (Mage::helper('turpentine/varnish')->shouldResponseUseVarnish()) {
34
            $response->setHeader('X-Turpentine-Cache',
35
                Mage::registry('turpentine_nocache_flag') ? '0' : '1');
36
            if (Mage::helper('turpentine/varnish')->getVarnishDebugEnabled()) {
37
                Mage::helper('turpentine/debug')->logDebug(
38
                    'Set Varnish cache flag header to: '.
39
                    (Mage::registry('turpentine_nocache_flag') ? '0' : '1') );
40
            }
41
        }
42
    }
43
44
    /**
45
     * Add a rewrite for catalog/product_list_toolbar if config option enabled
46
     *
47
     * @param Varien_Object $eventObject
48
     * @return null
49
     */
50
    public function addProductListToolbarRewrite($eventObject) {
51
        if (Mage::helper('turpentine/varnish')->shouldFixProductListToolbar()) {
52
            Mage::getSingleton('turpentine/shim_mage_core_app')
53
                ->shim_addClassRewrite('block', 'catalog', 'product_list_toolbar',
54
                    'Nexcessnet_Turpentine_Block_Catalog_Product_List_Toolbar');
55
        }
56
    }
57
58
    /**
59
     * Turpentine sets the fake cookie 'frontend=crawler-session' when a crawler is detected.
60
     * This causes lock problems with Cm_RedisSession, because all crawler hits are requesting the same session lock.
61
     * Cm_RedisSession provides the define CM_REDISSESSION_LOCKING_ENABLED to overrule if locking should be enabled.
62
     *
63
     * @param $eventObject
64
     * @return null
65
     */
66
    public function fixCmRedisSessionLocks($eventObject) {
67
        if (Mage::helper('core')->isModuleEnabled('Cm_RedisSession')) {
68
            if ( ! empty($_COOKIE['frontend']) && 'crawler-session' == $_COOKIE['frontend'] &&
69
                    ! defined('CM_REDISSESSION_LOCKING_ENABLED')) {
70
                define('CM_REDISSESSION_LOCKING_ENABLED', false);
71
            }
72
        }
73
    }
74
75
    /**
76
     * Re-apply and save Varnish configuration on config change
77
     *
78
     * @param  mixed $eventObject
79
     * @return null
80
     */
81
    public function adminSystemConfigChangedSection($eventObject) {
82
        if (Mage::helper('turpentine/varnish')->getVarnishEnabled() &&
83
                Mage::helper('turpentine/data')->getAutoApplyOnSave()) {
84
            $result = Mage::getModel('turpentine/varnish_admin')->applyConfig();
85
            $session = Mage::getSingleton('core/session');
86
            $helper = Mage::helper('turpentine');
87
            foreach ($result as $name => $value) {
88
                if ($value === true) {
89
                    $session->addSuccess($helper
90
                        ->__('VCL successfully applied to: '.$name));
91
                } else {
92
                    $session->addError($helper
93
                        ->__(sprintf('Failed to apply the VCL to %s: %s',
94
                            $name, $value)));
95
                }
96
            }
97
            $cfgr = Mage::getModel('turpentine/varnish_admin')->getConfigurator();
98
            if (is_null($cfgr)) {
99
                $session->addError($helper
100
                    ->__('Failed to load configurator'));
101
            } else {
102
                $result = $cfgr->save($cfgr->generate($helper->shouldStripVclWhitespace('save')));
103
                if ($result[0]) {
104
                    $session->addSuccess($helper
105
                        ->__('The VCL file has been saved.'));
106
                } else {
107
                    $session->addError($helper
108
                        ->__('Failed to save the VCL file: '.$result[1]['message']));
109
                }
110
            }
111
        }
112
    }
113
}
114