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.
Completed
Push — master ( d8af4f...3cf3d3 )
by Lester
01:16
created

Router::do()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 3
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
     * @param Missive $missive
32
     */
33
    public function __construct(Missive $missive)
34
    {
35
        $this->missive = $missive;
36
37
        $this->builder = new RegexBuilder([
38
            RegexBuilder::REGEX_MODIFIER => 'i'
39
        ]);
40
    }
41
42
    /**
43
     * @param string $route
44
     * @param string|callable $action
45
     * @return Router
46
     */
47
    public function register(string $route, $action): self
48
    {
49
        $regex = $this->builder->getRegex($route);
50
        $callable = $this->getCallable($action);
51
        $this->routes[$regex] = $callable;
52
53
        return $this;
54
    }
55
56
    /**
57
     * Sets the SMS property of Missive before
58
     * executing the contents of them sms
59
     * @param SMSAbstract $sms
60
     * @return mixed
61
     */
62
    public function process(SMSAbstract $sms)
63
    {
64
        $this->missive->setSMS($sms);
65
66
        return $this->execute($this->missive->getSMS()->getMessage());
67
    }
68
69
    /**
70
     * @param string $path
71
     * @return mixed
72
     */
73
    public function execute(string $path)
74
    {
75
        $ordered_routes = array_reverse($this->routes, true);
76
        foreach ($ordered_routes as $regex => $action) {
77
            if ($this->builder->matches($regex, $path)) {
78
                $values = $this->builder->getValues($regex, $path);
79
                $data = $this->do($path, $values);
0 ignored issues
show
Bug introduced by
The call to do() misses a required argument $values.

This check looks for function calls that miss required arguments.

Loading history...
80
                if ($data === false) {
81
                    continue;
82
                }
83
84
                return $data;
85
            }
86
        }
87
88
        return false;
89
    }
90
91
    protected function do($action, $path, $values)
92
    {
93
        $data = false;
94
95
        DB::beginTransaction();
96
        try {
97
            $data = $action($path, $values);
98
        }
99
        catch (Exception $e) {
100
            DB::rollBack();
101
        }
102
        DB::commit();
103
104
        return $data;
105
    }
106
107
    /**
108
     * Make an action for an invokable controller.
109
     *
110
     * @param string $action
111
     * @return string
112
     * @throws UnexpectedValueException
113
     */
114
    protected function makeInvokableAction($action)
115
    {
116
        if (! method_exists($action, '__invoke')) {
117
            throw new UnexpectedValueException(sprintf(
118
                'Invalid hears action: [%s]', $action
119
            ));
120
        }
121
122
        return $action.'@__invoke';
123
    }
124
125
    /**
126
     * @param $callback
127
     * @return array|string|Closure
128
     * @throws UnexpectedValueException
129
     * @throws NotFoundExceptionInterface
130
     */
131
    protected function getCallable($callback)
132
    {
133
        if ($callback instanceof Closure) {
134
            return $callback;
135
        }
136
137
        if (\is_array($callback)) {
138
            return $callback;
139
        }
140
141
        if (\is_object($callback)) {
142
            return $callback;
143
        }
144
145
        if (strpos($callback, '@') === false) {
146
            $callback = $this->makeInvokableAction($callback);
147
        }
148
149
        list($class, $method) = explode('@', $callback);
150
151
        $command = $this->container ? $this->container->get($class) : new $class($this);
152
153
        return [$command, $method];
154
    }
155
156
    /**
157
     * @param ContainerInterface $container
158
     */
159
    public function setContainer(ContainerInterface $container)
160
    {
161
        $this->container = $container;
162
    }
163
}
164