Passed
Pull Request — master (#3)
by Бабичев
01:37
created

Resolver::resource()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 27
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 26
nc 1
nop 3
crap 12
1
<?php
2
3
namespace Bavix\Router;
4
5
class Resolver implements GroupResolution
6
{
7
8
    /**
9
     * @var callable
10
     */
11
    protected $collections;
12
13
    /**
14
     * @var callable
15
     */
16
    protected $patterns;
17
18
    /**
19
     * Resolver constructor.
20
     *
21
     * @param callable $patterns
22
     * @param callable $collections
23
     */
24
    public function __construct(callable $patterns, callable $collections)
25
    {
26
        $this->collections = $collections;
27
        $this->patterns = $patterns;
28
    }
29
30
    /**
31
     * @param ResourceCollection $collection
32
     *
33
     * @return Pattern
34
     */
35
    protected function pushCollection(ResourceCollection $collection): ResourceCollection
36
    {
37
        return \call_user_func($this->collections, $collection);
38
    }
39
40
    /**
41
     * @param Pattern $pattern
42
     *
43
     * @return Pattern
44
     */
45
    protected function pushPattern(Pattern $pattern): Pattern
46
    {
47
        return \call_user_func($this->patterns, $pattern);
48
    }
49
50
    /**
51
     * @param string $path
52
     * @param null|string $name
53
     * @return Pattern
54
     */
55
    protected function pattern(string $path, ?string $name): Pattern
56
    {
57
        return new Pattern($path, $name);
58
    }
59
60
    /**
61
     * @param array       $methods
62
     * @param string      $path
63
     * @param null|string $name
64
     *
65
     * @return Pattern
66
     */
67
    public function methods(array $methods, string $path, ?string $name): Pattern
68
    {
69
        return $this->pushPattern($this->pattern($path, $name))->methods($methods);
70
    }
71
72
    /**
73
     * GET|POST|PUT|PATCH|HEAD|OPTIONS|DELETE
74
     *
75
     * @param string      $path
76
     * @param null|string $name
77
     *
78
     * @return Pattern
79
     */
80
    public function any(string $path, ?string $name = null): Pattern
81
    {
82
        return $this->pushPattern($this->pattern($path, $name))->any();
83
    }
84
85
    /**
86
     * @param string      $path
87
     * @param null|string $name
88
     *
89
     * @return Pattern
90
     */
91
    public function get(string $path, ?string $name = null): Pattern
92
    {
93
        return $this->pushPattern($this->pattern($path, $name))->get();
94
    }
95
96
    /**
97
     * @param string      $path
98
     * @param null|string $name
99
     *
100
     * @return Pattern
101
     */
102
    public function post(string $path, ?string $name = null): Pattern
103
    {
104
        return $this->pushPattern($this->pattern($path, $name))->post();
105
    }
106
107
    /**
108
     * @param string      $path
109
     * @param null|string $name
110
     *
111
     * @return Pattern
112
     */
113
    public function put(string $path, ?string $name = null): Pattern
114
    {
115
        return $this->pushPattern($this->pattern($path, $name))->put();
116
    }
117
118
    /**
119
     * @param string      $path
120
     * @param null|string $name
121
     *
122
     * @return Pattern
123
     */
124
    public function patch(string $path, ?string $name = null): Pattern
125
    {
126
        return $this->pushPattern($this->pattern($path, $name))->patch();
127
    }
128
129
    /**
130
     * @param string      $path
131
     * @param null|string $name
132
     *
133
     * @return Pattern
134
     */
135
    public function delete(string $path, ?string $name = null): Pattern
136
    {
137
        return $this->pushPattern($this->pattern($path, $name))->delete();
138
    }
139
140
    /**
141
     * @param ResourceCollection $collection
142
     * @param string $name
143
     * @param string $type
144
     * @return Pattern
145
     */
146
    protected function route(ResourceCollection $collection, string $name, string $type): Pattern
147
    {
148
        return $collection[$type] = $this->pattern('', $name . '.' . $type);
149
    }
150
151
    /**
152
     * entityName -> /users
153
     *
154
     *  GET         users.index     /users
155
     *  GET         users.create    /users/create
156
     *  POST        users.store     /users
157
     *  GET         users.show      /users/{id}
158
     *  GET         users.edit      /users/{id}/edit
159
     *  PUT/PATCH   users.update    /users/{id}/edit
160
     *  DELETE      users.destroy   /users/{id}
161
     *
162
     * @param string      $entityName
163
     * @param null|string $name
164
     * @param null|string $id
165
     *
166
     * @return ResourceCollection
167
     */
168
    public function resource(string $entityName, ?string $name = null, ?string $id = null): ResourceCollection
169
    {
170
        $entityName = \rtrim($entityName, '/');
171
        $name = $name ?: \ltrim($entityName, '/');
172
        $id = $id ?: $name;
173
174
        $collection = new ResourceCollection();
175
        $this->route($collection, $name, 'index')
176
            ->setPath($entityName)
177
            ->get();
178
179
        $this->route($collection, $name, 'create')
180
            ->setPath($entityName . '/create')
181
            ->get();
182
183
        $this->route($collection, $name, 'store')
184
            ->setPath($entityName)
185
            ->post();
186
187
        $this->route($collection, $name, 'show')
188
            ->setPath($entityName . '/<' . $id . '>')
189
            ->get();
190
191
        $this->route($collection, $name, 'edit')
192
            ->setPath($entityName . '/<' . $id . '>/edit')
193
            ->get();
194
195
        $this->route($collection, $name, 'update')
196
            ->setPath($entityName . '/<' . $id . '>/edit')
197
            ->setMethods(['PUT', 'PATCH']);
198
199
        $this->route($collection, $name, 'destroy')
200
            ->setPath($entityName . '/<' . $id . '>')
201
            ->delete();
202
203
        return $this->pushCollection($collection);
204
    }
205
206
}
207