ByPass   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A methodsAvailable() 0 4 1
A extensionHandles() 0 6 1
A byPassCache() 0 14 3
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Cache
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Cache\Extension;
16
17
use Phossa2\Cache\CachePool;
18
use Phossa2\Cache\Message\Message;
19
use Phossa2\Event\Interfaces\EventInterface;
20
use Phossa2\Event\EventableExtensionAbstract;
21
22
/**
23
 * ByPass extension
24
 *
25
 * Bypass the cache if URL has 'nocache=true' set
26
 *
27
 * ```php
28
 * $ext = new ByPass(['trigger' => 'nocache']);
29
 *
30
 * // enable this ext
31
 * $cachePool->addExtension($ext);
32
 * ```
33
 *
34
 * @package Phossa2\Cache
35
 * @author  Hong Zhang <[email protected]>
36
 * @see     EventableExtensionAbstract
37
 * @see     CachePool
38
 * @version 2.0.1
39
 * @since   2.0.0 added
40
 * @since   2.0.1 moved to EventableExtensionAbstract
41
 */
42
class ByPass extends EventableExtensionAbstract
43
{
44
    /**
45
     * bypass trigger in the url
46
     *
47
     * @var    string
48
     * @access protected
49
     */
50
    protected $trigger = 'nocache';
51
52
    /**
53
     * {@inheritDoc}
54
     */
55
    public function methodsAvailable()/*# : array */
56
    {
57
        return ['byPassCache'];
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    protected function extensionHandles()/*# : array */
64
    {
65
        return [
66
            ['event' => 'cache.*', 'handler' => ['byPassCache', 100]]
67
        ];
68
    }
69
70
    /**
71
     * Skip the cache
72
     *
73
     * 1. $this->trigger = '', always bypass the cache
74
     * 2. if sees $this->trigger in $_REQUEST, bypass the cache
75
     *
76
     * @param  EventInterface $event
77
     * @return bool
78
     * @access public
79
     */
80
    public function byPassCache(EventInterface $event)/*# : bool */
0 ignored issues
show
Coding Style introduced by
byPassCache uses the super-global variable $_REQUEST 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...
81
    {
82
        /* @var CachePool $pool */
83
        $pool = $event->getTarget();
84
85
        if ($this->trigger === '' || isset($_REQUEST[$this->trigger])) {
86
            return $pool->setError(
87
                Message::get(Message::CACHE_EXT_BYPASS),
88
                Message::CACHE_EXT_BYPASS
89
            );
90
        } else {
91
            return true;
92
        }
93
    }
94
}
95