Issues (7)

lib/Facade.php (7 issues)

Severity
1
<?php
2
3
namespace Xsolve\Associate;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Xsolve\Associate\AssociationCollecting\BasicAssociationCollectingStrategy;
7
use Xsolve\Associate\AssociationCollecting\DoctrineOrmAssociationCollectingStrategy;
8
use Xsolve\Associate\BufferedCollector\BufferedCollector;
9
use Xsolve\Associate\BufferedCollector\BufferedCollectorInterface;
10
use Xsolve\Associate\CollectionTraversal\ArrayCollectionTraversalStrategy;
11
use Xsolve\Associate\CollectionTraversal\DoctrineOrmCollectionCollectionTraversalStrategy;
12
use Xsolve\Associate\CollectionTraversal\TraversableCollectionTraversalStrategy;
13
use Xsolve\Associate\Loader\DoctrineOrmEntityLoader;
14
use Xsolve\Associate\Loader\DoctrineOrmNonProxiedAssociationQueryExecutor;
15
use Xsolve\Associate\Loader\DoctrineOrmUninitializedProxiesQueryExecutor;
16
use Xsolve\Associate\Metadata\MetadataWrapperProvider;
17
18
// TODO Make separate facade for Doctrine.
19
class Facade
20
{
21
    /**
22
     * @var EntityManagerInterface|null
23
     */
24
    protected $entityManager;
25
26
    /**
27
     * @var MetadataWrapperProvider
28
     */
29
    protected $metadataWrapperProvider;
30
31
    /**
32
     * @var BasicAssociationCollectingStrategy
33
     */
34
    protected $basicAssociationCollectingStrategy;
35
36
    /**
37
     * @var DoctrineOrmAssociationCollectingStrategy
38
     */
39
    protected $doctrineOrmAssociationCollectingStrategy;
40
41
    /**
42
     * @var DoctrineOrmEntityLoader
43
     */
44
    protected $doctrineOrmEntityLoader;
45
46
    /**
47
     * @var CollectorInterface
48
     */
49
    protected $basicCollector;
50
51
    /**
52
     * @var CollectorInterface
53
     */
54
    protected $doctrineOrmCollector;
55
56
    /**
57
     * @var BufferedCollector
58
     */
59
    protected $bufferedCollector;
60
61
    /**
62
     * @param EntityManagerInterface|null $entityManager
63
     */
64
    public function __construct(EntityManagerInterface $entityManager = null)
65
    {
66
        $this->entityManager = $entityManager;
67
    }
68
69
    /**
70
     * @return CollectorInterface
71
     */
72
    public function getBasicCollector(): CollectorInterface
73
    {
74
        if (!$this->basicCollector instanceof CollectorInterface) {
0 ignored issues
show
$this->basicCollector is always a sub-type of Xsolve\Associate\CollectorInterface. If $this->basicCollector can have other possible types, add them to lib/Facade.php:47.
Loading history...
75
            $this->basicCollector = new Collector(
76
                $this->getBasicAssociationCollectingStrategy()
77
            );
78
        }
79
80
        return $this->basicCollector;
81
    }
82
83
    /**
84
     * @return CollectorInterface
85
     */
86
    public function getDoctrineOrmCollector(): CollectorInterface
87
    {
88
        if (!$this->doctrineOrmCollector instanceof CollectorInterface) {
0 ignored issues
show
$this->doctrineOrmCollector is always a sub-type of Xsolve\Associate\CollectorInterface. If $this->doctrineOrmCollector can have other possible types, add them to lib/Facade.php:52.
Loading history...
89
            $this->doctrineOrmCollector = new Collector(
90
                $this->getDoctrineOrmAssociationCollectingStrategy()
91
            );
92
        }
93
94
        return $this->doctrineOrmCollector;
95
    }
96
97
    /**
98
     * @return BufferedCollectorInterface
99
     */
100
    public function getBufferedDoctrineOrmCollector(): BufferedCollectorInterface
101
    {
102
        if (!$this->bufferedCollector instanceof BufferedCollector) {
0 ignored issues
show
$this->bufferedCollector is always a sub-type of Xsolve\Associate\Buffere...ector\BufferedCollector. If $this->bufferedCollector can have other possible types, add them to lib/Facade.php:57.
Loading history...
103
            $this->bufferedCollector = new BufferedCollector(
104
                $this->getMetadataWrapperProvider(),
105
                $this->getDoctrineOrmCollector()
106
            );
107
        }
108
109
        return $this->bufferedCollector;
110
    }
111
112
    /**
113
     * @return MetadataWrapperProvider
114
     *
115
     * @throws \Exception
116
     */
117
    protected function getMetadataWrapperProvider(): MetadataWrapperProvider
118
    {
119
        $entityManager = $this->getEntityManager();
120
        if (!$this->metadataWrapperProvider instanceof MetadataWrapperProvider) {
0 ignored issues
show
$this->metadataWrapperProvider is always a sub-type of Xsolve\Associate\Metadata\MetadataWrapperProvider. If $this->metadataWrapperProvider can have other possible types, add them to lib/Facade.php:27.
Loading history...
121
            $this->metadataWrapperProvider = new MetadataWrapperProvider($entityManager);
122
        }
123
124
        return $this->metadataWrapperProvider;
125
    }
126
127
    /**
128
     * @return BasicAssociationCollectingStrategy
129
     */
130
    protected function getBasicAssociationCollectingStrategy(): BasicAssociationCollectingStrategy
131
    {
132
        if (!$this->basicAssociationCollectingStrategy instanceof BasicAssociationCollectingStrategy) {
0 ignored issues
show
$this->basicAssociationCollectingStrategy is always a sub-type of Xsolve\Associate\Associa...ationCollectingStrategy. If $this->basicAssociationCollectingStrategy can have other possible types, add them to lib/Facade.php:32.
Loading history...
133
            $this->basicAssociationCollectingStrategy = new BasicAssociationCollectingStrategy();
134
            $this->basicAssociationCollectingStrategy->addCollectionTraversalStrategy(
135
                new ArrayCollectionTraversalStrategy()
136
            );
137
            $this->basicAssociationCollectingStrategy->addCollectionTraversalStrategy(
138
                new TraversableCollectionTraversalStrategy()
139
            );
140
        }
141
142
        return $this->basicAssociationCollectingStrategy;
143
    }
144
145
    /**
146
     * @return DoctrineOrmAssociationCollectingStrategy
147
     */
148
    protected function getDoctrineOrmAssociationCollectingStrategy(): DoctrineOrmAssociationCollectingStrategy
149
    {
150
        if (!$this->doctrineOrmAssociationCollectingStrategy instanceof DoctrineOrmAssociationCollectingStrategy) {
0 ignored issues
show
$this->doctrineOrmAssociationCollectingStrategy is always a sub-type of Xsolve\Associate\Associa...ationCollectingStrategy. If $this->doctrineOrmAssociationCollectingStrategy can have other possible types, add them to lib/Facade.php:37.
Loading history...
151
            $this->doctrineOrmAssociationCollectingStrategy = new DoctrineOrmAssociationCollectingStrategy(
152
                $this->getMetadataWrapperProvider(),
153
                $this->getDoctrineOrmEntityLoader()
154
            );
155
            $this->doctrineOrmAssociationCollectingStrategy->addCollectionTraversalStrategy(
156
                new ArrayCollectionTraversalStrategy()
157
            );
158
            $this->doctrineOrmAssociationCollectingStrategy->addCollectionTraversalStrategy(
159
                new TraversableCollectionTraversalStrategy()
160
            );
161
            $this->doctrineOrmAssociationCollectingStrategy->addCollectionTraversalStrategy(
162
                new DoctrineOrmCollectionCollectionTraversalStrategy()
163
            );
164
        }
165
166
        return $this->doctrineOrmAssociationCollectingStrategy;
167
    }
168
169
    /**
170
     * @return DoctrineOrmEntityLoader
171
     */
172
    protected function getDoctrineOrmEntityLoader(): DoctrineOrmEntityLoader
173
    {
174
        if (!$this->doctrineOrmEntityLoader instanceof DoctrineOrmEntityLoader) {
0 ignored issues
show
$this->doctrineOrmEntityLoader is always a sub-type of Xsolve\Associate\Loader\DoctrineOrmEntityLoader. If $this->doctrineOrmEntityLoader can have other possible types, add them to lib/Facade.php:42.
Loading history...
175
            $this->doctrineOrmEntityLoader = new DoctrineOrmEntityLoader(
176
                $this->getMetadataWrapperProvider(),
177
                new BasicAssociationCollectingStrategy(),
178
                new DoctrineOrmUninitializedProxiesQueryExecutor(),
179
                new DoctrineOrmNonProxiedAssociationQueryExecutor()
180
            );
181
        }
182
183
        return $this->doctrineOrmEntityLoader;
184
    }
185
186
    /**
187
     * @throws \Exception
188
     */
189
    protected function assertEntityManagerAvailable()
190
    {
191
        if (!$this->entityManager instanceof EntityManagerInterface) {
192
            throw new \Exception('Entity manager not available.');
193
        }
194
    }
195
196
    /**
197
     * @return EntityManagerInterface
198
     *
199
     * @throws \Exception
200
     */
201
    protected function getEntityManager(): EntityManagerInterface
202
    {
203
        if (!$this->entityManager instanceof EntityManagerInterface) {
204
            throw new \Exception('Entity manager not available.');
205
        }
206
207
        return $this->entityManager;
208
    }
209
}
210