Completed
Push — master ( 9f13d4...a886ae )
by Alex
38:38 queued 38:36
created

AssociationFactory::getAssocationFromStubs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations;
7
8
use Illuminate\Support\Str;
9
10
abstract class AssociationFactory
11
{
12
    public static $marshalPolymorphics = true;
13
14
    /**
15
     * @param  AssociationStubBase $stubOne
16
     * @param  AssociationStubBase $stubTwo
17
     * @throws \Exception
18
     * @return Association
19
     */
20
    public static function getAssocationFromStubs(
21
        AssociationStubBase $stubOne,
22
        AssociationStubBase $stubTwo
23
    ): Association {
24
        $checkAssociation = self::checkAssociations($stubOne, $stubTwo);
25
        return null === $checkAssociation ? self::buildAssociationFromStubs($stubOne, $stubTwo) : $checkAssociation;
26
    }
27
28
    /**
29
     * @param  AssociationStubBase $stubOne
30
     * @param  AssociationStubBase $stubTwo
31
     * @throws \Exception
32
     * @return Association
33
     */
34
    private static function buildAssociationFromStubs(
35
        AssociationStubBase $stubOne,
36
        AssociationStubBase $stubTwo
37
    ): Association {
38
        $first = -1 === $stubOne->compare($stubTwo);
39
40
        $association = new AssociationMonomorphic();
41
        if ($stubOne->getTargType() == null && self::$marshalPolymorphics) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $stubOne->getTargType() of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
42
            $stubOne->addAssociation($association);
43
            $stubOne = self::marshalPolyToMono($stubOne, $stubTwo);
44
        }
45
46
        $input = [intval(!$first) => $stubOne, intval($first) => $stubTwo];
47
        $association->setFirst($input[0]);
48
        $association->setLast($input[1]);
49
        return $association;
50
    }
51
52
    private static function marshalPolyToMono(
53
        AssociationStubBase $stub,
54
        AssociationStubBase $stubTwo
55
    ): AssociationStubBase {
56
        $stubNew         = clone $stub;
57
        $relPolyTypeName = substr($stubTwo->getBaseType(), strrpos($stubTwo->getBaseType(), '\\')+1);
58
        $relPolyTypeName = Str::plural($relPolyTypeName, 1);
59
        $stubNew->setRelationName($stub->getRelationName() . '_' . $relPolyTypeName);
60
        $stubNew->setTargType($stubTwo->getBaseType());
61
        $stubNew->setForeignFieldName($stubTwo->getKeyFieldName());
62
        $entity = $stub->getEntity();
63
        $stubs  = $entity->getStubs();
64
65
        $stubs[$stubNew->getRelationName()] = $stubNew;
66
        $entity->setStubs($stubs);
67
        return $stubNew;
68
    }
69
70
    private static function checkAssociations(
71
        AssociationStubBase $stubOne,
72
        AssociationStubBase $stubTwo
73
    ): ?Association {
74
        $assocOne = $stubOne->getAssociations();
75
        foreach ($assocOne as $association) {
76
            $isFirst = $association->getFirst() === $stubOne;
77
            if ($association->{$isFirst ? 'getLast' : 'getFirst'}() == $stubTwo) {
78
                return $association;
79
            }
80
        }
81
        return null;
82
    }
83
}
84