Completed
Push — master ( cd6aa3...db07f0 )
by Jitendra
13s
created

BaseMiddleware::callAfter()   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 0
1
<?php
2
3
namespace PhalconExt\Http;
4
5
use Phalcon\Http\Request;
1 ignored issue
show
Bug introduced by
The type Phalcon\Http\Request 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\Response;
1 ignored issue
show
Bug introduced by
The type Phalcon\Http\Response 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...
7
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...
8
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...
9
use Phalcon\Mvc\Micro\MiddlewareInterface;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\Micro\MiddlewareInterface 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\View;
0 ignored issues
show
Bug introduced by
The type Phalcon\Mvc\View 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 handy base for middlewares.
15
 *
16
 * @author  Jitendra Adhikari <[email protected]>
17
 * @license MIT
18
 *
19
 * @link    https://github.com/adhocore/phalcon-ext
20
 */
21
abstract class BaseMiddleware implements MiddlewareInterface
22
{
23
    use ProvidesDi;
24
25
    /** @var array */
26
    protected $config = [];
27
28
    /** @var string */
29
    protected $configKey;
30
31
    public function __construct()
32
    {
33
        $this->config = $this->di('config')->toArray()[$this->configKey];
34
    }
35
36
    /**
37
     * Sets itself to be triggered on before &/or after route execution events.
38
     *
39
     * @return void
40
     */
41
    public function boot()
42
    {
43
        if ($this->isMicro()) {
44
            $this->di('application')->before($this);
45
            $this->di('application')->after([$this, 'callAfter']);
46
47
            return;
48
        }
49
50
        $evm = $this->di('eventsManager');
51
52
        $evm->attach('dispatch:beforeExecuteRoute', $this);
53
        $evm->attach('dispatch:afterExecuteRoute', $this);
54
55
        $this->di('dispatcher')->setEventsManager($evm);
56
    }
57
58
    /**
59
     * Before route handler for both micro and mvc app.
60
     *
61
     * @param Request  $request
62
     * @param Response $response
63
     *
64
     * @return bool
65
     */
66
    abstract public function before(Request $request, Response $response): bool;
67
68
    /**
69
     * After route handler for both micro and mvc app.
70
     *
71
     * @param Request  $request
72
     * @param Response $response
73
     *
74
     * @return bool
75
     */
76
    public function after(Request $request, Response $response): bool
77
    {
78
        // Implementing class may extend this and do the needful.
79
        return true;
80
    }
81
82
    /**
83
     * Before route executed in mvc.
84
     *
85
     * @return bool
86
     */
87
    public function beforeExecuteRoute(): bool
88
    {
89
        return $this->before($this->di('request'), $this->di('response'));
90
    }
91
92
    /**
93
     * After route executed in mvc.
94
     *
95
     *@return bool
96
     */
97
    public function afterExecuteRoute(): bool
98
    {
99
        return $this->after($this->di('request'), $this->di('response'));
100
    }
101
102
    /**
103
     * Before handler for micro app.
104
     *
105
     * @param MicroApplication $app
106
     *
107
     * @return bool
108
     */
109
    public function call(MicroApplication $app): bool
110
    {
111
        return $this->before($this->di('request'), $this->di('response'));
112
    }
113
114
    /**
115
     * After handler for micro app.
116
     *
117
     * @return bool
118
     */
119
    public function callAfter(): bool
120
    {
121
        return $this->after($this->di('request'), $this->di('response'));
122
    }
123
124
    /**
125
     * Disable view if possible.
126
     *
127
     * @return void
128
     */
129
    protected function disableView()
130
    {
131
        if ($this->di('view') instanceof View) {
132
            $this->di('view')->disable();
133
        }
134
    }
135
136
    /**
137
     * Abort with failure response.
138
     *
139
     * @param int $status
140
     *
141
     * @return bool
142
     */
143
    protected function abort(int $status): bool
144
    {
145
        $this->di('response')->setContent('')->setStatusCode($status)->send();
146
147
        return false;
148
    }
149
150
    /**
151
     * Checks if current app is micro.
152
     *
153
     * @return bool
154
     */
155
    protected function isMicro(): bool
156
    {
157
        static $isMicro = null;
158
159
        if (null !== $isMicro) {
160
            return $isMicro;
161
        }
162
163
        if (!$this->di()->has('application')) {
164
            return $isMicro = false;
165
        }
166
167
        return $isMicro = $this->di('application') instanceof MicroApplication;
168
    }
169
170
    /**
171
     * Get routeName and Url tuple.
172
     *
173
     * @return array [name, 'uri']
174
     */
175
    protected function getRouteNameUri(): array
176
    {
177
        $router = $this->di('router');
178
        $route  = $router->getMatchedRoute();
179
        $name   = $route ? $route->getName() : null;
180
181
        return [$name, $router->getRewriteUri()];
182
    }
183
}
184