Completed
Push — master ( 91ba10...42ada5 )
by Joschi
02:37
created

ObjectUrl::setDraft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object\Domain
8
 * @author      Joschi Kuphal <[email protected]> / @jkphl
9
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
10
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
11
 */
12
13
/***********************************************************************************
14
 *  The MIT License (MIT)
15
 *
16
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
17
 *
18
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
19
 *  this software and associated documentation files (the "Software"), to deal in
20
 *  the Software without restriction, including without limitation the rights to
21
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
22
 *  the Software, and to permit persons to whom the Software is furnished to do so,
23
 *  subject to the following conditions:
24
 *
25
 *  The above copyright notice and this permission notice shall be included in all
26
 *  copies or substantial portions of the Software.
27
 *
28
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
30
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
31
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
32
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
 ***********************************************************************************/
35
36
namespace Apparat\Object\Domain\Model\Path;
37
38
use Apparat\Kernel\Ports\Kernel;
39
use Apparat\Object\Domain\Model\Object\Id;
40
use Apparat\Object\Domain\Model\Object\Revision;
41
use Apparat\Object\Domain\Model\Object\Type;
42
43
/**
44
 * Object URL
45
 *
46
 * @package Apparat\Object\Domain\Model
47
 */
48
class ObjectUrl extends Url implements PathInterface
49
{
50
    /**
51
     * Object path
52
     *
53
     * @var LocalPath
54
     */
55
    protected $localPath = null;
56
57
    /*******************************************************************************
58
     * PUBLIC METHODS
59
     *******************************************************************************/
60
61
    /**
62
     * Object URL constructor
63
     *
64
     * @param string $url Object URL
65
     * @param boolean $remote Accept remote URL (less strict date component checking)
66
     * @throws InvalidArgumentException If remote URLs are not allowed and a remote URL is given
67
     * @throws InvalidArgumentException If the path component is empty
68
     */
69 37
    public function __construct($url, $remote = false)
70
    {
71 37
        parent::__construct($url);
72
73
        // If it's an invalid remote object URL
74 36
        if ($this->isAbsolute() && !$remote) {
75 1
            throw new InvalidArgumentException(
76 1
                sprintf('Unallowed remote object URL "%s"', $url),
77 1
                InvalidArgumentException::UNALLOWED_REMOTE_OBJECT_URL
78
            );
79
        }
80
81
        // If the path component is empty
82 35
        if (empty($this->urlParts['path'])) {
83 1
            throw new InvalidArgumentException(
84 1
                'Invalid object URL path (empty)',
85 1
                InvalidArgumentException::INVALID_OBJECT_URL_PATH
86
            );
87
        }
88
89
        // Instantiate the local path component
90 34
        $this->localPath = new LocalPath(
91 34
            $this->urlParts['path'],
92 34
            $remote ? true : null,
93 34
            $this->urlParts['path']
94
        );
95
96
        // Normalize the path prefix
97 33
        if (!strlen($this->urlParts['path'])) {
98 22
            $this->urlParts['path'] = null;
99
        }
100 33
    }
101
102
    /**
103
     * Set the object's creation date
104
     *
105
     * @param \DateTimeImmutable $creationDate
106
     * @return PathInterface|ObjectUrl New object path
107
     */
108 1
    public function setCreationDate(\DateTimeImmutable $creationDate)
109
    {
110 1
        $this->localPath = $this->localPath->setCreationDate($creationDate);
111 1
        return $this;
112
    }
113
114
    /**
115
     * Set the object type
116
     *
117
     * @param Type $type Object type
118
     * @return PathInterface|ObjectUrl New object URL
119
     */
120 1
    public function setType(Type $type)
121
    {
122 1
        $this->localPath = $this->localPath->setType($type);
123 1
        return $this;
124
    }
125
126
    /**
127
     * Set the object ID
128
     *
129
     * @param Id $uid Object ID
130
     * @return PathInterface|ObjectUrl New object URL
131
     */
132 1
    public function setId(Id $uid)
133
    {
134 1
        $this->localPath = $this->localPath->setId($uid);
135 1
        return $this;
136
    }
137
138
    /**
139
     * Set the object revision
140
     *
141
     * @param Revision $revision Object revision
142
     * @return PathInterface|ObjectUrl New object URL
143
     */
144 1
    public function setRevision(Revision $revision)
145
    {
146 1
        $this->localPath = $this->localPath->setRevision($revision);
147 1
        return $this;
148
    }
149
150
    /**
151
     * Test if this URL matches all available parts of a given URL
152
     *
153
     * @param Url $url Comparison URL
154
     * @return bool This URL matches all available parts of the given URL
155
     */
156 7
    public function matches(Url $url)
157
    {
158
        // If the standard URL components don't match
159 7
        if (!parent::matches($url)) {
160 5
            return false;
161
        }
162
163
        // Extended tests if it's an object URL
164 4
        if ($url instanceof self) {
165
            // Test the object creation date
166 1
            if ($this->getCreationDate() != $url->getCreationDate()) {
167 1
                return false;
168
            }
169
170
            // Test the object ID
171 1
            if ($this->getId()->serialize() !== $url->getId()->serialize()) {
172 1
                return false;
173
            }
174
175
            // Test the object type
176 1
            if ($this->getType()->serialize() !== $url->getType()->serialize()) {
177 1
                return false;
178
            }
179
180
            // Test the object revision
181 1
            if ($this->getRevision()->serialize() !== $url->getRevision()->serialize()) {
182 1
                return false;
183
            }
184
        }
185
186 4
        return true;
187
    }
188
189
    /**
190
     * Return the object's creation date
191
     *
192
     * @return \DateTimeImmutable Object creation date
193
     */
194 10
    public function getCreationDate()
195
    {
196 10
        return $this->localPath->getCreationDate();
197
    }
198
199
    /**
200
     * Return the object ID
201
     *
202
     * @return Id Object ID
203
     */
204 10
    public function getId()
205
    {
206 10
        return $this->localPath->getId();
207
    }
208
209
    /**
210
     * Return the object type
211
     *
212
     * @return Type Object type
213
     */
214 10
    public function getType()
215
    {
216 10
        return $this->localPath->getType();
217
    }
218
219
    /**
220
     * Return the object revision
221
     *
222
     * @return Revision Object revision
223
     */
224 10
    public function getRevision()
225
    {
226 10
        return $this->localPath->getRevision();
227
    }
228
229
    /**
230
     * Return the local object path
231
     *
232
     * @return PathInterface|LocalPath Local object path
233
     */
234 1
    public function getLocalPath()
235
    {
236 1
        return $this->localPath;
237
    }
238
239
    /**
240
     * Return the object draft mode
241
     *
242
     * @return boolean Object draft mode
243
     */
244
    public function isDraft()
245
    {
246
        return $this->localPath->isDraft();
247
    }
248
249
    /**
250
     * Set the object draft mode
251
     *
252
     * @param boolean $draft Object draft mode
253
     * @return PathInterface|LocalPath New object path
254
     */
255
    public function setDraft($draft)
256
    {
257
        $this->localPath = $this->localPath->setDraft($draft);
258
        return $this;
259
    }
260
261
    /**
262
     * Return the repository URL part of this object URL
263
     *
264
     * @return string Repository URL
265
     * @see https://github.com/apparat/apparat/blob/master/doc/URL-DESIGN.md#repository-url
266
     */
267 11
    public function getRepositoryUrl()
268
    {
269
        // If the object URL is absolute and local: Extract the repository URL
270 11
        if ($this->isAbsoluteLocal()) {
271 1
            $baseUrl = Kernel::create(Url::class, [getenv('APPARAT_BASE_URL')]);
272 1
            return substr($this->getPath(), strlen($baseUrl->getPath()));
273
274
            // Else: If it's a relative URL: Extract the repository URL
275 10
        } elseif (!$this->isAbsolute()) {
276 7
            return $this->getPath();
277
        }
278
279
        // Else: It must be a remote repository
280
        $override = [
281 3
            'object' => '',
282
            'query' => '',
283
            'fragment' => '',
284
        ];
285 3
        return $this->getUrlInternal($override);
286
    }
287
288
    /*******************************************************************************
289
     * PRIVATE METHODS
290
     *******************************************************************************/
291
292
    /**
293
     * Return the a complete serialized object URL
294
     *
295
     * @param array $override Override components
296
     * @return string Serialized URL
297
     */
298 16
    protected function getUrlInternal(array &$override = [])
299
    {
300 16
        parent::getUrlInternal($override);
301
302
        // Prepare the local object path
303 16
        $override['object'] = isset($override['object']) ? $override['object'] : strval($this->localPath);
304
305 16
        return "{$override['scheme']}{$override['user']}{$override['pass']}{$override['host']}{$override['port']}".
306 16
        "{$override['path']}{$override['object']}{$override['query']}{$override['fragment']}";
307
    }
308
}
309