GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Router::getCallable()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
cc 6
nc 7
nop 1
1
<?php
2
3
namespace LBHurtado\Missive\Routing;
4
5
use DB;
6
use Closure;
7
use Exception;
8
use Opis\Pattern\RegexBuilder;
9
use LBHurtado\Missive\Missive;
10
use Psr\Container\ContainerInterface;
11
use LBHurtado\Missive\Classes\SMSAbstract;
12
13
class Router
14
{
15
    /** @var \Opis\Pattern\RegexBuilder */
16
    protected $builder;
17
18
    /** @var array */
19
    protected $routes = [];
20
21
    /** @var \LBHurtado\Missive\Missive */
22
    public $missive;
23
24
    /** @var ContainerInterface */
25
    protected $container;
26
27
    /**
28
     * Router constructor.
29
     * Capture the Missive instance from the container.
30
     * Instantiate RegexBuilder that ignores casing.
31
     *
32
     * @param Missive $missive
33
     */
34
    public function __construct(Missive $missive)
35
    {
36
        $this->missive = $missive;
37
38
        $this->builder = new RegexBuilder([
39
            RegexBuilder::REGEX_MODIFIER => 'i'
40
        ]);
41
    }
42
43
    /**
44
     * @param string $route
45
     * @param string|callable $action
46
     * @return Router
47
     */
48
    public function register(string $route, $action): self
49
    {
50
        $regex = $this->builder->getRegex($route);
51
        $callable = $this->getCallable($action);
52
        $this->routes[$regex] = $callable;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Sets the SMS property of Missive before
59
     * executing the contents of them sms
60
     *
61
     * @param SMSAbstract $sms
62
     * @return bool
63
     * @throws Exception
64
     */
65
    public function process(SMSAbstract $sms)
66
    {
67
        $this->missive->setSMS($sms);
68
69
        return $this->execute($this->missive->getSMS()->getMessage());
70
    }
71
72
73
    /**
74
     * Return true if handled.
75
     * Otherwise, send to next route.
76
     *
77
     * @param string $path
78
     * @return bool
79
     * @throws Exception
80
     */
81
    public function execute(string $path)
82
    {
83
        $ordered_routes = array_reverse($this->routes, true);
84
        foreach ($ordered_routes as $regex => $action) {
85
            if ($this->builder->matches($regex, $path)) {
86
                $values = $this->builder->getValues($regex, $path);
87
                $data = $this->do($action, $path, $values);
88
                if ($data === false) {
89
                    continue;
90
                }
91
92
                return $data;
93
            }
94
        }
95
96
        return false;
97
    }
98
99
    /**
100
     * Make sure to rollback the database if an error occurs.
101
     *
102
     * @param $action
103
     * @param $path
104
     * @param $values
105
     * @return bool
106
     * @throws Exception
107
     */
108
    protected function do($action, $path, $values)
109
    {
110
        $data = false;
111
112
        DB::beginTransaction();
113
        try {
114
            $data = $action($path, $values);
115
        }
116
        catch (Exception $e) {
117
            DB::rollBack();
118
119
            throw $e;
120
        }
121
        DB::commit();
122
123
        return $data;
124
    }
125
126
    /**
127
     * Make an action for an invokable controller.
128
     *
129
     * @param string $action
130
     * @return string
131
     * @throws UnexpectedValueException
132
     */
133
    protected function makeInvokableAction($action)
134
    {
135
        if (! method_exists($action, '__invoke')) {
136
            throw new UnexpectedValueException(sprintf(
137
                'Invalid hears action: [%s]', $action
138
            ));
139
        }
140
141
        return $action.'@__invoke';
142
    }
143
144
    /**
145
     * @param $callback
146
     * @return array|string|Closure
147
     * @throws UnexpectedValueException
148
     * @throws NotFoundExceptionInterface
149
     */
150
    protected function getCallable($callback)
151
    {
152
        if ($callback instanceof Closure) {
153
            return $callback;
154
        }
155
156
        if (\is_array($callback)) {
157
            return $callback;
158
        }
159
160
        if (\is_object($callback)) {
161
            return $callback;
162
        }
163
164
        if (strpos($callback, '@') === false) {
165
            $callback = $this->makeInvokableAction($callback);
166
        }
167
168
        list($class, $method) = explode('@', $callback);
169
170
        $command = $this->container ? $this->container->get($class) : new $class($this);
171
172
        return [$command, $method];
173
    }
174
175
    /**
176
     * @param ContainerInterface $container
177
     */
178
    public function setContainer(ContainerInterface $container)
179
    {
180
        $this->container = $container;
181
    }
182
}
183