Passed
Pull Request — master (#24)
by Raed
04:43
created

CallbacksHandler::handleCallbacks()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7.7656

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 5
nop 2
dl 0
loc 22
ccs 9
cts 12
cp 0.75
crap 7.7656
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace LaraCrafts\GeoRoutes;
4
5
use BadMethodCallException;
6
use InvalidArgumentException;
7
8
trait CallbacksHandler
9
{
10
    /**
11
     * Dynamically call the underlying route.
12
     *
13
     * @param string $method
14
     * @param array $arguments
15
     *
16
     * @return mixed
17
     */
18 70
    public function __call(string $method, array $arguments)
19
    {
20 70
        $this->handleCallbacks($method, $arguments);
21 70
    }
22
23
    /**
24
     * Dynamically handle callbacks
25
     *
26
     * @param string $method
27
     * @param array $arguments
28
     *
29
     * @return void
30
     *
31
     * @throws \InvalidArgumentException
32
     * @throws \BadMethodCallException
33
     */
34 70
    protected function handleCallbacks(string $method, array $arguments)
35
    {
36 70
        if (isset($this->route) && method_exists($this->route, $method)) {
37
            return $this->route->$method(...$arguments);
38
        }
39
40 70
        if (CallbacksRegistrar::has($method)) {
41 40
            return $this->constraint
42 40
                        ->bind($method, $arguments);
43
        }
44
45 30
        if ($method == 'or') {
46 30
            if (count($arguments) == 0 || !is_callable($arguments[0])) {
47
                throw new InvalidArgumentException("The 'or' callback expects one parameter of type callable.");
48
            }
49
50 30
            $callback = new GeoCallback('or', $arguments[0]);
51
52 30
            return $this->constraint->setCallback($callback);
53
        }
54
55
        throw new BadMethodCallException("Undefined method '$method'");
56
    }
57
}
58