Completed
Push — master ( 318ca6...97a34f )
by Jasper
22s queued 10s
created

AbstractOneRelation::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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|null
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 60
    public function associate(DataInterface $included)
28
    {
29 60
        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 60
        $this->setId($included->getId());
36
37 60
        $this->included = $included;
38
39 60
        return $this;
40
    }
41
42
    /**
43
     * @return $this
44
     */
45 10
    public function dissociate()
46
    {
47 10
        $this->setId(null);
48
49 10
        $this->included = null;
50
51 10
        return $this;
52
    }
53
54
    /**
55
     * @return mixed
56
     */
57 25
    public function getId()
58
    {
59 25
        return $this->id;
60
    }
61
62
    /**
63
     * @param mixed $id
64
     *
65
     * @return $this
66
     */
67 75
    public function setId($id)
68
    {
69 75
        $this->id = $id;
70
71 75
        return $this;
72
    }
73
74
    /**
75
     * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
76
     */
77 60
    public function getIncluded()
78
    {
79 60
        return $this->included;
80
    }
81
82
    /**
83
     * @return bool
84
     */
85
    public function hasIncluded(): bool
86
    {
87
        return null !== $this->getIncluded();
88
    }
89
}
90