Completed
Push — master ( a783ca...18c4d4 )
by Joschi
03:44
created

ObjectProxy::setLongitude()   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
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 1
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 privacy
415
     *
416
     * @return string Privacy
417
     */
418
    public function getPrivacy()
419
    {
420
        return $this->object()->getPrivacy();
421
    }
422
423
    /**
424
     * Set the privacy
425
     *
426
     * @param string $privacy Privacy
427
     * @return ObjectInterface Self reference
428
     */
429
    public function setPrivacy($privacy)
430
    {
431
        return $this->object()->setPrivacy($privacy);
432
    }
433
434
    /**
435
     * Return all object categories
436
     *
437
     * @return array Object categories
438
     */
439
    public function getCategories()
440
    {
441
        return $this->object()->getCategories();
442
    }
443
444
    /**
445
     * Set the categories
446
     *
447
     * @param array $categories Categories
448
     * @return ObjectInterface Self reference
449
     */
450
    public function setCategories(array $categories)
451
    {
452
        return $this->object()->setCategories($categories);
453
    }
454
455
    /**
456
     * Get a domain property value
457
     *
458
     * Multi-level properties might be traversed by property name paths separated with colons (":").
459
     *
460
     * @param string $property Property name
461
     * @return mixed Property value
462
     */
463
    public function getDomainProperty($property)
464
    {
465
        return $this->object()->getDomainProperty($property);
466
    }
467
468
    /**
469
     * Set a domain property value
470
     *
471
     * @param string $property Property name
472
     * @param mixed $value Property value
473
     * @return ObjectInterface Self reference
474
     */
475
    public function setDomainProperty($property, $value)
476
    {
477
        return $this->object()->setDomainProperty($property, $value);
478
    }
479
480
    /**
481
     * Get a processing instruction
482
     *
483
     * @param string $procInst Processing instruction name
484
     * @return mixed Processing instruction
485
     */
486
    public function getProcessingInstruction($procInst)
487
    {
488
        return $this->object()->getProcessingInstruction($procInst);
489
    }
490
491
    /**
492
     * Set a processing instruction
493
     *
494
     * @param string $procInst Processing instruction name
495
     * @param mixed $value Processing instruction
496
     * @return ObjectInterface Self reference
497
     */
498
    public function setProcessingInstruction($procInst, $value)
499
    {
500
        return $this->object()->setProcessingInstruction($procInst, $value);
501
    }
502
503
    /**
504
     * Return the absolute object URL
505
     *
506
     * @return string
507
     */
508
    public function getAbsoluteUrl()
509
    {
510
        return strval($this->url);
511
    }
512
513
    /**
514
     * Generic caller
515
     *
516
     * @param string $name Method name
517
     * @param array $arguments Method arguments
518
     */
519
    public function __call($name, $arguments)
520
    {
521
        $object = $this->object();
522
        if (is_callable(array($object, $name))) {
523
            return $object->$name(...$arguments);
524
        }
525
526
        throw new InvalidArgumentException(
527
            sprintf('Invalid object proxy method "%s"', $name),
528
            InvalidArgumentException::INVALID_OBJECT_PROXY_METHOD
529
        );
530
    }
531
532
    /**
533
     * Use a specific object revision
534
     *
535
     * @param Revision $revision Revision to be used
536
     * @return ObjectInterface Object
537
     */
538
    public function useRevision(Revision $revision)
539
    {
540
        return $this->object()->useRevision($revision);
541
    }
542
543
    /**
544
     * Persist the current object revision
545
     *
546
     * @return ObjectInterface Object
547
     */
548
    public function persist()
549
    {
550
        return $this->object()->persist();
551
    }
552
553
    /**
554
     * Return whether the object is in published state
555
     *
556
     * @return boolean Published state
557
     */
558
    public function isPublished()
559
    {
560
        return $this->object()->isPublished();
561
    }
562
563
    /**
564
     * Publish the current object revision
565
     *
566
     * @return ObjectInterface Object
567
     */
568
    public function publish()
569
    {
570
        return $this->object()->publish();
571
    }
572
573
    /**
574
     * Add an object relation
575
     *
576
     * @param string|RelationInterface $relation Serialized or instantiated object relation
577
     * @param string|null $relationType Relation type
578
     * @return ObjectInterface
579
     */
580
    public function addRelation($relation, $relationType = null)
581
    {
582
        return $this->object()->addRelation($relation, $relationType);
583
    }
584
585
    /**
586
     * Delete an object relation
587
     *
588
     * @param RelationInterface $relation Object relation
589
     * @return ObjectInterface
590
     */
591
    public function deleteRelation(RelationInterface $relation)
592
    {
593
        return $this->object()->deleteRelation($relation);
594
    }
595
596
    /**
597
     * Get all relations (optional: Of a particular type)
598
     *
599
     * @param string|null $relationType Optional: Relation type
600
     * @return array Object relations
601
     */
602
    public function getRelations($relationType = null)
603
    {
604
        return $this->object()->getRelations($relationType);
605
    }
606
607
    /**
608
     * Find and return particular relations
609
     *
610
     * @param array $criteria Relation criteria
611
     * @return RelationInterface[] Relations
612
     */
613
    public function findRelations(array $criteria)
614
    {
615
        return $this->object()->findRelations($criteria);
616
    }
617
}
618