Completed
Push — master ( cec909...bbe7af )
by Jasper
11s queued 10s
created

MorphToRelation   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 57.58%

Importance

Changes 0
Metric Value
wmc 12
eloc 27
dl 0
loc 142
ccs 19
cts 33
cp 0.5758
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A shouldOmitIncluded() 0 3 1
A hasIncluded() 0 3 1
A getIncluded() 0 3 1
A getId() 0 3 1
A dissociate() 0 5 1
A getType() 0 3 1
A setType() 0 5 1
A setId() 0 5 1
A setOmitIncluded() 0 5 1
A associate() 0 14 2
1
<?php
2
3
namespace Swis\JsonApi\Client\Relations;
4
5
use Swis\JsonApi\Client\Interfaces\DataInterface;
6
use Swis\JsonApi\Client\Interfaces\ItemInterface;
7
use Swis\JsonApi\Client\Interfaces\RelationInterface;
8
9
class MorphToRelation implements RelationInterface
10
{
11
    /**
12
     * @var \Swis\JsonApi\Client\Interfaces\ItemInterface
13
     */
14
    protected $included;
15
16
    /**
17
     * @var string
18
     */
19
    protected $type;
20
21
    /**
22
     * @var int
23
     */
24
    protected $id;
25
26
    /**
27
     * @var \Swis\JsonApi\Client\Interfaces\ItemInterface
28
     */
29
    protected $parentItem;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $omitIncluded = false;
35
36
    /**
37
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
38
     */
39 18
    public function __construct(ItemInterface $item)
40
    {
41 18
        $this->parentItem = $item;
42 18
    }
43
44
    /**
45
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $included
46
     *
47
     * @throws \InvalidArgumentException
48
     *
49
     * @return static
50
     */
51 15
    public function associate(DataInterface $included)
52
    {
53 15
        if (!$included instanceof ItemInterface) {
54
            throw new \InvalidArgumentException(
55
                sprintf('MorphTo expects relation to be an instance of %s', ItemInterface::class)
56
            );
57
        }
58
59 15
        $this->setId($included->getId());
60 15
        $this->setType($included->getType());
61
62 15
        $this->included = $included;
63
64 15
        return $this;
65
    }
66
67
    /**
68
     * @return static
69
     */
70
    public function dissociate()
71
    {
72
        $this->included = null;
73
74
        return $this;
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    public function getId()
81
    {
82
        return $this->id;
83
    }
84
85
    /**
86
     * @param mixed $id
87
     *
88
     * @return static
89
     */
90 15
    public function setId($id)
91
    {
92 15
        $this->id = $id;
93
94 15
        return $this;
95
    }
96
97
    /**
98
     * @param string $type
99
     *
100
     * @return static
101
     */
102 15
    public function setType(string $type)
103
    {
104 15
        $this->type = $type;
105
106 15
        return $this;
107
    }
108
109
    /**
110
     * @return null|string
111
     */
112 3
    public function getType(): string
113
    {
114 3
        return $this->type;
115
    }
116
117
    /**
118
     * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
119
     */
120 12
    public function getIncluded()
121
    {
122 12
        return $this->included;
123
    }
124
125
    /**
126
     * @return bool
127
     */
128
    public function hasIncluded(): bool
129
    {
130
        return null !== $this->included;
131
    }
132
133
    /**
134
     * @param bool $omitIncluded
135
     *
136
     * @return static
137
     */
138
    public function setOmitIncluded(bool $omitIncluded)
139
    {
140
        $this->omitIncluded = $omitIncluded;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function shouldOmitIncluded(): bool
149
    {
150
        return $this->omitIncluded;
151
    }
152
}
153