Completed
Push — devel ( 555176...462c3f )
by Miguel
04:34
created

fixCmRedisSessionLocks()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.2
cc 4
eloc 4
nc 3
nop 1
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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) {
0 ignored issues
show
Coding Style introduced by
fixCmRedisSessionLocks uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
67
        if (Mage::helper('core')->isModuleEnabled('Cm_RedisSession')) {
68
            if (!empty($_COOKIE['frontend']) && 'crawler-session' == $_COOKIE['frontend']) {
69
                define('CM_REDISSESSION_LOCKING_ENABLED', false);
70
            }
71
        }
72
    }
73
74
    /**
75
     * Re-apply and save Varnish configuration on config change
76
     *
77
     * @param  mixed $eventObject
78
     * @return null
79
     */
80
    public function adminSystemConfigChangedSection($eventObject) {
81
        if (Mage::helper('turpentine/varnish')->getVarnishEnabled() &&
82
                Mage::helper('turpentine/data')->getAutoApplyOnSave()) {
83
            $result = Mage::getModel('turpentine/varnish_admin')->applyConfig();
84
            $session = Mage::getSingleton('core/session');
85
            foreach ($result as $name => $value) {
86
                if ($value === true) {
87
                    $session->addSuccess(Mage::helper('turpentine/data')
88
                        ->__('VCL successfully applied to: '.$name));
89
                } else {
90
                    $session->addError(Mage::helper('turpentine/data')
91
                        ->__(sprintf('Failed to apply the VCL to %s: %s',
92
                            $name, $value)));
93
                }
94
            }
95
            $cfgr = Mage::getModel('turpentine/varnish_admin')->getConfigurator();
96
            if (is_null($cfgr)) {
97
                $session->addError(Mage::helper('turpentine/data')
98
                    ->__('Failed to load configurator'));
99
            } else {
100
                $result = $cfgr->save($cfgr->generate());
101
                if ($result[0]) {
102
                    $session->addSuccess(Mage::helper('turpentine/data')
103
                        ->__('The VCL file has been saved.'));
104
                } else {
105
                    $session->addError(Mage::helper('turpentine/data')
106
                        ->__('Failed to save the VCL file: '.$result[1]['message']));
107
                }
108
            }
109
        }
110
    }
111
}
112