MiddlewareQueue   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 3 1
A __construct() 0 6 1
A addMiddleware() 0 4 1
A __invoke() 0 17 3
1
<?php
2
/*
3
 MIT License
4
 Copyright (c) 2010 - 2018 Peter Petermann
5
6
 Permission is hereby granted, free of charge, to any person
7
 obtaining a copy of this software and associated documentation
8
 files (the "Software"), to deal in the Software without
9
 restriction, including without limitation the rights to use,
10
 copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 copies of the Software, and to permit persons to whom the
12
 Software is furnished to do so, subject to the following
13
 conditions:
14
15
 The above copyright notice and this permission notice shall be
16
 included in all copies or substantial portions of the Software.
17
18
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25
 OTHER DEALINGS IN THE SOFTWARE.
26
27
*/
28
namespace King23\Http;
29
30
use Psr\Container\ContainerInterface;
31
use Psr\Http\Message\ResponseFactoryInterface;
32
use Psr\Http\Message\ResponseInterface;
33
use Psr\Http\Message\ServerRequestInterface;
34
use Psr\Http\Server\MiddlewareInterface;
35
use Psr\Http\Server\RequestHandlerInterface;
36
use Psr\Log\LoggerInterface;
37
38
class MiddlewareQueue implements MiddlewareQueueInterface, RequestHandlerInterface
39
{
40
    /**
41
     * @var LoggerInterface
42
     */
43
    protected $log;
44
45
    /**
46
     * @var string[]
47
     */
48
    protected $queue = [];
49
50
    /**
51
     * @var ContainerInterface
52
     */
53
    protected $container;
54
55
    /**
56
     * @var ResponseFactoryInterface
57
     */
58
    private $responseFactory;
59
60
    /**
61
     * @param LoggerInterface $log
62
     * @param ContainerInterface $container
63
     * @param ResponseFactoryInterface $response
64
     */
65
    public function __construct(LoggerInterface $log, ContainerInterface $container, ResponseFactoryInterface $response)
66
    {
67
        $this->log = $log;
68
        $this->container = $container;
69
        $this->responseFactory = $response;
70
    }
71
72
    /**
73
     * register a classname as part of the middleware queue
74
     *
75
     * @param $className
76
     * @return void
77
     */
78
    public function addMiddleware($className)
79
    {
80
        $this->queue[] = $className;
81
    }
82
83
    /**
84
     * execute the queue
85
     *
86
     * @param ServerRequestInterface $request
87
     * @return ResponseInterface
88
     * @throws MiddlewareDoesNotImplementInterfaceException
89
     */
90
    public function handle(ServerRequestInterface $request) : ResponseInterface {
91
        return $this($request);
92
    }
93
94
    /**
95
     * @param ServerRequestInterface $request
96
     * @return ResponseInterface
97
     * @throws MiddlewareDoesNotImplementInterfaceException
98
     */
99
    public function __invoke(
100
        ServerRequestInterface $request
101
    ) {
102
        // if the queue is empty, we have reached the lowest level
103
        if(count($this->queue) == 0) {
104
            return $this->responseFactory->createResponse();
105
        }
106
107
        /** @var MiddlewareInterface $middleware */
108
        $middleware = $this->container->get(array_shift($this->queue));
109
110
        if (!($middleware instanceof MiddlewareInterface)) {
111
            throw new MiddlewareDoesNotImplementInterfaceException();
112
        }
113
114
        return $middleware->process($request, $this);
115
    }
116
}
117