AbstractPresenter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 18
c 1
b 0
f 0
dl 0
loc 56
ccs 0
cts 25
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getQueueableConnection() 0 3 1
A __get() 0 6 2
A mutateRelation() 0 8 2
A relationPresenters() 0 4 1
A isRelationMutator() 0 3 1
A __isset() 0 3 1
A getQueueableId() 0 3 1
A jsonSerialize() 0 3 1
A getQueueableRelations() 0 3 1
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