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

HasOneRelation   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 54.29%

Importance

Changes 0
Metric Value
wmc 12
eloc 29
dl 0
loc 148
ccs 19
cts 35
cp 0.5429
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setId() 0 5 1
A getType() 0 3 1
A setType() 0 5 1
A setOmitIncluded() 0 5 1
A associate() 0 15 2
A getIncluded() 0 3 1
A shouldOmitIncluded() 0 3 1
A hasIncluded() 0 3 1
A getId() 0 3 1
A dissociate() 0 8 1
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 HasOneRelation 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 string                                        $type
38
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
39
     */
40 18
    public function __construct(string $type, ItemInterface $item)
41
    {
42 18
        $this->parentItem = $item;
43 18
        $this->type = $type;
44 18
    }
45
46
    /**
47
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $included
48
     *
49
     * @throws \InvalidArgumentException
50
     *
51
     * @return static
52
     */
53 15
    public function associate(DataInterface $included)
54
    {
55 15
        if (!$included instanceof ItemInterface) {
56
            throw new \InvalidArgumentException(
57
                sprintf('HasOne expects relation to be an instance of %s', ItemInterface::class)
58
            );
59
        }
60
61 15
        $this->setId($included->getId());
62 15
        $this->included = $included;
63
64
        // Set the $relation.'_id' on the parent
65 15
        $this->parentItem->setAttribute($this->type.'_id', $this->getId());
66
67 15
        return $this;
68
    }
69
70
    /**
71
     * @return static
72
     */
73
    public function dissociate()
74
    {
75
        $this->included = null;
76
77
        // Remove the $relation.'_id' on the parent
78
        $this->parentItem->setAttribute($this->type.'_id', null);
79
80
        return $this;
81
    }
82
83
    /**
84
     * @return mixed
85
     */
86 18
    public function getId()
87
    {
88 18
        return $this->id;
89
    }
90
91
    /**
92
     * @param mixed $id
93
     *
94
     * @return static
95
     */
96 18
    public function setId($id)
97
    {
98 18
        $this->id = $id;
99
100 18
        return $this;
101
    }
102
103
    /**
104
     * @param string $type
105
     *
106
     * @return static
107
     */
108
    public function setType(string $type)
109
    {
110
        $this->type = $type;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return string
117
     */
118 9
    public function getType(): string
119
    {
120 9
        return $this->type;
121
    }
122
123
    /**
124
     * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
125
     */
126 6
    public function getIncluded()
127
    {
128 6
        return $this->included;
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function hasIncluded(): bool
135
    {
136
        return null !== $this->included;
137
    }
138
139
    /**
140
     * @param bool $omitIncluded
141
     *
142
     * @return static
143
     */
144
    public function setOmitIncluded(bool $omitIncluded)
145
    {
146
        $this->omitIncluded = $omitIncluded;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @return bool
153
     */
154
    public function shouldOmitIncluded(): bool
155
    {
156
        return $this->omitIncluded;
157
    }
158
}
159