Passed
Pull Request — master (#226)
by Christopher
06:17
created

AssociationStubPolymorphic::isKnownSide()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
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
        $thisTarg = $this->getTargType();
41
        $thatTarg = $otherStub->getTargType();
42
        $thisNull = null === $thisTarg;
43
        $thatNull = null === $thatTarg;
44
        if ($thisNull == $thatNull && null === $thatNull) {
0 ignored issues
show
introduced by
The condition null === $thatNull is always false.
Loading history...
45
            return false;
46
        }
47
        if((!$thisNull &&
48
            !$thatNull) &&
49
            ($this->getBaseType() !== $otherStub->getTargType() ||
50
            $otherStub->getBaseType() !== $this->getTargType())
51
        ){
52
            return false;
53
        }
54
55
56
        if ($thisNull && ($thatTarg != $this->getBaseType())) {
57
            return false;
58
        }
59
        if ($thatNull && ($thisTarg != $otherStub->getBaseType())) {
60
            return false;
61
        }
62
63
        return true;
64
    }
65
66
    /**
67
     * @throws InvalidOperationException
68
     * @return bool
69
     */
70
    public function isKnownSide()
71
    {
72
        if (!($this->isOk())) {
73
            throw new InvalidOperationException('Polymorphic stub not OK so known-side determination is meaningless');
74
        }
75
        return null === $this->targType;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function morphicType()
82
    {
83
        return 'polymorphic';
84
    }
85
}
86