Passed
Push — master ( a4ddd3...82bd79 )
by Alex
06:44
created

InvalidRouteErrorHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 4
eloc 11
c 1
b 1
f 0
dl 0
loc 53
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setNoProcessorFoundErrorHandler() 0 7 1
A noProcessorFoundErrorHandler() 0 3 1
A getNoProcessorErrorHandler() 0 10 2
1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router;
4
5
/**
6
 * Trait InvalidRouteErrorHandler
7
 *
8
 * @package Router
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2019/08/15)
11
 * @copyright Copyright (c) 2019, aeon.org
12
 */
13
14
/**
15
 * Error handler for unexisting route
16
 *
17
 * @author gdever
18
 */
19
trait InvalidRouteErrorHandler
20
{
21
22
    /**
23
     * Method wich handles invalid route error
24
     *
25
     * @var ?callable
26
     */
27
    private $invalidRouteErrorHandler = null;
28
29
    /**
30
     * Method processes no processor found error
31
     *
32
     * @param string $route
33
     *            route
34
     */
35
    public function noProcessorFoundErrorHandler(string $route): void
36
    {
37
        throw (new \Exception('The processor was not found for the route ' . $route . ' in ' . $this->getAllRoutesTrace(), -1));
0 ignored issues
show
Bug introduced by
It seems like getAllRoutesTrace() 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

37
        throw (new \Exception('The processor was not found for the route ' . $route . ' in ' . $this->/** @scrutinizer ignore-call */ getAllRoutesTrace(), -1));
Loading history...
38
    }
39
40
    /**
41
     * Method sets InvalidRouteErrorHandler function
42
     *
43
     * @param callable $function
44
     *            error handler
45
     *            
46
     * @return ?callable old error handler
47
     */
48
    public function setNoProcessorFoundErrorHandler(callable $function): ?callable
49
    {
50
        $oldErrorHandler = $this->invalidRouteErrorHandler;
51
52
        $this->invalidRouteErrorHandler = $function;
53
54
        return $oldErrorHandler;
55
    }
56
57
    /**
58
     * Method returns error handler
59
     *
60
     * @return callable
61
     */
62
    public function getNoProcessorErrorHandler(): callable
63
    {
64
        if ($this->invalidRouteErrorHandler === null) {
65
            $this->invalidRouteErrorHandler = [
66
                $this,
67
                'noProcessorFoundErrorHandler'
68
            ];
69
        }
70
71
        return $this->invalidRouteErrorHandler;
72
    }
73
}
74