Total Complexity | 54 |
Total Lines | 401 |
Duplicated Lines | 0 % |
Changes | 12 | ||
Bugs | 0 | Features | 0 |
Complex classes like ORM often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ORM, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | final class ORM implements ORMInterface |
||
30 | { |
||
31 | /** @var CommandGenerator */ |
||
32 | private $generator; |
||
33 | |||
34 | /** @var FactoryInterface */ |
||
35 | private $factory; |
||
36 | |||
37 | /** @var PromiseFactoryInterface|null */ |
||
38 | private $promiseFactory; |
||
39 | |||
40 | /** @var HeapInterface */ |
||
41 | private $heap; |
||
42 | |||
43 | /** @var SchemaInterface|null */ |
||
44 | private $schema; |
||
45 | |||
46 | /** @var MapperInterface[] */ |
||
47 | private $mappers = []; |
||
48 | |||
49 | /** @var RepositoryInterface[] */ |
||
50 | private $repositories = []; |
||
51 | |||
52 | /** @var RelationMap[] */ |
||
53 | private $relmaps = []; |
||
54 | |||
55 | /** @var array */ |
||
56 | private $indexes = []; |
||
57 | |||
58 | /** @var SourceInterface[] */ |
||
59 | private $sources = []; |
||
60 | |||
61 | /** |
||
62 | * @param FactoryInterface $factory |
||
63 | * @param SchemaInterface|null $schema |
||
64 | */ |
||
65 | public function __construct(FactoryInterface $factory, SchemaInterface $schema = null) |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Reset related objects cache. |
||
76 | */ |
||
77 | public function __clone() |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * @return array |
||
89 | */ |
||
90 | public function __debugInfo() |
||
91 | { |
||
92 | return [ |
||
93 | 'schema' => $this->schema, |
||
94 | ]; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Automatically resolve role based on object name or instance. |
||
99 | * |
||
100 | * @param object|string $entity |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | public function resolveRole($entity): string |
||
105 | { |
||
106 | if (is_object($entity)) { |
||
107 | $node = $this->getHeap()->get($entity); |
||
108 | if ($node !== null) { |
||
109 | return $node->getRole(); |
||
110 | } |
||
111 | |||
112 | $class = get_class($entity); |
||
113 | if (!$this->schema->defines($class)) { |
||
|
|||
114 | throw new ORMException("Unable to resolve role of `$class`"); |
||
115 | } |
||
116 | |||
117 | $entity = $class; |
||
118 | } |
||
119 | |||
120 | return $this->schema->resolveAlias($entity); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | public function get(string $role, array $scope, bool $load = true) |
||
127 | { |
||
128 | $role = $this->resolveRole($role); |
||
129 | $e = $this->heap->find($role, $scope); |
||
130 | |||
131 | if ($e !== null) { |
||
132 | return $e; |
||
133 | } |
||
134 | |||
135 | if (!$load) { |
||
136 | return null; |
||
137 | } |
||
138 | |||
139 | return $this->getRepository($role)->findOne($scope); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | public function make(string $role, array $data = [], int $node = Node::NEW) |
||
146 | { |
||
147 | $role = $this->resolveRole($role); |
||
148 | $m = $this->getMapper($role); |
||
149 | |||
150 | // unique entity identifier |
||
151 | $pk = $this->schema->define($role, Schema::PRIMARY_KEY); |
||
152 | $id = $data[$pk] ?? null; |
||
153 | |||
154 | if ($node !== Node::NEW && $id !== null) { |
||
155 | $e = $this->heap->find($role, [$pk => $id]); |
||
156 | |||
157 | if ($e !== null) { |
||
158 | $node = $this->heap->get($e); |
||
159 | |||
160 | // new set of data and relations always overwrite entity state |
||
161 | return $m->hydrate( |
||
162 | $e, |
||
163 | $this->getRelationMap($role)->init($node, $data) |
||
164 | ); |
||
165 | } |
||
166 | } |
||
167 | |||
168 | // init entity class and prepared (typecasted) data |
||
169 | [$e, $prepared] = $m->init($data); |
||
170 | |||
171 | $node = new Node($node, $prepared, $m->getRole()); |
||
172 | |||
173 | $this->heap->attach($e, $node, $this->getIndexes($m->getRole())); |
||
174 | |||
175 | // hydrate entity with it's data, relations and proxies |
||
176 | return $m->hydrate( |
||
177 | $e, |
||
178 | $this->getRelationMap($role)->init($node, $prepared) |
||
179 | ); |
||
180 | } |
||
181 | |||
182 | /** @deprecated since Cycle ORM v1.8, this method will be removed in future releases. Use method with instead. */ |
||
183 | public function withFactory(FactoryInterface $factory): ORMInterface |
||
184 | { |
||
185 | return $this->with(null, $factory); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @inheritdoc |
||
190 | */ |
||
191 | public function getFactory(): FactoryInterface |
||
192 | { |
||
193 | return $this->factory; |
||
194 | } |
||
195 | |||
196 | /** @deprecated since Cycle ORM v1.8, this method will be removed in future releases. Use method with instead. */ |
||
197 | public function withSchema(SchemaInterface $schema): ORMInterface |
||
198 | { |
||
199 | return $this->with($schema); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @inheritdoc |
||
204 | */ |
||
205 | public function getSchema(): SchemaInterface |
||
206 | { |
||
207 | if ($this->schema === null) { |
||
208 | throw new ORMException('ORM is not configured, schema is missing'); |
||
209 | } |
||
210 | |||
211 | return $this->schema; |
||
212 | } |
||
213 | |||
214 | /** @deprecated since Cycle ORM v1.8, this method will be removed in future releases. Use method with instead. */ |
||
215 | public function withHeap(HeapInterface $heap): ORMInterface |
||
216 | { |
||
217 | return $this->with(null, null, $heap); |
||
218 | } |
||
219 | |||
220 | public function with( |
||
221 | ?SchemaInterface $schema = null, |
||
222 | ?FactoryInterface $factory = null, |
||
223 | ?HeapInterface $heap = null |
||
224 | ): ORMInterface { |
||
225 | $orm = clone $this; |
||
226 | |||
227 | if ($schema !== null) { |
||
228 | $orm->schema = $schema; |
||
229 | } |
||
230 | if ($factory !== null) { |
||
231 | $orm->factory = $factory; |
||
232 | } |
||
233 | if ($heap !== null) { |
||
234 | $orm->heap = $heap; |
||
235 | } |
||
236 | |||
237 | return $orm; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @inheritdoc |
||
242 | */ |
||
243 | public function getHeap(): HeapInterface |
||
244 | { |
||
245 | return $this->heap; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @inheritdoc |
||
250 | */ |
||
251 | public function getMapper($entity): MapperInterface |
||
252 | { |
||
253 | $role = $this->resolveRole($entity); |
||
254 | if (isset($this->mappers[$role])) { |
||
255 | return $this->mappers[$role]; |
||
256 | } |
||
257 | |||
258 | return $this->mappers[$role] = $this->factory->mapper($this, $this->schema, $role); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @inheritdoc |
||
263 | */ |
||
264 | public function getRepository($entity): RepositoryInterface |
||
265 | { |
||
266 | $role = $this->resolveRole($entity); |
||
267 | if (isset($this->repositories[$role])) { |
||
268 | return $this->repositories[$role]; |
||
269 | } |
||
270 | |||
271 | $select = null; |
||
272 | |||
273 | if ($this->schema->define($role, Schema::TABLE) !== null) { |
||
274 | $select = new Select($this, $role); |
||
275 | $select->scope($this->getSource($role)->getConstrain()); |
||
276 | } |
||
277 | |||
278 | return $this->repositories[$role] = $this->factory->repository($this, $this->schema, $role, $select); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @inheritdoc |
||
283 | */ |
||
284 | public function getSource(string $role): SourceInterface |
||
285 | { |
||
286 | if (isset($this->sources[$role])) { |
||
287 | return $this->sources[$role]; |
||
288 | } |
||
289 | |||
290 | return $this->sources[$role] = $this->factory->source($this, $this->schema, $role); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Overlay existing promise factory. |
||
295 | * |
||
296 | * @param PromiseFactoryInterface $promiseFactory |
||
297 | * |
||
298 | * @return ORM |
||
299 | */ |
||
300 | public function withPromiseFactory(PromiseFactoryInterface $promiseFactory = null): self |
||
301 | { |
||
302 | $orm = clone $this; |
||
303 | $orm->promiseFactory = $promiseFactory; |
||
304 | |||
305 | return $orm; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @inheritdoc |
||
310 | * |
||
311 | * Returns references by default. |
||
312 | */ |
||
313 | public function promise(string $role, array $scope) |
||
314 | { |
||
315 | if (\count($scope) === 1) { |
||
316 | $e = $this->heap->find($role, $scope); |
||
317 | if ($e !== null) { |
||
318 | return $e; |
||
319 | } |
||
320 | } |
||
321 | |||
322 | if ($this->promiseFactory !== null) { |
||
323 | return $this->promiseFactory->promise($this, $role, $scope); |
||
324 | } |
||
325 | |||
326 | return new Reference($role, $scope); |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * @inheritdoc |
||
331 | */ |
||
332 | public function queueStore($entity, int $mode = TransactionInterface::MODE_CASCADE): ContextCarrierInterface |
||
333 | { |
||
334 | if ($entity instanceof PromiseInterface && $entity->__loaded()) { |
||
335 | $entity = $entity->__resolve(); |
||
336 | } |
||
337 | |||
338 | if ($entity instanceof ReferenceInterface) { |
||
339 | // we do not expect to store promises |
||
340 | return new Nil(); |
||
341 | } |
||
342 | |||
343 | $mapper = $this->getMapper($entity); |
||
344 | |||
345 | $node = $this->heap->get($entity); |
||
346 | if ($node === null) { |
||
347 | // automatic entity registration |
||
348 | $node = new Node(Node::NEW, [], $mapper->getRole()); |
||
349 | $this->heap->attach($entity, $node); |
||
350 | } |
||
351 | |||
352 | $cmd = $this->generator->generateStore($mapper, $entity, $node); |
||
353 | if ($mode !== TransactionInterface::MODE_CASCADE) { |
||
354 | return $cmd; |
||
355 | } |
||
356 | |||
357 | if ($this->schema->define($node->getRole(), Schema::RELATIONS) === []) { |
||
358 | return $cmd; |
||
359 | } |
||
360 | |||
361 | // generate set of commands required to store entity relations |
||
362 | return $this->getRelationMap($node->getRole())->queueRelations( |
||
363 | $cmd, |
||
364 | $entity, |
||
365 | $node, |
||
366 | $mapper->extract($entity) |
||
367 | ); |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * @inheritdoc |
||
372 | */ |
||
373 | public function queueDelete($entity, int $mode = TransactionInterface::MODE_CASCADE): CommandInterface |
||
374 | { |
||
375 | if ($entity instanceof PromiseInterface && $entity->__loaded()) { |
||
376 | $entity = $entity->__resolve(); |
||
377 | } |
||
378 | |||
379 | $node = $this->heap->get($entity); |
||
380 | if ($entity instanceof ReferenceInterface || $node === null) { |
||
381 | // nothing to do, what about promises? |
||
382 | return new Nil(); |
||
383 | } |
||
384 | |||
385 | // currently we rely on db to delete all nested records (or soft deletes) |
||
386 | return $this->generator->generateDelete($this->getMapper($node->getRole()), $entity, $node); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * Get list of keys entity must be indexed in a Heap by. |
||
391 | * |
||
392 | * @param string $role |
||
393 | * |
||
394 | * @return array |
||
395 | */ |
||
396 | protected function getIndexes(string $role): array |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Get relation map associated with the given class. |
||
410 | * |
||
411 | * @param string $entity |
||
412 | * |
||
413 | * @return RelationMap |
||
414 | */ |
||
415 | protected function getRelationMap($entity): RelationMap |
||
430 | } |
||
431 | } |
||
432 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.