BaseMiddleware   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 12
Bugs 4 Features 3
Metric Value
wmc 11
eloc 24
c 12
b 4
f 3
dl 0
loc 81
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isMicro() 0 3 1
A abort() 0 19 4
A getRouteNameUri() 0 7 2
A __construct() 0 3 1
A stop() 0 8 3
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\Http\Response;
15
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...
16
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...
17
use PhalconExt\Di\ProvidesDi;
18
19
/**
20
 * A handy base for middlewares.
21
 *
22
 * @author  Jitendra Adhikari <[email protected]>
23
 * @license MIT
24
 *
25
 * @link    https://github.com/adhocore/phalcon-ext
26
 */
27
abstract class BaseMiddleware
28
{
29
    use ProvidesDi;
30
31
    /** @var array */
32
    protected $config = [];
33
34
    /** @var string */
35
    protected $configKey;
36
37
    public function __construct()
38
    {
39
        $this->config = $this->di('config')->path($this->configKey)->toArray();
40
    }
41
42
    /**
43
     * Stop the app execution and disable view if possible.
44
     */
45
    protected function stop()
46
    {
47
        if ($this->di('view') instanceof View) {
48
            $this->di('view')->disable();
49
        }
50
51
        if ($this->isMicro()) {
52
            $this->di('application')->stop();
53
        }
54
    }
55
56
    /**
57
     * Abort with response.
58
     *
59
     * @param int   $status
60
     * @param mixed $content If not string, will be json encoded
61
     * @param array $headers
62
     *
63
     * @return bool Always false
64
     */
65
    protected function abort(int $status, $content = null, array $headers = []): bool
66
    {
67
        $this->stop();
68
69
        $response = $this->di('response');
70
71
        foreach ($headers as $key => $value) {
72
            $response->setHeader($key, $value);
73
        }
74
75
        if ($content && !\is_scalar($content)) {
76
            $response->setJsonContent($content);
77
        } else {
78
            $response->setContent($content);
79
        }
80
81
        $response->setStatusCode($status)->send();
82
83
        return false;
84
    }
85
86
    /**
87
     * Checks if current app is micro.
88
     *
89
     * @return bool
90
     */
91
    protected function isMicro(): bool
92
    {
93
        return $this->di('application') instanceof MicroApplication;
94
    }
95
96
    /**
97
     * Get routeName and Uri tuple.
98
     *
99
     * @return array ['name', 'uri']
100
     */
101
    protected function getRouteNameUri(): array
102
    {
103
        $router = $this->di('router');
104
        $route  = $router->getMatchedRoute();
105
        $name   = $route ? $route->getName() : null;
106
107
        return [$name, $router->getRewriteUri()];
108
    }
109
}
110