AbstractOneRelation::getAssociated()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 11
ccs 3
cts 6
cp 0.5
crap 4.125
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Swis\JsonApi\Client\Relations;
6
7
use Swis\JsonApi\Client\Interfaces\ItemInterface;
8
use Swis\JsonApi\Client\Interfaces\OneRelationInterface;
9
10
/**
11
 * @property \Swis\JsonApi\Client\Interfaces\ItemInterface|false|null $data
12
 * @property \Swis\JsonApi\Client\Interfaces\ItemInterface|false|null $included
13
 */
14
abstract class AbstractOneRelation extends AbstractRelation implements OneRelationInterface
15
{
16
    /**
17
     * @return $this
18
     */
19 184
    public function setData(?ItemInterface $data)
20
    {
21 184
        $this->data = $data;
22
23 184
        return $this;
24
    }
25
26 12
    public function getData(): ?ItemInterface
27
    {
28 12
        return $this->data ?: null;
29
    }
30
31
    /**
32
     * @return $this
33
     */
34 184
    public function setIncluded(?ItemInterface $included)
35
    {
36 184
        $this->included = $included;
37
38 184
        return $this;
39
    }
40
41 148
    public function getIncluded(): ?ItemInterface
42
    {
43 148
        return $this->included ?: null;
44
    }
45
46
    /**
47
     * @return $this
48
     */
49 184
    public function associate(ItemInterface $included)
50
    {
51 184
        return $this->setData($included)
52 184
            ->setIncluded($included);
53
    }
54
55 64
    public function getAssociated(): ?ItemInterface
56
    {
57 64
        if ($this->hasIncluded()) {
58 64
            return $this->getIncluded();
59
        }
60
61
        if ($this->hasData()) {
62
            return $this->getData();
63
        }
64
65
        return null;
66
    }
67
}
68