Completed
Pull Request — master (#69)
by Woody
02:53
created

Directory::withoutPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Equip;
4
5
use Equip\Action;
6
use Equip\Exception\DirectoryException;
7
use Equip\Structure\Dictionary;
8
9
class Directory extends Dictionary
10
{
11
    const ANY = '*';
12
    const GET = 'GET';
13
    const POST = 'POST';
14
    const PUT = 'PUT';
15
    const PATCH = 'PATCH';
16
    const HEAD = 'HEAD';
17
    const DELETE = 'DELETE';
18
    const OPTIONS = 'OPTIONS';
19
20
    /**
21
     * @var string
22
     */
23
    private $prefix = '';
24
25
    /**
26
     * Set the directory path prefix.
27
     *
28
     * @param string $prefix
29
     *
30
     * @return static
31
     */
32 2
    public function withPrefix($prefix)
33
    {
34 2
        $copy = clone $this;
35 2
        $copy->prefix = '/' . trim($prefix, '/');
36
37 2
        return $copy;
38
    }
39
40
    /**
41
     * Remove the directory path prefix.
42
     *
43
     * @return static
44
     */
45 1
    public function withoutPrefix()
46
    {
47 1
        $copy = clone $this;
48 1
        $copy->prefix = '';
49
50 1
        return $copy;
51
    }
52
53
    /**
54
     * Add the prefix to a path.
55
     *
56
     * @param string $path
57
     *
58
     * @return string
59
     */
60 4
    public function prefix($path)
61
    {
62 4
        return $this->prefix . $path;
63
    }
64
65
    /**
66
     * @param string $path
67
     * @param string|Action $domainOrAction
68
     *
69
     * @return static
70
     */
71 1
    public function any($path, $domainOrAction)
72
    {
73 1
        return $this->action(self::ANY, $path, $domainOrAction);
74
    }
75
76
    /**
77
     * @param string $path
78
     * @param string|Action $domainOrAction
79
     *
80
     * @return static
81
     */
82 4
    public function get($path, $domainOrAction)
83
    {
84 4
        return $this->action(self::GET, $path, $domainOrAction);
85
    }
86
87
    /**
88
     * @param string $path
89
     * @param string|Action $domainOrAction
90
     *
91
     * @return static
92
     */
93 1
    public function post($path, $domainOrAction)
94
    {
95 1
        return $this->action(self::POST, $path, $domainOrAction);
96
    }
97
98
    /**
99
     * @param string $path
100
     * @param string|Action $domainOrAction
101
     *
102
     * @return static
103
     */
104 1
    public function put($path, $domainOrAction)
105
    {
106 1
        return $this->action(self::PUT, $path, $domainOrAction);
107
    }
108
109
    /**
110
     * @param string $path
111
     * @param string|Action $domainOrAction
112
     *
113
     * @return static
114
     */
115 1
    public function patch($path, $domainOrAction)
116
    {
117 1
        return $this->action(self::PATCH, $path, $domainOrAction);
118
    }
119
120
    /**
121
     * @param string $path
122
     * @param string|Action $domainOrAction
123
     *
124
     * @return static
125
     */
126 1
    public function head($path, $domainOrAction)
127
    {
128 1
        return $this->action(self::HEAD, $path, $domainOrAction);
129
    }
130
131
    /**
132
     * @param string $path
133
     * @param string|Action $domainOrAction
134
     *
135
     * @return static
136
     */
137 1
    public function delete($path, $domainOrAction)
138
    {
139 1
        return $this->action(self::DELETE, $path, $domainOrAction);
140
    }
141
142
    /**
143
     * @param string $path
144
     * @param string|Action $domainOrAction
145
     *
146
     * @return static
147
     */
148 1
    public function options($path, $domainOrAction)
149
    {
150 1
        return $this->action(self::OPTIONS, $path, $domainOrAction);
151
    }
152
153
    /**
154
     * @param string $method
155
     * @param string $path
156
     * @param string|Action $domainOrAction
157
     *
158
     * @return static
159
     */
160 13
    public function action($method, $path, $domainOrAction)
161
    {
162 13
        if ($domainOrAction instanceof Action) {
163 12
            $action = $domainOrAction;
164 12
        } else {
165 1
            $action = new Action($domainOrAction);
166
        }
167
168 13
        return $this->withValue(sprintf('%s %s', $method, $path), $action);
169
    }
170
171
    /**
172
     * @inheritDoc
173
     *
174
     * @throws DirectoryException If a value is not an Action instance
175
     */
176 17
    protected function assertValid(array $data)
177
    {
178 17
        parent::assertValid($data);
179
180 17
        foreach ($data as $value) {
181 14
            if (!is_object($value) || !$value instanceof Action) {
182 1
                throw DirectoryException::invalidEntry($value);
183
            }
184 17
        }
185 17
    }
186
}
187