AbstractPresenter::__get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Umbrellio\Common\Contracts;
6
7
use Hemp\Presenter\Presenter;
8
use Illuminate\Contracts\Queue\QueueableEntity;
9
use JsonSerializable;
10
11
/**
12
 * @codeCoverageIgnore
13
 */
14
class AbstractPresenter extends Presenter implements JsonSerializable, QueueableEntity
15
{
16
    private $relationMapping = [];
17
18
    public function __isset($key)
19
    {
20
        return $this->offsetExists($key);
21
    }
22
23
    public function __get($key)
24
    {
25
        if ($this->isRelationMutator($key)) {
26
            return $this->mutateRelation($key);
27
        }
28
        return parent::__get($key);
29
    }
30
31
    public function getQueueableId()
32
    {
33
        return $this->model->getQueueableId();
34
    }
35
36
    public function getQueueableRelations()
37
    {
38
        return $this->model->getQueueableRelations();
39
    }
40
41
    public function getQueueableConnection()
42
    {
43
        return $this->model->getQueueableConnection();
44
    }
45
46
    public function jsonSerialize()
47
    {
48
        return $this->toArray();
49
    }
50
51
    public function relationPresenters(array $mapping): self
52
    {
53
        $this->relationMapping = $mapping;
54
        return $this;
55
    }
56
57
    private function isRelationMutator($key): bool
58
    {
59
        return array_key_exists($key, $this->relationMapping);
60
    }
61
62
    private function mutateRelation($key)
63
    {
64
        $presenterClass = $this->relationMapping[$key];
65
        $result = $this->model->{$key};
66
        if ($result === null) {
67
            return null;
68
        }
69
        return $result->present($presenterClass);
70
    }
71
}
72