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

Resolver::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Bavix\Router;
4
5
class Resolver implements GroupResolution
6
{
7
8
    /**
9
     * @var callable
10
     */
11
    protected $pusher;
12
13
    /**
14
     * Resolver constructor.
15
     *
16
     * @param callable $pusher
17
     */
18
    public function __construct(callable $pusher)
19
    {
20
        $this->pusher = $pusher;
21
    }
22
23
    /**
24
     * @param Pattern $pattern
25
     *
26
     * @return Pattern
27
     */
28
    protected function callback(Pattern $pattern): Pattern
29
    {
30
        return \call_user_func($this->pusher, $pattern);
31
    }
32
33
    /**
34
     * @param array       $methods
35
     * @param string      $path
36
     * @param null|string $name
37
     *
38
     * @return Pattern
39
     */
40
    public function methods(array $methods, string $path, ?string $name): Pattern
41
    {
42
        return $this->callback(new Pattern($path, $name))->methods($methods);
43
    }
44
45
    /**
46
     * GET|POST|PUT|PATCH|HEAD|OPTIONS|DELETE
47
     *
48
     * @param string      $path
49
     * @param null|string $name
50
     *
51
     * @return Pattern
52
     */
53
    public function any(string $path, ?string $name = null): Pattern
54
    {
55
        return $this->callback(new Pattern($path, $name))->any();
56
    }
57
58
    /**
59
     * @param string      $path
60
     * @param null|string $name
61
     *
62
     * @return Pattern
63
     */
64
    public function get(string $path, ?string $name = null): Pattern
65
    {
66
        return $this->callback(new Pattern($path, $name))->get();
67
    }
68
69
    /**
70
     * @param string      $path
71
     * @param null|string $name
72
     *
73
     * @return Pattern
74
     */
75
    public function post(string $path, ?string $name = null): Pattern
76
    {
77
        return $this->callback(new Pattern($path, $name))->post();
78
    }
79
80
    /**
81
     * @param string      $path
82
     * @param null|string $name
83
     *
84
     * @return Pattern
85
     */
86
    public function put(string $path, ?string $name = null): Pattern
87
    {
88
        return $this->callback(new Pattern($path, $name))->put();
89
    }
90
91
    /**
92
     * @param string      $path
93
     * @param null|string $name
94
     *
95
     * @return Pattern
96
     */
97
    public function patch(string $path, ?string $name = null): Pattern
98
    {
99
        return $this->callback(new Pattern($path, $name))->patch();
100
    }
101
102
    /**
103
     * @param string      $path
104
     * @param null|string $name
105
     *
106
     * @return Pattern
107
     */
108
    public function delete(string $path, ?string $name = null): Pattern
109
    {
110
        return $this->callback(new Pattern($path, $name))->delete();
111
    }
112
113
    /**
114
     * entityName -> /users
115
     *
116
     *  GET         users.index     /users
117
     *  GET         users.create    /users/create
118
     *  POST        users.store     /users
119
     *  GET         users.show      /users/{id}
120
     *  GET         users.edit      /users/{id}/edit
121
     *  PUT/PATCH   users.update    /users/{id}/edit
122
     *  DELETE      users.destroy   /users/{id}
123
     *
124
     * @param string      $entityName
125
     * @param null|string $name
126
     * @param null|string $id
127
     */
128
    public function resource(string $entityName, ?string $name = null, ?string $id = null): void
129
    {
130
        $entityName = \rtrim($entityName, '/');
131
        $name = $name ?: \ltrim($entityName, '/');
132
        $id = $id ?: $name;
133
134
        $this->get($entityName, $name . '.index');
135
        $this->get($entityName . '/create', $name . '.create');
136
137
        $this->post($entityName, $name . '.store');
138
139
        $this->get($entityName . '/<' . $id . '>', $name . '.show');
140
        $this->get($entityName . '/<' . $id . '>/edit', $name . '.edit');
141
        $this->methods(
142
            ['PUT', 'PATCH'],
143
            $entityName . '/<' . $id . '>/edit',
144
            $name . '.update'
145
        );
146
147
        $this->delete($entityName . '/<' . $id . '>', $name . '.destroy');
148
    }
149
150
}
151