Completed
Push — master ( 108e84...f077fb )
by Joschi
08:25
created

ObjectProxyTrait::delete()   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

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
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\Traits;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Domain\Model\Object\Id;
41
use Apparat\Object\Domain\Model\Object\InvalidArgumentException;
42
use Apparat\Object\Domain\Model\Object\ObjectInterface;
43
use Apparat\Object\Domain\Model\Object\Revision;
44
use Apparat\Object\Domain\Model\Object\Type;
45
use Apparat\Object\Domain\Model\Relation\RelationInterface;
46
use Apparat\Object\Domain\Model\Uri\ApparatUrl;
47
use Apparat\Object\Domain\Model\Uri\RepositoryLocatorInterface;
48
use Apparat\Object\Domain\Repository\Service;
49
50
/**
51
 * Object proxy (lazy loading)
52
 *
53
 * @package Apparat\Object
54
 * @subpackage Apparat\Object\Domain
55
 */
56
trait ObjectProxyTrait
57
{
58
    /**
59
     * Use traits
60
     */
61
    use IterableProxyTrait;
62
    /**
63
     * Object
64
     *
65
     * @var ObjectInterface
66
     */
67
    protected $object = null;
68
69
    /**
70
     * Return the object repository locator
71
     *
72
     * @return RepositoryLocatorInterface Object repository locator
73
     */
74
    public function getRepositoryLocator()
75
    {
76
        // If the object has already been instantiated
77
        if ($this->object instanceof ObjectInterface) {
78
            return $this->object->getRepositoryLocator();
79
        }
80
81
        return $this->getUrl()->getLocator();
82
    }
83
84
    /**
85
     * Return the URL
86
     *
87
     * @return ApparatUrl URL
88
     */
89
    abstract public function getUrl();
90
91
    /**
92
     * Return the object property data
93
     *
94
     * @param bool $serialize Serialize property objects
95
     * @return array Object property data
96
     */
97
    public function getPropertyData($serialize = true)
98
    {
99
        return $this->object()->getPropertyData($serialize);
100
    }
101
102
    /**
103
     * Return the enclosed remote object
104
     *
105
     * @return ObjectInterface Remote object
106
     */
107 1
    protected function object()
108
    {
109
        // Lazy-load the remote object if necessary
110 1
        if (!$this->object instanceof ObjectInterface) {
111
            // Instantiate the local object repository, load and return the object
112 1
            $this->object =
113 1
                Kernel::create(Service::class)->get($this->getUrl())->loadObject($this->getUrl()->getLocator());
114
        }
115
116 1
        return $this->object;
117
    }
118
119
    /**
120
     * Return the object payload
121
     *
122
     * @return string Object payload
123
     */
124
    public function getPayload()
125
    {
126
        return $this->object()->getPayload();
127
    }
128
129
    /**
130
     * Set the payload
131
     *
132
     * @param string $payload Payload
133
     * @return ObjectInterface Self reference
134
     */
135
    public function setPayload($payload)
136
    {
137
        return $this->object()->setPayload($payload);
138
    }
139
140
    /**
141
     * Return the object ID
142
     *
143
     * @return Id Object ID
144
     */
145
    public function getId()
146
    {
147
        return $this->object()->getId();
148
    }
149
150
    /**
151
     * Return the object type
152
     *
153
     * @return Type Object type
154
     */
155
    public function getObjectType()
156
    {
157
        return $this->object()->getObjectType();
158
    }
159
160
    /**
161
     * Return the object revision
162
     *
163
     * @return Revision Object revision
164
     */
165
    public function getRevision()
166
    {
167
        return $this->object()->getRevision();
168
    }
169
170
    /**
171
     * Return the latitude
172
     *
173
     * @return float Latitude
174
     */
175
    public function getLatitude()
176
    {
177
        return $this->object()->getLatitude();
178
    }
179
180
    /**
181
     * Set the latitude
182
     *
183
     * @param float $latitude Latitude
184
     * @return ObjectInterface Self reference
185
     */
186
    public function setLatitude($latitude)
187
    {
188
        return $this->object()->setLatitude($latitude);
189
    }
190
191
    /**
192
     * Return the longitude
193
     *
194
     * @return float Longitude
195
     */
196
    public function getLongitude()
197
    {
198
        return $this->object()->getLongitude();
199
    }
200
201
    /**
202
     * Set the longitude
203
     *
204
     * @param float $longitude Longitude
205
     * @return ObjectInterface Self reference
206
     */
207
    public function setLongitude($longitude)
208
    {
209
        return $this->object()->setLongitude($longitude);
210
    }
211
212
    /**
213
     * Return the elevation
214
     *
215
     * @return float Elevation
216
     */
217
    public function getElevation()
218
    {
219
        return $this->object()->getElevation();
220
    }
221
222
    /**
223
     * Set the elevation
224
     *
225
     * @param float $elevation
226
     * @return ObjectInterface Self reference
227
     */
228
    public function setElevation($elevation)
229
    {
230
        return $this->object()->setElevation($elevation);
231
    }
232
233
    /**
234
     * Return the object draft mode
235
     *
236
     * @return boolean Object draft mode
237
     */
238
    public function isDraft()
239
    {
240
        return $this->object()->isDraft();
241
    }
242
243
    /**
244
     * Return whether the object is in modified state
245
     *
246
     * @return boolean Modified state
247
     */
248
    public function hasBeenModified()
249
    {
250
        return $this->object()->hasBeenModified();
251
    }
252
253
    /**
254
     * Return whether the object is in mutated state
255
     *
256
     * @return boolean Mutated state
257
     */
258
    public function hasBeenMutated()
259
    {
260
        return $this->object()->hasBeenMutated();
261
    }
262
263
    /**
264
     * Return the creation date & time
265
     *
266
     * @return \DateTimeInterface Creation date & time
267
     */
268
    public function getCreated()
269
    {
270
        return $this->object()->getCreated();
271
    }
272
273
    /**
274
     * Return the deletion date & time
275
     *
276
     * @return \DateTimeInterface Deletion date & time
277
     */
278
    public function getDeleted()
279
    {
280
        return $this->object()->getDeleted();
281
    }
282
283
    /**
284
     * Return the modification date & time
285
     *
286
     * @return \DateTimeInterface Modification date & time
287
     */
288
    public function getModified()
289
    {
290
        return $this->object()->getModified();
291
    }
292
293
    /**
294
     * Return the publication date & time
295
     *
296
     * @return \DateTimeInterface Publication date & time
297
     */
298
    public function getPublished()
299
    {
300
        return $this->object()->getPublished();
301
    }
302
303
    /**
304
     * Return the object title
305
     *
306
     * @return string Object title
307
     */
308 1
    public function getTitle()
309
    {
310 1
        return $this->object()->getTitle();
311
    }
312
313
    /**
314
     * Set the title
315
     *
316
     * @param string $title Title
317
     * @return ObjectInterface Self reference
318
     */
319
    public function setTitle($title)
320
    {
321
        return $this->object()->setTitle($title);
322
    }
323
324
    /**
325
     * Return the object slug
326
     *
327
     * @return string Object slug
328
     */
329
    public function getSlug()
330
    {
331
        return $this->object()->getSlug();
332
    }
333
334
    /**
335
     * Set the slug
336
     *
337
     * @param string $slug Slug
338
     * @return ObjectInterface Self reference
339
     */
340
    public function setSlug($slug)
341
    {
342
        return $this->object()->setSlug($slug);
343
    }
344
345
    /**
346
     * Return the object description
347
     *
348
     * @return string Object description
349
     */
350
    public function getDescription()
351
    {
352
        return $this->object()->getDescription();
353
    }
354
355
    /**
356
     * Set the description
357
     *
358
     * @param string $description Description
359
     * @return ObjectInterface Self reference
360
     */
361
    public function setDescription($description)
362
    {
363
        return $this->object()->setDescription($description);
364
    }
365
366
    /**
367
     * Return the object abstract
368
     *
369
     * @return string Object abstract
370
     */
371
    public function getAbstract()
372
    {
373
        return $this->object()->getAbstract();
374
    }
375
376
    /**
377
     * Set the abstract
378
     *
379
     * @param string $abstract Abstract
380
     * @return ObjectInterface Self reference
381
     */
382
    public function setAbstract($abstract)
383
    {
384
        return $this->object()->setAbstract($abstract);
385
    }
386
387
    /**
388
     * Return all object keywords
389
     *
390
     * @return array Object keywords
391
     */
392
    public function getKeywords()
393
    {
394
        return $this->object()->getKeywords();
395
    }
396
397
    /**
398
     * Set the keywords
399
     *
400
     * @param array $keywords Keywords
401
     * @return ObjectInterface Self reference
402
     */
403
    public function setKeywords(array $keywords)
404
    {
405
        return $this->object()->setKeywords($keywords);
406
    }
407
408
    /**
409
     * Return the license
410
     *
411
     * @return string License
412
     */
413
    public function getLicense()
414
    {
415
        return $this->object()->getLicense();
416
    }
417
418
    /**
419
     * Set the license
420
     *
421
     * @param string $license License
422
     * @return ObjectInterface Self reference
423
     */
424
    public function setLicense($license)
425
    {
426
        return $this->object()->setLicense($license);
427
    }
428
429
    /**
430
     * Return the language
431
     *
432
     * @return string Language
433
     */
434
    public function getLanguage()
435
    {
436
        return $this->object()->getLanguage();
437
    }
438
439
    /**
440
     * Return the privacy
441
     *
442
     * @return string Privacy
443
     */
444
    public function getPrivacy()
445
    {
446
        return $this->object()->getPrivacy();
447
    }
448
449
    /**
450
     * Set the privacy
451
     *
452
     * @param string $privacy Privacy
453
     * @return ObjectInterface Self reference
454
     */
455
    public function setPrivacy($privacy)
456
    {
457
        return $this->object()->setPrivacy($privacy);
458
    }
459
460
    /**
461
     * Return all object categories
462
     *
463
     * @return array Object categories
464
     */
465
    public function getCategories()
466
    {
467
        return $this->object()->getCategories();
468
    }
469
470
    /**
471
     * Set the categories
472
     *
473
     * @param array $categories Categories
474
     * @return ObjectInterface Self reference
475
     */
476
    public function setCategories(array $categories)
477
    {
478
        return $this->object()->setCategories($categories);
479
    }
480
481
    /**
482
     * Get a domain property value
483
     *
484
     * Multi-level properties might be traversed by property name locators separated with colons (":").
485
     *
486
     * @param string $property Property name
487
     * @return mixed Property value
488
     */
489
    public function getDomain($property)
490
    {
491
        return $this->object()->getDomain($property);
492
    }
493
494
    /**
495
     * Set a domain property value
496
     *
497
     * @param string $property Property name
498
     * @param mixed $value Property value
499
     * @return ObjectInterface Self reference
500
     */
501
    public function setDomain($property, $value)
502
    {
503
        return $this->object()->setDomain($property, $value);
504
    }
505
506
    /**
507
     * Get a processing instruction
508
     *
509
     * @param string $procInst Processing instruction name
510
     * @return mixed Processing instruction
511
     */
512
    public function getProcessingInstruction($procInst)
513
    {
514
        return $this->object()->getProcessingInstruction($procInst);
515
    }
516
517
    /**
518
     * Set a processing instruction
519
     *
520
     * @param string $procInst Processing instruction name
521
     * @param mixed $value Processing instruction
522
     * @return ObjectInterface Self reference
523
     */
524
    public function setProcessingInstruction($procInst, $value)
525
    {
526
        return $this->object()->setProcessingInstruction($procInst, $value);
527
    }
528
529
    /**
530
     * Return the canonical object URL
531
     *
532
     * @return string
533
     */
534
    public function getCanonicalUrl()
535
    {
536
        return $this->getAbsoluteUrl();
537
    }
538
539
    /**
540
     * Return the absolute object URL
541
     *
542
     * @return string
543
     */
544
    public function getAbsoluteUrl()
545
    {
546
        return strval($this->getUrl());
547
    }
548
549
    /**
550
     * Generic caller
551
     *
552
     * @param string $name Method name
553
     * @param array $arguments Method arguments
554
     */
555
    public function __call($name, $arguments)
556
    {
557
        $object = $this->object();
558
        if (is_callable(array($object, $name))) {
559
            return $object->$name(...$arguments);
560
        }
561
562
        throw new InvalidArgumentException(
563
            sprintf('Invalid object proxy method "%s"', $name),
564
            InvalidArgumentException::INVALID_OBJECT_PROXY_METHOD
565
        );
566
    }
567
568
    /**
569
     * Use a specific object revision
570
     *
571
     * @param Revision $revision Revision to be used
572
     * @return ObjectInterface Object
573
     */
574
    public function useRevision(Revision $revision)
575
    {
576
        return $this->object()->useRevision($revision);
577
    }
578
579
    /**
580
     * Persist the current object revision
581
     *
582
     * @return ObjectInterface Object
583
     */
584
    public function persist()
585
    {
586
        return $this->object()->persist();
587
    }
588
589
    /**
590
     * Return whether the object is in published state
591
     *
592
     * @return boolean Published state
593
     */
594
    public function isPublished()
595
    {
596
        return $this->object()->isPublished();
597
    }
598
599
    /**
600
     * Return whether the object has just been published
601
     *
602
     * @return boolean Object has just been published
603
     */
604
    public function hasBeenPublished()
605
    {
606
        return $this->object()->hasBeenPublished();
607
    }
608
609
    /**
610
     * Return whether the object has been deleted
611
     *
612
     * @return boolean Object is deleted
613
     */
614
    public function isDeleted()
615
    {
616
        return $this->object()->isDeleted();
617
    }
618
619
    /**
620
     * Return whether the object has just been deleted
621
     *
622
     * @return boolean Object has just been deleted
623
     */
624
    public function hasBeenDeleted()
625
    {
626
        return $this->object()->hasBeenDeleted();
627
    }
628
629
    /**
630
     * Return whether the object has just been undeleted
631
     *
632
     * @return boolean Object has just been undeleted
633
     */
634
    public function hasBeenUndeleted()
635
    {
636
        return $this->object()->hasBeenUndeleted();
637
    }
638
639
    /**
640
     * Publish the current object revision
641
     *
642
     * @return ObjectInterface Object
643
     */
644
    public function publish()
645
    {
646
        return $this->object()->publish();
647
    }
648
649
    /**
650
     * Delete the object and all its revisions
651
     *
652
     * @return ObjectInterface Object
653
     */
654
    public function delete()
655
    {
656
        return $this->object()->delete();
657
    }
658
659
    /**
660
     * Undelete the object and all its revisions
661
     *
662
     * @return ObjectInterface Object
663
     */
664
    public function undelete()
665
    {
666
        return $this->object()->undelete();
667
    }
668
669
    /**
670
     * Add an object relation
671
     *
672
     * @param string|RelationInterface $relation Serialized or instantiated object relation
673
     * @param string|null $relationType Relation type
674
     * @return ObjectInterface
675
     */
676
    public function addRelation($relation, $relationType = null)
677
    {
678
        return $this->object()->addRelation($relation, $relationType);
679
    }
680
681
    /**
682
     * Delete an object relation
683
     *
684
     * @param RelationInterface $relation Object relation
685
     * @return ObjectInterface
686
     */
687
    public function deleteRelation(RelationInterface $relation)
688
    {
689
        return $this->object()->deleteRelation($relation);
690
    }
691
692
    /**
693
     * Get all relations (optional: Of a particular type)
694
     *
695
     * @param string|null $relationType Optional: Relation type
696
     * @return array Object relations
697
     */
698
    public function getRelations($relationType = null)
699
    {
700
        return $this->object()->getRelations($relationType);
701
    }
702
703
    /**
704
     * Find and return particular relations
705
     *
706
     * @param array $criteria Relation criteria
707
     * @return RelationInterface[] Relations
708
     */
709
    public function findRelations(array $criteria)
710
    {
711
        return $this->object()->findRelations($criteria);
712
    }
713
}
714