Passed
Push — master ( 571547...7a0005 )
by Jasper
03:20
created

HasOneRelation::hasIncluded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 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
8
class HasOneRelation extends AbstractOneRelation
9
{
10
    /**
11
     * @var \Swis\JsonApi\Client\Interfaces\ItemInterface
12
     */
13
    protected $parentItem;
14
15
    /**
16
     * @param string                                        $type
17
     * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
18
     */
19 30
    public function __construct(string $type, ItemInterface $item)
20
    {
21 30
        $this->type = $type;
22 30
        $this->parentItem = $item;
23 30
    }
24
25
    /**
26
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $included
27
     *
28
     * @throws \InvalidArgumentException
29
     *
30
     * @return $this
31
     */
32 25
    public function associate(DataInterface $included)
33
    {
34 25
        $result = parent::associate($included);
35
36
        // Set the $relation.'_id' on the parent
37 25
        $this->parentItem->setAttribute($this->type.'_id', $this->getId());
38
39 25
        return $result;
40
    }
41
42
    /**
43
     * @return $this
44
     */
45
    public function dissociate()
46
    {
47
        $result = parent::dissociate();
48
49
        // Remove the $relation.'_id' on the parent
50
        $this->parentItem->setAttribute($this->type.'_id', null);
51
52
        return $result;
53
    }
54
}
55