Completed
Push — master ( 863650...abf17e )
by Joschi
03:17
created

ObjectProxy::getPayload()   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 0
dl 0
loc 4
rs 10
ccs 0
cts 4
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\Path\ApparatUrl;
41
use Apparat\Object\Domain\Model\Path\PathInterface;
42
use Apparat\Object\Domain\Model\Relation\RelationInterface;
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
    public function __construct(ApparatUrl $url)
76
    {
77
        $this->url = $url;
78
    }
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 latitude
176
     *
177
     * @return float Latitude
178
     */
179
    public function getLatitude()
180
    {
181
        return $this->object()->getLatitude();
182
    }
183
184
    /**
185
     * Set the latitude
186
     *
187
     * @param float $latitude Latitude
188
     * @return ObjectInterface Self reference
189
     */
190
    public function setLatitude($latitude)
191
    {
192
        return $this->object()->setLatitude($latitude);
193
    }
194
195
    /**
196
     * Return the longitude
197
     *
198
     * @return float Longitude
199
     */
200
    public function getLongitude()
201
    {
202
        return $this->object()->getLongitude();
203
    }
204
205
    /**
206
     * Set the longitude
207
     *
208
     * @param float $longitude Longitude
209
     * @return ObjectInterface Self reference
210
     */
211
    public function setLongitude($longitude)
212
    {
213
        return $this->object()->setLongitude($longitude);
214
    }
215
216
    /**
217
     * Return the elevation
218
     *
219
     * @return float Elevation
220
     */
221
    public function getElevation()
222
    {
223
        return $this->object()->getElevation();
224
    }
225
226
    /**
227
     * Set the elevation
228
     *
229
     * @param float $elevation
230
     * @return ObjectInterface Self reference
231
     */
232
    public function setElevation($elevation)
233
    {
234
        return $this->object()->setElevation($elevation);
235
    }
236
237
238
    /**
239
     * Return the object draft mode
240
     *
241
     * @return boolean Object draft mode
242
     */
243
    public function isDraft()
244
    {
245
        return $this->object()->isDraft();
246
    }
247
248
    /**
249
     * Return whether the object is in dirty state
250
     *
251
     * @return boolean Dirty state
252
     */
253
    public function isDirty()
254
    {
255
        return $this->object()->isDirty();
256
    }
257
258
    /**
259
     * Return whether the object is in mutated state
260
     *
261
     * @return boolean Mutated state
262
     */
263
    public function isMutated()
264
    {
265
        return $this->object()->isMutated();
266
    }
267
268
    /**
269
     * Return the creation date & time
270
     *
271
     * @return \DateTimeImmutable Creation date & time
272
     */
273
    public function getCreated()
274
    {
275
        return $this->object()->getCreated();
276
    }
277
278
    /**
279
     * Return the publication date & time
280
     *
281
     * @return \DateTimeImmutable Publication date & time
282
     */
283
    public function getPublished()
284
    {
285
        return $this->object()->getPublished();
286
    }
287
288
    /**
289
     * Return the object hash
290
     *
291
     * @return string Object hash
292
     */
293
    public function getHash()
294
    {
295
        return $this->object()->getHash();
296
    }
297
298
    /**
299
     * Return the object title
300
     *
301
     * @return string Object title
302
     */
303
    public function getTitle()
304
    {
305
        return $this->object()->getTitle();
306
    }
307
308
    /**
309
     * Set the title
310
     *
311
     * @param string $title Title
312
     * @return ObjectInterface Self reference
313
     */
314
    public function setTitle($title)
315
    {
316
        return $this->object()->setTitle($title);
317
    }
318
319
    /**
320
     * Return the object slug
321
     *
322
     * @return string Object slug
323
     */
324
    public function getSlug()
325
    {
326
        return $this->object()->getSlug();
327
    }
328
329
    /**
330
     * Set the slug
331
     *
332
     * @param string $slug Slug
333
     * @return ObjectInterface Self reference
334
     */
335
    public function setSlug($slug)
336
    {
337
        return $this->object()->setSlug($slug);
338
    }
339
340
341
    /**
342
     * Return the object description
343
     *
344
     * @return string Object description
345
     */
346
    public function getDescription()
347
    {
348
        return $this->object()->getDescription();
349
    }
350
351
    /**
352
     * Set the description
353
     *
354
     * @param string $description Description
355
     * @return ObjectInterface Self reference
356
     */
357
    public function setDescription($description)
358
    {
359
        return $this->object()->setDescription($description);
360
    }
361
362
    /**
363
     * Return the object abstract
364
     *
365
     * @return string Object abstract
366
     */
367
    public function getAbstract()
368
    {
369
        return $this->object()->getAbstract();
370
    }
371
372
    /**
373
     * Set the abstract
374
     *
375
     * @param string $abstract Abstract
376
     * @return ObjectInterface Self reference
377
     */
378
    public function setAbstract($abstract)
379
    {
380
        return $this->object()->setAbstract($abstract);
381
    }
382
383
    /**
384
     * Return all object keywords
385
     *
386
     * @return array Object keywords
387
     */
388
    public function getKeywords()
389
    {
390
        return $this->object()->getKeywords();
391
    }
392
393
    /**
394
     * Set the keywords
395
     *
396
     * @param array $keywords Keywords
397
     * @return ObjectInterface Self reference
398
     */
399
    public function setKeywords(array $keywords)
400
    {
401
        return $this->object()->setKeywords($keywords);
402
    }
403
404
    /**
405
     * Return the license
406
     *
407
     * @return string License
408
     */
409
    public function getLicense()
410
    {
411
        return $this->object()->getLicense();
412
    }
413
414
    /**
415
     * Set the license
416
     *
417
     * @param string $license License
418
     * @return ObjectInterface Self reference
419
     */
420
    public function setLicense($license)
421
    {
422
        return $this->object()->setLicense($license);
423
    }
424
425
    /**
426
     * Return the privacy
427
     *
428
     * @return string Privacy
429
     */
430
    public function getPrivacy()
431
    {
432
        return $this->object()->getPrivacy();
433
    }
434
435
    /**
436
     * Set the privacy
437
     *
438
     * @param string $privacy Privacy
439
     * @return ObjectInterface Self reference
440
     */
441
    public function setPrivacy($privacy)
442
    {
443
        return $this->object()->setPrivacy($privacy);
444
    }
445
446
    /**
447
     * Return all object categories
448
     *
449
     * @return array Object categories
450
     */
451
    public function getCategories()
452
    {
453
        return $this->object()->getCategories();
454
    }
455
456
    /**
457
     * Set the categories
458
     *
459
     * @param array $categories Categories
460
     * @return ObjectInterface Self reference
461
     */
462
    public function setCategories(array $categories)
463
    {
464
        return $this->object()->setCategories($categories);
465
    }
466
467
    /**
468
     * Get a domain property value
469
     *
470
     * Multi-level properties might be traversed by property name paths separated with colons (":").
471
     *
472
     * @param string $property Property name
473
     * @return mixed Property value
474
     */
475
    public function getDomainProperty($property)
476
    {
477
        return $this->object()->getDomainProperty($property);
478
    }
479
480
    /**
481
     * Set a domain property value
482
     *
483
     * @param string $property Property name
484
     * @param mixed $value Property value
485
     * @return ObjectInterface Self reference
486
     */
487
    public function setDomainProperty($property, $value)
488
    {
489
        return $this->object()->setDomainProperty($property, $value);
490
    }
491
492
    /**
493
     * Get a processing instruction
494
     *
495
     * @param string $procInst Processing instruction name
496
     * @return mixed Processing instruction
497
     */
498
    public function getProcessingInstruction($procInst)
499
    {
500
        return $this->object()->getProcessingInstruction($procInst);
501
    }
502
503
    /**
504
     * Set a processing instruction
505
     *
506
     * @param string $procInst Processing instruction name
507
     * @param mixed $value Processing instruction
508
     * @return ObjectInterface Self reference
509
     */
510
    public function setProcessingInstruction($procInst, $value)
511
    {
512
        return $this->object()->setProcessingInstruction($procInst, $value);
513
    }
514
515
    /**
516
     * Return the absolute object URL
517
     *
518
     * @return string
519
     */
520
    public function getAbsoluteUrl()
521
    {
522
        return strval($this->url);
523
    }
524
525
    /**
526
     * Generic caller
527
     *
528
     * @param string $name Method name
529
     * @param array $arguments Method arguments
530
     */
531
    public function __call($name, $arguments)
532
    {
533
        $object = $this->object();
534
        if (is_callable(array($object, $name))) {
535
            return $object->$name(...$arguments);
536
        }
537
538
        throw new InvalidArgumentException(
539
            sprintf('Invalid object proxy method "%s"', $name),
540
            InvalidArgumentException::INVALID_OBJECT_PROXY_METHOD
541
        );
542
    }
543
544
    /**
545
     * Use a specific object revision
546
     *
547
     * @param Revision $revision Revision to be used
548
     * @return ObjectInterface Object
549
     */
550
    public function useRevision(Revision $revision)
551
    {
552
        return $this->object()->useRevision($revision);
553
    }
554
555
    /**
556
     * Persist the current object revision
557
     *
558
     * @return ObjectInterface Object
559
     */
560
    public function persist()
561
    {
562
        return $this->object()->persist();
563
    }
564
565
    /**
566
     * Return whether the object is in published state
567
     *
568
     * @return boolean Published state
569
     */
570
    public function isPublished()
571
    {
572
        return $this->object()->isPublished();
573
    }
574
575
    /**
576
     * Publish the current object revision
577
     *
578
     * @return ObjectInterface Object
579
     */
580
    public function publish()
581
    {
582
        return $this->object()->publish();
583
    }
584
585
    /**
586
     * Add an object relation
587
     *
588
     * @param string $relationType Relation type
589
     * @param string|RelationInterface $relation Serialized or instantiated object relation
590
     * @return ObjectInterface
591
     */
592
    public function addRelation($relationType, $relation)
593
    {
594
        return $this->object()->addRelation($relationType, $relation);
595
    }
596
597
    /**
598
     * Get all relations (optional: Of a particular type)
599
     *
600
     * @param string|null $relationType Optional: Relation type
601
     * @return array Object relations
602
     */
603
    public function getRelations($relationType = null)
604
    {
605
        return $this->object()->getRelations($relationType);
606
    }
607
}
608