ArrayRouteProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace MatthiasMullie\Api\Routes\Providers;
4
5
use MatthiasMullie\Api\Routes\Route;
6
use Generator;
7
8
/**
9
 * @author Matthias Mullie <[email protected]>
10
 * @copyright Copyright (c) 2016, Matthias Mullie. All rights reserved
11
 * @license LICENSE MIT
12
 */
13
class ArrayRouteProvider implements RouteProviderInterface
14
{
15
    /**
16
     * @var array[]
17
     */
18
    protected $routes = [];
19
20
    /**
21
     * @param array[] $data
22
     */
23
    public function __construct(array $data)
24
    {
25
        $this->routes = $data;
26
    }
27
28
    /**
29
     * @return Generator
30
     */
31
    public function getRoutes(): Generator
32
    {
33
        foreach ($this->routes as $data) {
34
            yield new Route(
35
                $this->getMethods($data),
36
                $this->getPath($data),
37
                $this->getHandler($data)
38
            );
39
        }
40
    }
41
42
    /**
43
     * @param array $data
44
     *
45
     * @return string[]
46
     *
47
     * @throws Exception
48
     */
49 View Code Duplication
    protected function getMethods(array $data): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        if (!isset($data['method'])) {
52
            $serialized = serialize($data);
53
            throw new Exception("Missing method. (input: $serialized)");
54
        }
55
56
        return (array) $data['method'];
57
    }
58
59
    /**
60
     * @param array $data
61
     *
62
     * @return string
63
     *
64
     * @throws Exception
65
     */
66 View Code Duplication
    protected function getPath(array $data): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        if (!isset($data['path'])) {
69
            $serialized = serialize($data);
70
            throw new Exception("Missing path. (input: $serialized)");
71
        }
72
73
        return $data['path'];
74
    }
75
76
    /**
77
     * @param array $data
78
     *
79
     * @return callable
80
     *
81
     * @throws Exception
82
     */
83
    protected function getHandler(array $data): callable
84
    {
85
        if (!isset($data['handler'])) {
86
            $serialized = serialize($data);
87
            throw new Exception("Missing handler. (input: $serialized)");
88
        }
89
90
        $handler = $data['handler'];
91
92
        if (is_callable($handler)) {
93
            return $handler;
94
        }
95
96
        if (is_string($handler) && method_exists($handler, '__invoke')) {
97
            return new $handler();
98
        }
99
100
        $serialized = serialize($data);
101
        throw new Exception("Handler must be callable. (input: $serialized)");
102
    }
103
}
104