Middlewares::beforeHandleRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the PHALCON-EXT package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace PhalconExt\Http;
13
14
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...
15
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...
16
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...
17
use PhalconExt\Di\ProvidesDi;
18
19
/**
20
 * A manager for middlewares.
21
 *
22
 * @author  Jitendra Adhikari <[email protected]>
23
 * @license MIT
24
 *
25
 * @link    https://github.com/adhocore/phalcon-ext
26
 */
27
class Middlewares
28
{
29
    use ProvidesDi;
30
31
    protected $middlewares = [];
32
33
    public function __construct(array $middlewares)
34
    {
35
        $this->middlewares = $middlewares;
36
    }
37
38
    /**
39
     * Wraps app with middlewares and runs the app.
40
     *
41
     * @param Injectable $app The app instance: micro or mvc
42
     *
43
     * @return mixed
44
     */
45
    public function wrap(Injectable $app)
46
    {
47
        if ($app instanceof MicroApplication) {
48
            return $this->handleMicro($app);
49
        }
50
51
        if (!$app instanceof Application) {
52
            throw new \InvalidArgumentException('The app instance is not one of micro or mvc');
53
        }
54
55
        $this->di()->setShared('application', $app);
56
57
        return $this->handleMvc($app);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->handleMvc($app) targeting PhalconExt\Http\Middlewares::handleMvc() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
    }
59
60
    /**
61
     * Setup before/after handers for micro app and run the app.
62
     *
63
     * @param Injectable $app
64
     *
65
     * @return mixed
66
     */
67
    protected function handleMicro(Injectable $app)
68
    {
69
        $app
70
            ->before(function () {
71
                return $this->relay('before');
72
            })
73
            ->after(function () {
74
                return $this->relay('after');
75
            });
76
77
        return $this->handleApp($app);
78
    }
79
80
    /**
81
     * Setup event handlers for mvc app and run the app.
82
     *
83
     * @param Injectable $app
84
     *
85
     * @return void
86
     */
87
    protected function handleMvc(Injectable $app)
88
    {
89
        $evm = $this->di('eventsManager');
90
91
        $evm->attach('application', $this);
92
        $evm->attach('dispatch', $this);
93
        $app->setEventsManager($evm);
94
95
        $this->di('dispatcher')->setEventsManager($evm);
96
97
        return $this->handleApp($app);
98
    }
99
100
    /**
101
     * Handles http requests to the app.
102
     *
103
     * @param Injectable $app
104
     *
105
     * @return mixed Whatever is given by $app->handle().
106
     */
107
    protected function handleApp(Injectable $app)
108
    {
109
        $return = $app->handle();
110
111
        $response = $this->di('response');
112
113
        if (!$response->isSent()) {
114
            $response->send();
115
        }
116
117
        return $return;
118
    }
119
120
    public function beforeHandleRequest(): bool
121
    {
122
        return $this->relay('before');
123
    }
124
125
    public function beforeSendResponse(): bool
126
    {
127
        return $this->relay('after');
128
    }
129
130
    /**
131
     * Sends events to all the middlewares. Aborts if one fails with falsy return value.
132
     *
133
     * @param string $event
134
     *
135
     * @return bool
136
     */
137
    protected function relay(string $event): bool
138
    {
139
        foreach ($this->middlewares as $middleware) {
140
            if (!$this->call($event, $middleware)) {
141
                return false;
142
            }
143
        }
144
145
        return true;
146
    }
147
148
    /**
149
     * Call event method on middleware.
150
     *
151
     * @param string $event
152
     * @param mixed  $middleware
153
     *
154
     * @return bool
155
     */
156
    protected function call(string $event, $middleware): bool
157
    {
158
        if (\is_string($middleware)) {
159
            $middleware = $this->di($middleware);
160
        }
161
162
        if (!\method_exists($middleware, $event)) {
163
            return true;
164
        }
165
166
        return $middleware->$event($this->di('request'), $this->di('response'));
167
    }
168
}
169