Completed
Push — master ( 42a244...873090 )
by Mariusz
03:17
created

Facade::resolveOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Malef\Associate\DoctrineOrm;
4
5
use Malef\Associate\DoctrineOrm\Association\AssociationTreeBuilder;
6
use Malef\Associate\DoctrineOrm\Collector\AssociationCollector;
7
use Malef\Associate\DoctrineOrm\Loader\AssociationLoader;
8
use Malef\Associate\DoctrineOrm\Loader\ChunkingStrategy\ChunkingStrategy;
9
use Malef\Associate\DoctrineOrm\Loader\DeferredEntityLoaderFactory;
10
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\OneToOneInverseSideAssociationLoadingStrategy;
11
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\ToManyAssociationLoadingStrategy;
12
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\ToOneAssociationLoadingStrategy;
13
use Malef\Associate\DoctrineOrm\Loader\EntityLoader;
14
use Malef\Associate\DoctrineOrm\Loader\EntityLoaderFactory;
15
use Malef\Associate\DoctrineOrm\Loader\UninitializedProxiesLoader;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
use Symfony\Component\PropertyAccess\PropertyAccess;
19
20
class Facade
21
{
22
    const CHUNK_SIZE_FOR_UNINITIALIZED_PROXIES = 'chunk_size_for_uninitialized_proxies';
23
    const CHUNK_SIZE_FOR_TO_MANY_ASSOCIATIONS = 'chunk_size_for_to_many_associations';
24
25
    /**
26
     * @var EntityManagerInterface
27
     */
28
    protected $entityManager;
29
30
    /**
31
     * @var array
32
     */
33
    protected $options;
34
35
    public function __construct(EntityManagerInterface $entityManager, array $options = [])
36
    {
37
        $this->entityManager = $entityManager;
38
        $this->options = $this->resolveOptions($options);
39
    }
40
41
    public function createAssociationTreeBuilder(): AssociationTreeBuilder
42
    {
43
        return new AssociationTreeBuilder();
44
    }
45
46
    public function createEntityLoader(): EntityLoader
47
    {
48
        $associationCollector = new AssociationCollector(
49
            PropertyAccess::createPropertyAccessor()
50
        );
51
52
        $uninitializedProxiesLoader = new UninitializedProxiesLoader(
53
            new ChunkingStrategy($this->options[self::CHUNK_SIZE_FOR_UNINITIALIZED_PROXIES])
54
        );
55
56
        $entityLoaderFactory = new EntityLoaderFactory(
57
            new AssociationLoader(
58
                new OneToOneInverseSideAssociationLoadingStrategy(),
59
                new ToOneAssociationLoadingStrategy(
60
                    $associationCollector,
61
                    $uninitializedProxiesLoader
62
                ),
63
                new ToManyAssociationLoadingStrategy(
64
                    new ChunkingStrategy($this->options[self::CHUNK_SIZE_FOR_TO_MANY_ASSOCIATIONS])
65
                )
66
            ),
67
            $associationCollector,
68
            $uninitializedProxiesLoader
69
        );
70
71
        return $entityLoaderFactory->create($this->entityManager);
72
    }
73
74
    public function createDeferredEntityLoaderFactory(): DeferredEntityLoaderFactory
75
    {
76
        return new DeferredEntityLoaderFactory(
77
            $this->createEntityLoader()
78
        );
79
    }
80
81
    protected function resolveOptions(array $options): array
82
    {
83
        $optionsResolver = new OptionsResolver();
84
        $optionsResolver
85
            ->setRequired([
86
                self::CHUNK_SIZE_FOR_UNINITIALIZED_PROXIES,
87
                self::CHUNK_SIZE_FOR_TO_MANY_ASSOCIATIONS,
88
            ])
89
            ->setAllowedTypes(self::CHUNK_SIZE_FOR_UNINITIALIZED_PROXIES, 'int')
90
            ->setDefault(self::CHUNK_SIZE_FOR_UNINITIALIZED_PROXIES, 100)
91
            ->setAllowedTypes(self::CHUNK_SIZE_FOR_TO_MANY_ASSOCIATIONS, 'int')
92
            ->setDefault(self::CHUNK_SIZE_FOR_TO_MANY_ASSOCIATIONS, 100);
93
94
        return $optionsResolver->resolve($options);
95
    }
96
}
97