Passed
Pull Request — master (#226)
by Alex
03:56
created

AssociationStubPolymorphic::isCompatible()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 13
c 7
b 0
f 0
dl 0
loc 22
rs 9.2222
cc 6
nc 5
nop 1
1
<?php declare(strict_types=1);
2
3
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations;
4
5
use POData\Common\InvalidOperationException;
6
7
class AssociationStubPolymorphic extends AssociationStubBase
8
{
9
    /**
10
     * @var string
11
     */
12
    private $morphType;
13
14
    /**
15
     * @return string
16
     */
17
    public function getMorphType()
18
    {
19
        return $this->morphType;
20
    }
21
22
    /**
23
     * @param string $morphType
24
     */
25
    public function setMorphType($morphType)
26
    {
27
        $this->morphType = $morphType;
28
    }
29
30
    /**
31
     * @param \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations\AssociationStubBase $otherStub
32
     *
33
     * @return bool
34
     */
35
    public function isCompatible(AssociationStubBase $otherStub)
36
    {
37
        if (!parent::isCompatible($otherStub)) {
38
            return false;
39
        }
40
41
        if (null === $this->getTargType() && null === $otherStub->getTargType()) {
0 ignored issues
show
introduced by
The condition null === $this->getTargType() is always false.
Loading history...
42
            return false;
43
        }
44
45
        $thisBase = $this->getBaseType();
46
        $thatBase = $otherStub->getBaseType();
47
        $thatTarg = $otherStub->getTargType() ?? $thisBase;
48
        $thisTarg = $this->getTargType() ?? $thatBase;
49
        if ($thatTarg != $thisBase) {
50
            return false;
51
        }
52
        if ($thisTarg != $thatBase) {
53
            return false;
54
        }
55
56
        return true;
57
    }
58
59
    /**
60
     * @throws InvalidOperationException
61
     * @return bool
62
     */
63
    public function isKnownSide()
64
    {
65
        if (!($this->isOk())) {
66
            throw new InvalidOperationException('Polymorphic stub not OK so known-side determination is meaningless');
67
        }
68
        return null === $this->targType;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function morphicType()
75
    {
76
        return 'polymorphic';
77
    }
78
}
79