MiddlewareTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 12
eloc 24
c 2
b 1
f 0
dl 0
loc 73
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A middleware() 0 5 1
A relay() 0 9 3
A bindEvents() 0 8 1
A afterExecuteRoute() 0 3 1
A beforeExecuteRoute() 0 3 1
A middlewares() 0 9 2
A call() 0 11 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\Cli;
13
14
use Phalcon\Cli\Console;
0 ignored issues
show
Bug introduced by
The type Phalcon\Cli\Console 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...
Bug introduced by
This use statement conflicts with another class in this namespace, PhalconExt\Cli\Console. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
16
trait MiddlewareTrait
17
{
18
    protected $middlewares = [
19
        Middleware\Factory::class,
20
    ];
21
22
    protected function bindEvents(Console $console)
23
    {
24
        $evm = $this->di('eventsManager');
0 ignored issues
show
Bug introduced by
It seems like di() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        /** @scrutinizer ignore-call */ 
25
        $evm = $this->di('eventsManager');
Loading history...
25
26
        $evm->attach('dispatch', $console);
27
        $console->setEventsManager($evm);
28
29
        $this->di('dispatcher')->setEventsManager($evm);
30
    }
31
32
    public function middleware(string $class): self
33
    {
34
        $this->middlewares[] = $class;
35
36
        return $this;
37
    }
38
39
    /**
40
     * Bulk setter/getter.
41
     *
42
     * @param array $middlewares Class names.
43
     *
44
     * @return array|self
45
     */
46
    public function middlewares(array $middlewares = [])
47
    {
48
        if (\func_num_args() > 0) {
49
            $this->middlewares = \array_unique(\array_merge($this->middlewares, $middlewares));
50
51
            return $this;
52
        }
53
54
        return $this->middlewares;
55
    }
56
57
    public function beforeExecuteRoute(): bool
58
    {
59
        return $this->relay('before');
60
    }
61
62
    public function afterExecuteRoute(): bool
63
    {
64
        return $this->relay('after');
65
    }
66
67
    protected function relay(string $event): bool
68
    {
69
        foreach ($this->middlewares as $middleware) {
70
            if (!$this->call($event, $middleware)) {
71
                return false;
72
            }
73
        }
74
75
        return true;
76
    }
77
78
    protected function call(string $event, $middleware): bool
79
    {
80
        if (\is_string($middleware)) {
81
            $middleware = $this->di($middleware);
82
        }
83
84
        if (!\method_exists($middleware, $event)) {
85
            return true;
86
        }
87
88
        return $middleware->$event($this->di('console'));
89
    }
90
}
91