Passed
Pull Request — master (#24)
by Jitendra
03:50
created

Middlewares::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace PhalconExt\Http;
4
5
use Phalcon\Di\Injectable;
0 ignored issues
show
Bug introduced by
The type Phalcon\Di\Injectable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Phalcon\Http\Request;
7
use Phalcon\Http\Response;
8
use Phalcon\Mvc\Application;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Phalcon\Mvc\DispatcherInterface as Dispatcher;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\DispatcherInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Phalcon\Mvc\Micro as MicroApplication;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\Micro was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use PhalconExt\Di\ProvidesDi;
12
13
/**
14
 * A manager for middlewares.
15
 *
16
 * @author  Jitendra Adhikari <[email protected]>
17
 * @license MIT
18
 *
19
 * @link    https://github.com/adhocore/phalcon-ext
20
 */
21
class Middlewares
22
{
23
    use ProvidesDi;
24
25
    protected $middlewares = [];
26
27
    public function __construct(array $middlewares)
28
    {
29
        $this->middlewares = $middlewares;
30
    }
31
32
    /**
33
     * Wraps app with middlewares and runs the app.
34
     *
35
     * @param Injectable $app The app instance: micro or mvc
36
     */
37
    public function wrap(Injectable $app)
38
    {
39
        $isMicro = $app instanceof MicroApplication;
40
41
        if (!$app instanceof Application && !$isMicro) {
42
            throw new \InvalidArgumentException('The app instance is not one of micro or mvc');
43
        }
44
45
        if (!$this->di()->has('application')) {
46
            $this->di()->setShared('application', $app);
47
        }
48
49
        $isMicro ? $this->handleMicro($app) : $this->handleMvc($app);
50
    }
51
52
    /**
53
     * Setup before/after handers for micro app and run the app.
54
     *
55
     * @param Injectable $app
56
     *
57
     * @return void
58
     */
59
    protected function handleMicro(Injectable $app)
60
    {
61
        $app
62
            ->before([$this, 'beforeHandleRequest'])
63
            ->after([$this, 'beforeSendResponse']);
64
65
        $this->handleApp($app);
66
    }
67
68
    /**
69
     * Setup event handlers for mvc app and run the app.
70
     *
71
     * @param Injectable $app
72
     *
73
     * @return void
74
     */
75
    protected function handleMvc(Injectable $app)
76
    {
77
        $evm = $this->di('eventsManager');
78
79
        $evm->attach('application', $this);
80
        $evm->attach('dispatch', $this);
81
        $app->setEventsManager($evm);
82
83
        $this->di('dispatcher')->setEventsManager($evm);
84
85
        $this->handleApp($app);
86
    }
87
88
    /**
89
     * Handles http requests to the app.
90
     *
91
     * @param Injectable $app
92
     *
93
     * @return mixed Whatever is given by $app->handle().
94
     */
95
    protected function handleApp(Injectable $app)
96
    {
97
        $return = $app->handle();
98
99
        $response = $this->di('response');
100
101
        if (!$response->isSent()) {
102
            $response->send();
103
        }
104
105
        return $return;
106
    }
107
108
    public function beforeHandleRequest(): bool
109
    {
110
        return $this->invoke('before');
111
    }
112
113
    public function beforeSendResponse(): bool
114
    {
115
        return $this->invoke('after');
116
    }
117
118
    /**
119
     * Sends events to all the middlewares. Aborts if one fails with falsy return value.
120
     *
121
     * @param string $event
122
     *
123
     * @return bool
124
     */
125
    protected function invoke(string $event): bool
126
    {
127
        $args = [$this->di('request'), $this->di('response')];
128
129
        foreach ($this->middlewares as $middleware) {
130
            if (\is_string($middleware)) {
131
                $middleware = $this->di($middleware);
132
            }
133
134
            if (!\method_exists($middleware, $event)) {
135
                continue;
136
            }
137
138
            if (!$middleware->$event(...$args)) {
139
                return false;
140
            }
141
        }
142
143
        return true;
144
    }
145
}
146