Completed
Push — master ( 4dc655...679eeb )
by Jitendra
12s queued 10s
created

BaseMiddleware::stop()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 4
nop 0
1
<?php
2
3
namespace PhalconExt\Http;
4
5
use Phalcon\Http\Response;
6
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...
7
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...
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
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
     * Stop the app execution and disable view if possible.
36
     */
37
    protected function stop()
38
    {
39
        if ($this->di('view') instanceof View) {
40
            $this->di('view')->disable();
41
        }
42
43
        if ($this->isMicro()) {
44
            $this->di('application')->stop();
45
        }
46
    }
47
48
    /**
49
     * Abort with failure response.
50
     *
51
     * @param int    $status
52
     * @param string $body
53
     *
54
     * @return bool
55
     */
56
    protected function abort(int $status, string $body = null): bool
57
    {
58
        $this->stop();
59
60
        $this->di('response')->setStatusCode($status)->setContent($body)->send();
61
62
        return false;
63
    }
64
65
    /**
66
     * Checks if current app is micro.
67
     *
68
     * @return bool
69
     */
70
    protected function isMicro(): bool
71
    {
72
        return $this->di('application') instanceof MicroApplication;
73
    }
74
75
    /**
76
     * Get routeName and Uri tuple.
77
     *
78
     * @return array ['name', 'uri']
79
     */
80
    protected function getRouteNameUri(): array
81
    {
82
        $router = $this->di('router');
83
        $route  = $router->getMatchedRoute();
84
        $name   = $route ? $route->getName() : null;
85
86
        return [$name, $router->getRewriteUri()];
87
    }
88
}
89