getSupportingAssociationLoadingStrategy()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 10
1
<?php
2
3
namespace Malef\Associate\DoctrineOrm\Loader;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use Malef\Associate\AssociateException;
8
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\AssociationLoadingStrategyInterface;
9
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\OneToOneInverseSideAssociationLoadingStrategy;
10
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\ToManyAssociationLoadingStrategy;
11
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\ToOneAssociationLoadingStrategy;
12
use Malef\Associate\DoctrineOrm\Metadata\AssociationMetadataAdapter;
13
14
class AssociationLoader
15
{
16
    /**
17
     * @var Collection|AssociationLoadingStrategyInterface[]
18
     */
19
    protected $associationLoadingStrategies;
20
21
    public function __construct(
22
        OneToOneInverseSideAssociationLoadingStrategy $oneToOneInverseSideAssociationLoadingStrategy,
23
        ToOneAssociationLoadingStrategy $toOneAssociationLoadingStrategy,
24
        ToManyAssociationLoadingStrategy $toManyAssociationLoadingStrategy
25
    ) {
26
        $this->associationLoadingStrategies = new ArrayCollection([
27
            $oneToOneInverseSideAssociationLoadingStrategy,
28
            $toOneAssociationLoadingStrategy,
29
            $toManyAssociationLoadingStrategy,
30
        ]);
31
    }
32
33
    /**
34
     * @throws AssociateException if no association loading strategy
35
     *                            or multiple association loading strategies are found
36
     */
37
    public function load(
38
        ArrayCollection $sourceEntities,
39
        AssociationMetadataAdapter $associationMetadataAdapter
40
    ): void {
41
        $this
42
            ->getSupportingAssociationLoadingStrategy($associationMetadataAdapter)
43
            ->load($sourceEntities, $associationMetadataAdapter);
44
    }
45
46
    protected function getSupportingAssociationLoadingStrategy(
47
        AssociationMetadataAdapter $associationMetadataAdapter
48
    ): AssociationLoadingStrategyInterface {
49
        $supportingAssociationLoadingStrategies = $this->associationLoadingStrategies->filter(
50
            function (AssociationLoadingStrategyInterface $associationLoadingStrategy) use ($associationMetadataAdapter) {
51
                return $associationLoadingStrategy->supports($associationMetadataAdapter);
52
            }
53
        );
54
55
        if ($supportingAssociationLoadingStrategies->isEmpty()) {
56
            throw new AssociateException('No association loading strategy found.');
57
        }
58
59
        if (1 < $supportingAssociationLoadingStrategies->count()) {
60
            throw new AssociateException('Multiple association loading strategies found.');
61
        }
62
63
        return $supportingAssociationLoadingStrategies->first();
64
    }
65
}
66