Completed
Pull Request — dev (#11)
by
unknown
04:51
created

AbstractPlugin::supportsControllerAndAction()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
ccs 13
cts 13
cp 1
rs 6.7272
cc 7
eloc 13
nc 7
nop 2
crap 7
1
<?php
2
3
namespace Vectorface\SnappyRouter\Plugin;
4
5
use \Exception;
6
use Vectorface\SnappyRouter\Controller\AbstractController;
7
use Vectorface\SnappyRouter\Di\Di;
8
use Vectorface\SnappyRouter\Di\DiProviderInterface;
9
use Vectorface\SnappyRouter\Handler\AbstractHandler;
10
use Vectorface\SnappyRouter\Request\AbstractRequest;
11
12
/**
13
 * The base class for all plugins. It is recommended to extend this class
14
 * instead of implementing the PluginInterface directly.
15
 * @copyright Copyright (c) 2014, VectorFace, Inc.
16
 * @author Dan Bruce <[email protected]>
17
 */
18
abstract class AbstractPlugin implements PluginInterface, DiProviderInterface
19
{
20
    /** the default priority of a plugin */
21
    const PRIORITY_DEFAULT = 1000;
22
23
    /** A string constant indicating the whitelist/blacklist applies to all
24
        actions within a controller */
25
    const ALL_ACTIONS = 'all';
26
27
    /** The plugin options */
28
    protected $options;
29
30
    // properties for plugin/service compatibility
31
    // both properties cannot be set at the same time (one or the other or both
32
    // must be null at any point)
33
    private $whitelist;
34
    private $blacklist;
35
36
    /**
37
     * Constructor for the plugin.
38
     * @param array $options The array of options.
39
     */
40 19
    public function __construct($options)
41
    {
42 19
        $this->options = $options;
43 19
    }
44
45
    /**
46
     * Invoked directly after the router decides which handler will be used.
47
     * @param AbstractHandler $handler The handler selected by the router.
48
     */
49 13
    public function afterHandlerSelected(AbstractHandler $handler)
50
    {
51
52 13
    }
53
54
    /**
55
     * Invoked after the entire route has been handled.
56
     * @param AbstractHandler $handler The handler selected by the router.
57
     */
58 1
    public function afterFullRouteInvoked(AbstractHandler $handler)
59
    {
60
61 1
    }
62
63
    /**
64
     * Invoked if an exception is thrown during the route.
65
     * @param AbstractHandler $handler The handler selected by the router.
66
     * @param Exception $exception The exception that was thrown.
67
     */
68 1
    public function errorOccurred(AbstractHandler $handler, Exception $exception)
69
    {
70
71 1
    }
72
73
    /**
74
     * Returns a sortable number for sorting plugins by execution priority. A lower number indicates
75
     * higher priority.
76
     * @return integer The execution priority (as a number).
77
     */
78 7
    public function getExecutionOrder()
79
    {
80 7
        return self::PRIORITY_DEFAULT;
81
    }
82
83
    /**
84
     * Sets the controller/action whitelist of this particular plugin. Note that
85
     * setting a whitelist will remove any previously set blacklists.
86
     * @param array $whitelist The controller/action whitelist.
87
     * @return AbstractPlugin Returns $this.
88
     */
89 2
    public function setWhitelist($whitelist)
90
    {
91 2
        $this->whitelist = $whitelist;
92 2
        $this->blacklist = null;
93 2
    }
94
95
    /**
96
     * Sets the controller/action blacklist of this particular plugin. Note that
97
     * setting a blacklist will remove any previously set whitelists.
98
     * @param array $blacklist The controller/action blacklist.
99
     * @return AbstractPlugin Returns $this.
100
     */
101 1
    public function setBlacklist($blacklist)
102
    {
103 1
        $this->whitelist = null;
104 1
        $this->blacklist = $blacklist;
105 1
    }
106
107
    /**
108
     * Returns whether or not the given controller and action requested should
109
     * invoke this plugin.
110
     * @param string $controller The requested controller.
111
     * @param string $action The requested action.
112
     * @return boolean Returns true if the given plugin is allowed to run against
113
     *         this controller/action and false otherwise.
114
     */
115 2
    public function supportsControllerAndAction($controller, $action)
116
    {
117 2
        if (null === $this->blacklist) {
118 2
            if (null === $this->whitelist) {
119
                // plugin has global scope
120 1
                return true;
121
            }
122
            // we use a whitelist so ensure the controller is in the whitelist
123 2
            if (!isset($this->whitelist[$controller])) {
124 1
                return false;
125
            }
126
            // the whitelisted controller could be an array of actions or it could
127
            // be mapped to the "all" string
128 2
            if (is_array($this->whitelist[$controller])) {
129 1
                return in_array($action, $this->whitelist[$controller]);
130
            }
131 2
            return (self::ALL_ACTIONS === (string)$this->whitelist[$controller]);
132
        }
133
        // if the controller isn't in the blacklist at all, we're good
134 1
        if (!isset($this->blacklist[$controller])) {
135 1
            return true;
136
        }
137
138
        // if the controller is not an array we return false
139
        // otherwise we check if the action is listed in the array
140 1
        return is_array($this->blacklist[$controller]) &&
141 1
              !in_array($action, $this->blacklist[$controller]);
142
    }
143
144
    /**
145
     * Retrieve an element from the DI container.
146
     * @param string $key The DI key.
147
     * @param boolean $useCache (optional) An optional indicating whether we
148
     *        should use the cached version of the element (true by default).
149
     * @return mixed Returns the DI element mapped to that key.
150
     */
151 2
    public function get($key, $useCache = true)
152
    {
153 2
        return Di::getDefault()->get($key, $useCache);
154
    }
155
156
    /**
157
     * Sets an element in the DI container for the specified key.
158
     * @param string $key The DI key.
159
     * @param mixed  $element The DI element to store.
160
     * @return Di Returns the Di instance.
161
     */
162 1
    public function set($key, $element)
163
    {
164 1
        return Di::getDefault()->set($key, $element);
165
    }
166
}
167