Passed
Branch master (9f13d4)
by Alex
50:02 queued 44:57
created

AssociationFactory::marshalPolyToMono()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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