Completed
Push — master ( e75790...24066c )
by Joschi
02:49
created

ObjectProxy::setSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Object;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Domain\Model\Author\AuthorInterface;
41
use Apparat\Object\Domain\Model\Path\ApparatUrl;
42
use Apparat\Object\Domain\Model\Path\PathInterface;
43
use Apparat\Object\Domain\Repository\Service;
44
45
/**
46
 * Object proxy (lazy loading)
47
 *
48
 * @package Apparat\Object
49
 * @subpackage Apparat\Object\Domain
50
 */
51
class ObjectProxy implements ObjectInterface
52
{
53
    /**
54
     * Apparat object URL
55
     *
56
     * @var ApparatUrl
57
     */
58
    protected $url = null;
59
    /**
60
     * Object
61
     *
62
     * @var ObjectInterface
63
     */
64
    protected $object = null;
65
66
    /*******************************************************************************
67
     * PUBLIC METHODS
68
     *******************************************************************************/
69
70
    /**
71
     * Object proxy constructor
72
     *
73
     * @param ApparatUrl $url Apparat object URL
74
     */
75 15
    public function __construct(ApparatUrl $url)
76
    {
77 15
        $this->url = $url;
78 15
    }
79
80
    /**
81
     * Return the object repository path
82
     *
83
     * @return PathInterface Object repository path
84
     */
85
    public function getRepositoryPath()
86
    {
87
        // If the object has already been instantiated
88
        if ($this->object instanceof ObjectInterface) {
89
            return $this->object->getRepositoryPath();
90
91
            // Else
92
        } else {
93
            return $this->url->getLocalPath();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->url->getLocalPath(); (Apparat\Object\Domain\Model\Path\LocalPath) is incompatible with the return type declared by the interface Apparat\Object\Domain\Mo...face::getRepositoryPath of type Apparat\Object\Domain\Mo...RepositoryPathInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
94
        }
95
    }
96
97
    /**
98
     * Return the object property data
99
     *
100
     * @return array Object property data
101
     */
102
    public function getPropertyData()
103
    {
104
        return $this->object()->getPropertyData();
105
    }
106
107
    /**
108
     * Return the enclosed remote object
109
     *
110
     * @return ObjectInterface Remote object
111
     */
112
    protected function object()
113
    {
114
        // Lazy-load the remote object if necessary
115
        if (!$this->object instanceof ObjectInterface) {
116
            // Instantiate the local object repository, load and return the object
117
            $this->object = Kernel::create(Service::class)->get($this->url)->loadObject($this->url->getLocalPath());
118
        }
119
120
        return $this->object;
121
    }
122
123
    /**
124
     * Return the object payload
125
     *
126
     * @return string Object payload
127
     */
128
    public function getPayload()
129
    {
130
        return $this->object()->getPayload();
131
    }
132
133
    /**
134
     * Set the payload
135
     *
136
     * @param string $payload Payload
137
     * @return ObjectInterface Self reference
138
     */
139
    public function setPayload($payload)
140
    {
141
        return $this->object()->setPayload($payload);
142
    }
143
144
    /**
145
     * Return the object ID
146
     *
147
     * @return Id Object ID
148
     */
149
    public function getId()
150
    {
151
        return $this->object()->getId();
152
    }
153
154
    /**
155
     * Return the object type
156
     *
157
     * @return Type Object type
158
     */
159
    public function getType()
160
    {
161
        return $this->object()->getType();
162
    }
163
164
    /**
165
     * Return the object revision
166
     *
167
     * @return Revision Object revision
168
     */
169
    public function getRevision()
170
    {
171
        return $this->object()->getRevision();
172
    }
173
174
    /**
175
     * Return the object draft mode
176
     *
177
     * @return boolean Object draft mode
178
     */
179
    public function isDraft()
180
    {
181
        return $this->object()->isDraft();
182
    }
183
184
    /**
185
     * Return whether the object is in dirty state
186
     *
187
     * @return boolean Dirty state
188
     */
189
    public function isDirty()
190
    {
191
        return $this->object()->isDirty();
192
    }
193
194
    /**
195
     * Return whether the object is in mutated state
196
     *
197
     * @return boolean Mutated state
198
     */
199
    public function isMutated()
200
    {
201
        return $this->object()->isMutated();
202
    }
203
204
    /**
205
     * Return the creation date & time
206
     *
207
     * @return \DateTimeImmutable Creation date & time
208
     */
209
    public function getCreated()
210
    {
211
        return $this->object()->getCreated();
212
    }
213
214
    /**
215
     * Return the publication date & time
216
     *
217
     * @return \DateTimeImmutable Publication date & time
218
     */
219
    public function getPublished()
220
    {
221
        return $this->object()->getPublished();
222
    }
223
224
    /**
225
     * Return the object hash
226
     *
227
     * @return string Object hash
228
     */
229
    public function getHash()
230
    {
231
        return $this->object()->getHash();
232
    }
233
234
    /**
235
     * Return the object title
236
     *
237
     * @return string Object title
238
     */
239
    public function getTitle()
240
    {
241
        return $this->object()->getTitle();
242
    }
243
244
    /**
245
     * Set the title
246
     *
247
     * @param string $title Title
248
     * @return ObjectInterface Self reference
249
     */
250
    public function setTitle($title)
251
    {
252
        return $this->object()->setTitle($title);
253
    }
254
255
    /**
256
     * Return the object slug
257
     *
258
     * @return string Object slug
259
     */
260
    public function getSlug()
261
    {
262
        return $this->object()->getSlug();
263
    }
264
265
    /**
266
     * Set the slug
267
     *
268
     * @param string $slug Slug
269
     * @return ObjectInterface Self reference
270
     */
271
    public function setSlug($slug)
272
    {
273
        return $this->object()->setSlug($slug);
274
    }
275
276
277
    /**
278
     * Return the object description
279
     *
280
     * @return string Object description
281
     */
282
    public function getDescription()
283
    {
284
        return $this->object()->getDescription();
285
    }
286
287
    /**
288
     * Set the description
289
     *
290
     * @param string $description Description
291
     * @return ObjectInterface Self reference
292
     */
293
    public function setDescription($description)
294
    {
295
        return $this->object()->setDescription($description);
296
    }
297
298
    /**
299
     * Return the object abstract
300
     *
301
     * @return string Object abstract
302
     */
303
    public function getAbstract()
304
    {
305
        return $this->object()->getAbstract();
306
    }
307
308
    /**
309
     * Set the abstract
310
     *
311
     * @param string $abstract Abstract
312
     * @return ObjectInterface Self reference
313
     */
314
    public function setAbstract($abstract)
315
    {
316
        return $this->object()->setAbstract($abstract);
317
    }
318
319
    /**
320
     * Return all object keywords
321
     *
322
     * @return array Object keywords
323
     */
324
    public function getKeywords()
325
    {
326
        return $this->object()->getKeywords();
327
    }
328
329
    /**
330
     * Set the keywords
331
     *
332
     * @param array $keywords Keywords
333
     * @return ObjectInterface Self reference
334
     */
335
    public function setKeywords(array $keywords)
336
    {
337
        return $this->object()->setKeywords($keywords);
338
    }
339
340
    /**
341
     * Return all object categories
342
     *
343
     * @return array Object categories
344
     */
345
    public function getCategories()
346
    {
347
        return $this->object()->getCategories();
348
    }
349
350
    /**
351
     * Set the categories
352
     *
353
     * @param array $categories Categories
354
     * @return ObjectInterface Self reference
355
     */
356
    public function setCategories(array $categories)
357
    {
358
        return $this->object()->setCategories($categories);
359
    }
360
361
    /**
362
     * Return all object authors
363
     *
364
     * @return AuthorInterface[] Authors
365
     */
366
    public function getAuthors()
367
    {
368
        return $this->object()->getAuthors();
369
    }
370
371
    /**
372
     * Add an object author
373
     *
374
     * @param AuthorInterface $author Author
375
     * @return ObjectInterface Self reference
376
     */
377
    public function addAuthor(AuthorInterface $author)
378
    {
379
        return $this->object()->addAuthor($author);
380
    }
381
382
    /**
383
     * Get a domain property value
384
     *
385
     * Multi-level properties might be traversed by property name paths separated with colons (":").
386
     *
387
     * @param string $property Property name
388
     * @return mixed Property value
389
     */
390
    public function getDomainProperty($property)
391
    {
392
        return $this->object()->getDomainProperty($property);
393
    }
394
395
    /**
396
     * Set a domain property value
397
     *
398
     * @param string $property Property name
399
     * @param mixed $value Property value
400
     * @return ObjectInterface Self reference
401
     */
402
    public function setDomainProperty($property, $value)
403
    {
404
        return $this->object()->setDomainProperty($property, $value);
405
    }
406
407
    /**
408
     * Get a processing instruction
409
     *
410
     * @param string $procInst Processing instruction name
411
     * @return mixed Processing instruction
412
     */
413
    public function getProcessingInstruction($procInst)
414
    {
415
        return $this->object()->getProcessingInstruction($procInst);
416
    }
417
418
    /**
419
     * Set a processing instruction
420
     *
421
     * @param string $procInst Processing instruction name
422
     * @param mixed $value Processing instruction
423
     * @return ObjectInterface Self reference
424
     */
425
    public function setProcessingInstruction($procInst, $value)
426
    {
427
        return $this->object()->setProcessingInstruction($procInst, $value);
428
    }
429
430
    /**
431
     * Return the absolute object URL
432
     *
433
     * @return string
434
     */
435 14
    public function getAbsoluteUrl()
436
    {
437 14
        return strval($this->url);
438
    }
439
440
    /**
441
     * Generic caller
442
     *
443
     * @param string $name Method name
444
     * @param array $arguments Method arguments
445
     */
446
    public function __call($name, $arguments)
447
    {
448
        $object = $this->object();
449
        if (is_callable(array($object, $name))) {
450
            return $object->$name(...$arguments);
451
        }
452
453
        throw new InvalidArgumentException(
454
            sprintf('Invalid object proxy method "%s"', $name),
455
            InvalidArgumentException::INVALID_OBJECT_PROXY_METHOD
456
        );
457
    }
458
459
    /**
460
     * Use a specific object revision
461
     *
462
     * @param Revision $revision Revision to be used
463
     * @return ObjectInterface Object
464
     */
465
    public function useRevision(Revision $revision)
466
    {
467
        return $this->object()->useRevision($revision);
468
    }
469
470
    /**
471
     * Persist the current object revision
472
     *
473
     * @return ObjectInterface Object
474
     */
475
    public function persist()
476
    {
477
        return $this->object()->persist();
478
    }
479
480
    /**
481
     * Return whether the object is in published state
482
     *
483
     * @return boolean Published state
484
     */
485
    public function isPublished()
486
    {
487
        return $this->object()->isPublished();
488
    }
489
490
    /**
491
     * Publish the current object revision
492
     *
493
     * @return ObjectInterface Object
494
     */
495
    public function publish()
496
    {
497
        return $this->object()->publish();
498
    }
499
500
501
}
502