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

HasOneRelation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 3
eloc 10
dl 0
loc 45
ccs 8
cts 12
cp 0.6667
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A associate() 0 8 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
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