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

MorphToRelation::associate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
ccs 6
cts 8
cp 0.75
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
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