Completed
Push — master ( 5874e2...48c27a )
by Joschi
03:10
created

ObjectProxy::getLanguage()   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
ccs 0
cts 4
cp 0
crap 2
rs 10
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\RepositoryPathInterface;
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 RepositoryPathInterface 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
92
        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...
93
    }
94
95
    /**
96
     * Return the object property data
97
     *
98
     * @return array Object property data
99
     */
100
    public function getPropertyData()
101
    {
102
        return $this->object()->getPropertyData();
103
    }
104
105
    /**
106
     * Return the enclosed remote object
107
     *
108
     * @return ObjectInterface Remote object
109
     */
110
    protected function object()
111
    {
112
        // Lazy-load the remote object if necessary
113
        if (!$this->object instanceof ObjectInterface) {
114
            // Instantiate the local object repository, load and return the object
115
            $this->object = Kernel::create(Service::class)->get($this->url)->loadObject($this->url->getLocalPath());
116
        }
117
118
        return $this->object;
119
    }
120
121
    /**
122
     * Return the object payload
123
     *
124
     * @return string Object payload
125
     */
126
    public function getPayload()
127
    {
128
        return $this->object()->getPayload();
129
    }
130
131
    /**
132
     * Set the payload
133
     *
134
     * @param string $payload Payload
135
     * @return ObjectInterface Self reference
136
     */
137
    public function setPayload($payload)
138
    {
139
        return $this->object()->setPayload($payload);
140
    }
141
142
    /**
143
     * Return the object ID
144
     *
145
     * @return Id Object ID
146
     */
147
    public function getId()
148
    {
149
        return $this->object()->getId();
150
    }
151
152
    /**
153
     * Return the object type
154
     *
155
     * @return Type Object type
156
     */
157
    public function getType()
158
    {
159
        return $this->object()->getType();
160
    }
161
162
    /**
163
     * Return the object revision
164
     *
165
     * @return Revision Object revision
166
     */
167
    public function getRevision()
168
    {
169
        return $this->object()->getRevision();
170
    }
171
172
    /**
173
     * Return the latitude
174
     *
175
     * @return float Latitude
176
     */
177
    public function getLatitude()
178
    {
179
        return $this->object()->getLatitude();
180
    }
181
182
    /**
183
     * Set the latitude
184
     *
185
     * @param float $latitude Latitude
186
     * @return ObjectInterface Self reference
187
     */
188
    public function setLatitude($latitude)
189
    {
190
        return $this->object()->setLatitude($latitude);
191
    }
192
193
    /**
194
     * Return the longitude
195
     *
196
     * @return float Longitude
197
     */
198
    public function getLongitude()
199
    {
200
        return $this->object()->getLongitude();
201
    }
202
203
    /**
204
     * Set the longitude
205
     *
206
     * @param float $longitude Longitude
207
     * @return ObjectInterface Self reference
208
     */
209
    public function setLongitude($longitude)
210
    {
211
        return $this->object()->setLongitude($longitude);
212
    }
213
214
    /**
215
     * Return the elevation
216
     *
217
     * @return float Elevation
218
     */
219
    public function getElevation()
220
    {
221
        return $this->object()->getElevation();
222
    }
223
224
    /**
225
     * Set the elevation
226
     *
227
     * @param float $elevation
228
     * @return ObjectInterface Self reference
229
     */
230
    public function setElevation($elevation)
231
    {
232
        return $this->object()->setElevation($elevation);
233
    }
234
235
236
    /**
237
     * Return the object draft mode
238
     *
239
     * @return boolean Object draft mode
240
     */
241
    public function isDraft()
242
    {
243
        return $this->object()->isDraft();
244
    }
245
246
    /**
247
     * Return whether the object is in dirty state
248
     *
249
     * @return boolean Dirty state
250
     */
251
    public function isDirty()
252
    {
253
        return $this->object()->isDirty();
254
    }
255
256
    /**
257
     * Return whether the object is in mutated state
258
     *
259
     * @return boolean Mutated state
260
     */
261
    public function isMutated()
262
    {
263
        return $this->object()->isMutated();
264
    }
265
266
    /**
267
     * Return the creation date & time
268
     *
269
     * @return \DateTimeImmutable Creation date & time
270
     */
271
    public function getCreated()
272
    {
273
        return $this->object()->getCreated();
274
    }
275
276
    /**
277
     * Return the publication date & time
278
     *
279
     * @return \DateTimeImmutable Publication date & time
280
     */
281
    public function getPublished()
282
    {
283
        return $this->object()->getPublished();
284
    }
285
286
    /**
287
     * Return the object title
288
     *
289
     * @return string Object title
290
     */
291
    public function getTitle()
292
    {
293
        return $this->object()->getTitle();
294
    }
295
296
    /**
297
     * Set the title
298
     *
299
     * @param string $title Title
300
     * @return ObjectInterface Self reference
301
     */
302
    public function setTitle($title)
303
    {
304
        return $this->object()->setTitle($title);
305
    }
306
307
    /**
308
     * Return the object slug
309
     *
310
     * @return string Object slug
311
     */
312
    public function getSlug()
313
    {
314
        return $this->object()->getSlug();
315
    }
316
317
    /**
318
     * Set the slug
319
     *
320
     * @param string $slug Slug
321
     * @return ObjectInterface Self reference
322
     */
323
    public function setSlug($slug)
324
    {
325
        return $this->object()->setSlug($slug);
326
    }
327
328
329
    /**
330
     * Return the object description
331
     *
332
     * @return string Object description
333
     */
334
    public function getDescription()
335
    {
336
        return $this->object()->getDescription();
337
    }
338
339
    /**
340
     * Set the description
341
     *
342
     * @param string $description Description
343
     * @return ObjectInterface Self reference
344
     */
345
    public function setDescription($description)
346
    {
347
        return $this->object()->setDescription($description);
348
    }
349
350
    /**
351
     * Return the object abstract
352
     *
353
     * @return string Object abstract
354
     */
355
    public function getAbstract()
356
    {
357
        return $this->object()->getAbstract();
358
    }
359
360
    /**
361
     * Set the abstract
362
     *
363
     * @param string $abstract Abstract
364
     * @return ObjectInterface Self reference
365
     */
366
    public function setAbstract($abstract)
367
    {
368
        return $this->object()->setAbstract($abstract);
369
    }
370
371
    /**
372
     * Return all object keywords
373
     *
374
     * @return array Object keywords
375
     */
376
    public function getKeywords()
377
    {
378
        return $this->object()->getKeywords();
379
    }
380
381
    /**
382
     * Set the keywords
383
     *
384
     * @param array $keywords Keywords
385
     * @return ObjectInterface Self reference
386
     */
387
    public function setKeywords(array $keywords)
388
    {
389
        return $this->object()->setKeywords($keywords);
390
    }
391
392
    /**
393
     * Return the license
394
     *
395
     * @return string License
396
     */
397
    public function getLicense()
398
    {
399
        return $this->object()->getLicense();
400
    }
401
402
    /**
403
     * Set the license
404
     *
405
     * @param string $license License
406
     * @return ObjectInterface Self reference
407
     */
408
    public function setLicense($license)
409
    {
410
        return $this->object()->setLicense($license);
411
    }
412
413
    /**
414
     * Return the language
415
     *
416
     * @return string Language
417
     */
418
    public function getLanguage()
419
    {
420
        return $this->object()->getLanguage();
421
    }
422
423
    /**
424
     * Return the privacy
425
     *
426
     * @return string Privacy
427
     */
428
    public function getPrivacy()
429
    {
430
        return $this->object()->getPrivacy();
431
    }
432
433
    /**
434
     * Set the privacy
435
     *
436
     * @param string $privacy Privacy
437
     * @return ObjectInterface Self reference
438
     */
439
    public function setPrivacy($privacy)
440
    {
441
        return $this->object()->setPrivacy($privacy);
442
    }
443
444
    /**
445
     * Return all object categories
446
     *
447
     * @return array Object categories
448
     */
449
    public function getCategories()
450
    {
451
        return $this->object()->getCategories();
452
    }
453
454
    /**
455
     * Set the categories
456
     *
457
     * @param array $categories Categories
458
     * @return ObjectInterface Self reference
459
     */
460
    public function setCategories(array $categories)
461
    {
462
        return $this->object()->setCategories($categories);
463
    }
464
465
    /**
466
     * Get a domain property value
467
     *
468
     * Multi-level properties might be traversed by property name paths separated with colons (":").
469
     *
470
     * @param string $property Property name
471
     * @return mixed Property value
472
     */
473
    public function getDomainProperty($property)
474
    {
475
        return $this->object()->getDomainProperty($property);
476
    }
477
478
    /**
479
     * Set a domain property value
480
     *
481
     * @param string $property Property name
482
     * @param mixed $value Property value
483
     * @return ObjectInterface Self reference
484
     */
485
    public function setDomainProperty($property, $value)
486
    {
487
        return $this->object()->setDomainProperty($property, $value);
488
    }
489
490
    /**
491
     * Get a processing instruction
492
     *
493
     * @param string $procInst Processing instruction name
494
     * @return mixed Processing instruction
495
     */
496
    public function getProcessingInstruction($procInst)
497
    {
498
        return $this->object()->getProcessingInstruction($procInst);
499
    }
500
501
    /**
502
     * Set a processing instruction
503
     *
504
     * @param string $procInst Processing instruction name
505
     * @param mixed $value Processing instruction
506
     * @return ObjectInterface Self reference
507
     */
508
    public function setProcessingInstruction($procInst, $value)
509
    {
510
        return $this->object()->setProcessingInstruction($procInst, $value);
511
    }
512
513
    /**
514
     * Return the absolute object URL
515
     *
516
     * @return string
517
     */
518
    public function getAbsoluteUrl()
519
    {
520
        return strval($this->url);
521
    }
522
523
    /**
524
     * Generic caller
525
     *
526
     * @param string $name Method name
527
     * @param array $arguments Method arguments
528
     */
529
    public function __call($name, $arguments)
530
    {
531
        $object = $this->object();
532
        if (is_callable(array($object, $name))) {
533
            return $object->$name(...$arguments);
534
        }
535
536
        throw new InvalidArgumentException(
537
            sprintf('Invalid object proxy method "%s"', $name),
538
            InvalidArgumentException::INVALID_OBJECT_PROXY_METHOD
539
        );
540
    }
541
542
    /**
543
     * Use a specific object revision
544
     *
545
     * @param Revision $revision Revision to be used
546
     * @return ObjectInterface Object
547
     */
548
    public function useRevision(Revision $revision)
549
    {
550
        return $this->object()->useRevision($revision);
551
    }
552
553
    /**
554
     * Persist the current object revision
555
     *
556
     * @return ObjectInterface Object
557
     */
558
    public function persist()
559
    {
560
        return $this->object()->persist();
561
    }
562
563
    /**
564
     * Return whether the object is in published state
565
     *
566
     * @return boolean Published state
567
     */
568
    public function isPublished()
569
    {
570
        return $this->object()->isPublished();
571
    }
572
573
    /**
574
     * Publish the current object revision
575
     *
576
     * @return ObjectInterface Object
577
     */
578
    public function publish()
579
    {
580
        return $this->object()->publish();
581
    }
582
583
    /**
584
     * Add an object relation
585
     *
586
     * @param string|RelationInterface $relation Serialized or instantiated object relation
587
     * @param string|null $relationType Relation type
588
     * @return ObjectInterface
589
     */
590
    public function addRelation($relation, $relationType = null)
591
    {
592
        return $this->object()->addRelation($relation, $relationType);
593
    }
594
595
    /**
596
     * Delete an object relation
597
     *
598
     * @param RelationInterface $relation Object relation
599
     * @return ObjectInterface
600
     */
601
    public function deleteRelation(RelationInterface $relation)
602
    {
603
        return $this->object()->deleteRelation($relation);
604
    }
605
606
    /**
607
     * Get all relations (optional: Of a particular type)
608
     *
609
     * @param string|null $relationType Optional: Relation type
610
     * @return array Object relations
611
     */
612
    public function getRelations($relationType = null)
613
    {
614
        return $this->object()->getRelations($relationType);
615
    }
616
617
    /**
618
     * Find and return particular relations
619
     *
620
     * @param array $criteria Relation criteria
621
     * @return RelationInterface[] Relations
622
     */
623
    public function findRelations(array $criteria)
624
    {
625
        return $this->object()->findRelations($criteria);
626
    }
627
}
628