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

AbstractOneRelation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 65%

Importance

Changes 0
Metric Value
wmc 7
eloc 17
dl 0
loc 79
ccs 13
cts 20
cp 0.65
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A associate() 0 14 2
A getIncluded() 0 3 1
A setId() 0 5 1
A dissociate() 0 5 1
A hasIncluded() 0 3 1
A getId() 0 3 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
abstract class AbstractOneRelation extends AbstractRelation
9
{
10
    /**
11
     * @var \Swis\JsonApi\Client\Interfaces\ItemInterface
12
     */
13
    protected $included;
14
15
    /**
16
     * @var int
17
     */
18
    protected $id;
19
20
    /**
21
     * @param \Swis\JsonApi\Client\Interfaces\DataInterface $included
22
     *
23
     * @throws \InvalidArgumentException
24
     *
25
     * @return $this
26
     */
27 50
    public function associate(DataInterface $included)
28
    {
29 50
        if (!$included instanceof ItemInterface) {
30
            throw new \InvalidArgumentException(
31
                sprintf('%s expects relation to be an instance of %s', static::class, ItemInterface::class)
32
            );
33
        }
34
35 50
        $this->setId($included->getId());
36 50
        $this->setType($included->getType());
37
38 50
        $this->included = $included;
39
40 50
        return $this;
41
    }
42
43
    /**
44
     * @return $this
45
     */
46
    public function dissociate()
47
    {
48
        $this->included = null;
49
50
        return $this;
51
    }
52
53
    /**
54
     * @return mixed
55
     */
56 30
    public function getId()
57
    {
58 30
        return $this->id;
59
    }
60
61
    /**
62
     * @param mixed $id
63
     *
64
     * @return $this
65
     */
66 55
    public function setId($id)
67
    {
68 55
        $this->id = $id;
69
70 55
        return $this;
71
    }
72
73
    /**
74
     * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
75
     */
76 30
    public function getIncluded()
77
    {
78 30
        return $this->included;
79
    }
80
81
    /**
82
     * @return bool
83
     */
84
    public function hasIncluded(): bool
85
    {
86
        return null !== $this->getIncluded();
87
    }
88
}
89