Completed
Push — master ( b99a49...895f8e )
by Jitendra
07:41 queued 03:12
created

BaseMiddleware::getRouteNameUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace PhalconExt\Http;
4
5
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...
6
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...
7
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...
8
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...
9
use PhalconExt\Di\ProvidesDi;
10
11
/**
12
 * A handy base for middlewares.
13
 *
14
 * @author  Jitendra Adhikari <[email protected]>
15
 * @license MIT
16
 *
17
 * @link    https://github.com/adhocore/phalcon-ext
18
 */
19
abstract class BaseMiddleware implements MiddlewareInterface
20
{
21
    use ProvidesDi;
22
23
    /** @var array */
24
    protected $config;
25
26
    /** @var string */
27
    protected $configKey;
28
29
    public function __construct()
30
    {
31
        $this->config = $this->di('config')->toArray()[$this->configKey];
32
    }
33
34
    /**
35
     * Sets itself to be triggered on `beforeExecuteRoute` (or `before` in micro) events.
36
     *
37
     * @return void
38
     */
39
    public function boot()
40
    {
41
        if ($this->isMicro()) {
42
            $this->di('application')->before($this);
43
44
            return;
45
        }
46
47
        $evm = $this->di('eventsManager');
48
49
        $evm->attach('dispatch:beforeExecuteRoute', $this);
50
51
        $this->di('dispatcher')->setEventsManager($evm);
52
    }
53
54
    /**
55
     * Common handler for both micro and mvc app.
56
     *
57
     * @return bool
58
     */
59
    abstract protected function handle(): bool;
60
61
    /**
62
     * Handler for mvc app.
63
     *
64
     * @return bool
65
     */
66
    public function beforeExecuteRoute(): bool
67
    {
68
        return $this->handle();
69
    }
70
71
    /**
72
     * Handler for micro app.
73
     *
74
     * @param MicroApplication $app
75
     *
76
     * @return bool
77
     */
78
    public function call(MicroApplication $app): bool
79
    {
80
        return $this->handle();
81
    }
82
83
    /**
84
     * Disable view if possible.
85
     *
86
     * @return void
87
     */
88
    protected function disableView()
89
    {
90
        if ($this->di('view') instanceof View) {
91
            $this->di('view')->disable();
92
        }
93
    }
94
95
    /**
96
     * Abort with failure response.
97
     *
98
     * @param int $status
99
     *
100
     * @return bool
101
     */
102
    protected function abort(int $status): bool
103
    {
104
        $this->di('response')->setContent('')->setStatusCode($status)->send();
105
106
        return false;
107
    }
108
109
    /**
110
     * Checks if current app is micro.
111
     *
112
     * @return bool
113
     */
114
    protected function isMicro(): bool
115
    {
116
        static $isMicro = null;
117
118
        if (null !== $isMicro) {
119
            return $isMicro;
120
        }
121
122
        if (!$this->di()->has('application')) {
123
            return $isMicro = false;
124
        }
125
126
        return $isMicro = $this->di('application') instanceof MicroApplication;
127
    }
128
129
    /**
130
     * Get routeName and Url tuple.
131
     *
132
     * @return array [name, 'uri']
133
     */
134
    protected function getRouteNameUri(): array
135
    {
136
        $router = $this->di('router');
137
        $route  = $router->getMatchedRoute();
138
        $name   = $route ? $route->getName() : null;
139
140
        return [$name, $router->getRewriteUri()];
141
    }
142
}
143