Queue::evalCondition()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 3
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Middleware
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\Middleware;
16
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Phossa2\Shared\Base\ObjectAbstract;
20
use Phossa2\Middleware\Message\Message;
21
use Phossa2\Middleware\Exception\LogicException;
22
use Phossa2\Middleware\Interfaces\QueueInterface;
23
use Phossa2\Middleware\Interfaces\DelegateInterface;
24
use Phossa2\Middleware\Interfaces\ConditionInterface;
25
use Phossa2\Middleware\Interfaces\MiddlewareInterface;
26
27
/**
28
 * Queue
29
 *
30
 * Middleware queue
31
 *
32
 * @package Phossa2\Middleware
33
 * @author  Hong Zhang <[email protected]>
34
 * @version 2.0.1
35
 * @since   2.0.0 added
36
 * @since   2.0.1 added $terminate
37
 */
38
class Queue extends ObjectAbstract implements QueueInterface
39
{
40
    /**
41
     * @var    \SplQueue
42
     * @access protected
43
     */
44
    protected $queue;
45
46
    /**
47
     * @var    bool
48
     * @access protected
49
     */
50
    protected $terminate = false;
51
52
    /**
53
     * Constructor
54
     *
55
     * @param  array $middlewares
56
     * @access public
57
     */
58
    public function __construct(
59
        array $middlewares = []
60
    ) {
61
        // create the queue
62
        $this->queue = new \SplQueue();
63
64
        // fill the queue with middlewares
65
        $this->fillTheQueue($middlewares);
66
    }
67
68
    /**
69
     * Compatible with middlewares of the signature
70
     *
71
     * ```php
72
     * fn($request, $response, callable $next)
73
     * ```
74
     *
75
     * @param  RequestInterface $request
76
     * @param  ResponseInterface $response
77
     * @param  DelegateInterface $next
78
     * @return ResponseInterface
79
     * @access public
80
     */
81
    public function __invoke(
82
        RequestInterface $request,
83
        ResponseInterface $response,
84
        DelegateInterface $next = null
85
    )/*# : ResponseInterface */ {
86
        return $this->process($request, $response, $next);
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function push($middleware, $condition = null)
93
    {
94
        $this->queue->push([$middleware, $condition]);
95
        $this->queue->rewind();
96
        return $this;
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    public function process(
103
        RequestInterface $request,
104
        ResponseInterface $response,
105
        DelegateInterface $next = null
106
    )/*# : ResponseInterface */ {
107
        // process the queue
108
        $response = $this->next($request, $response);
109
110
        // $next is parent queue
111
        return ($next && !$this->terminate) ?
112
            $next->next($request, $response) :
113
            $response;
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    public function next(
120
        RequestInterface $request,
121
        ResponseInterface $response
122
    )/*# : ResponseInterface */ {
123
        if ($this->queue->valid()) {
124
            list($middleware, $condition) = $this->queue->current();
125
            $this->queue->next();
126
127
            if (null === $condition ||
128
                $this->evalCondition($condition, $request, $response)
129
            ) { // run this mw
130
                return $this->runMiddleware($middleware, $request, $response);
131
            } else { // skip this mw
132
                return $this->next($request, $response);
133
            }
134
        }
135
        $this->queue->rewind();
136
        return $response;
137
    }
138
139
    /**
140
     * Fill the queue with middlewares
141
     *
142
     * @param  array $middlewares
143
     * @access protected
144
     */
145
    protected function fillTheQueue(array $middlewares)
146
    {
147
        foreach ($middlewares as $mw) {
148
            // with conditions specified
149
            if (is_array($mw)) {
150
                $this->push($mw[0], $mw[1]);
151
152
            // no condition
153
            } else {
154
                $this->push($mw);
155
            }
156
        }
157
    }
158
159
    /**
160
     * Process/run this middleware
161
     *
162
     * @param  MiddlewareInterface|callable $middleware
163
     * @param  RequestInterface $request
164
     * @param  ResponseInterface $response
165
     * @return ResponseInterface
166
     * @throws LogicException if invalid middleware type
167
     * @access protected
168
     */
169 View Code Duplication
    protected function runMiddleware(
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...
170
        $middleware,
171
        RequestInterface $request,
172
        ResponseInterface $response
173
    )/*# : ResponseInterface */ {
174
        // instance of MiddlewareInterface
175
        if (is_object($middleware) && $middleware instanceof MiddlewareInterface) {
176
            return $middleware->process($request, $response, $this);
177
178
        // old style callable
179
        } elseif (is_callable($middleware)) {
180
            return $middleware($request, $response, $this);
181
182
        // unknown middleware type
183
        } else {
184
            throw new LogicException(
185
                Message::get(Message::MIDDLEWARE_INVALID, $middleware),
186
                Message::MIDDLEWARE_INVALID
187
            );
188
        }
189
    }
190
191
    /**
192
     * Evaluate the condition
193
     *
194
     * support both a callable returns bool value or an object instance of
195
     * ConditionInterface.
196
     *
197
     * @param  ConditionInterface|callable $condition
198
     * @param  RequestInterface $request
199
     * @param  ResponseInterface $response
200
     * @return bool
201
     * @throws LogicException if condition is invalid
202
     * @access protected
203
     */
204 View Code Duplication
    protected function evalCondition(
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...
205
        $condition,
206
        RequestInterface $request,
207
        ResponseInterface $response
208
    )/*# : bool */ {
209
        // instanceof ConditionInterface
210
        if (is_object($condition) && $condition instanceof ConditionInterface) {
211
            return $condition->evaluate($request, $response);
212
213
        // old style callable
214
        } elseif (is_callable($condition)) {
215
            return $condition($request, $response);
216
217
        // unknown type
218
        } else {
219
            throw new LogicException(
220
                Message::get(Message::CONDITION_INVALID, $condition),
221
                Message::CONDITION_INVALID
222
            );
223
        }
224
    }
225
}
226