| Total Complexity | 48 |
| Total Lines | 385 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 28 | final class ORM implements ORMInterface |
||
| 29 | { |
||
| 30 | /** @var CommandGenerator */ |
||
| 31 | private $generator; |
||
| 32 | |||
| 33 | /** @var FactoryInterface */ |
||
| 34 | private $factory; |
||
| 35 | |||
| 36 | /** @var PromiseFactoryInterface|null */ |
||
| 37 | private $promiseFactory; |
||
| 38 | |||
| 39 | /** @var HeapInterface */ |
||
| 40 | private $heap; |
||
| 41 | |||
| 42 | /** @var SchemaInterface|null */ |
||
| 43 | private $schema; |
||
| 44 | |||
| 45 | /** @var MapperInterface[] */ |
||
| 46 | private $mappers = []; |
||
| 47 | |||
| 48 | /** @var RepositoryInterface[] */ |
||
| 49 | private $repositories = []; |
||
| 50 | |||
| 51 | /** @var RelationMap[] */ |
||
| 52 | private $relmaps = []; |
||
| 53 | |||
| 54 | /** @var array */ |
||
| 55 | private $indexes = []; |
||
| 56 | |||
| 57 | /** @var SourceInterface[] */ |
||
| 58 | private $sources = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param FactoryInterface $factory |
||
| 62 | * @param SchemaInterface|null $schema |
||
| 63 | */ |
||
| 64 | public function __construct(FactoryInterface $factory, SchemaInterface $schema = null) |
||
| 65 | { |
||
| 66 | $this->generator = new CommandGenerator(); |
||
| 67 | $this->factory = $factory; |
||
| 68 | $this->schema = $schema; |
||
| 69 | |||
| 70 | $this->heap = new Heap(); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Automatically resolve role based on object name or instance. |
||
| 75 | * |
||
| 76 | * @param string|object $entity |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | public function resolveRole($entity): string |
||
| 80 | { |
||
| 81 | if (is_object($entity)) { |
||
| 82 | $node = $this->getHeap()->get($entity); |
||
| 83 | if (!is_null($node)) { |
||
| 84 | return $node->getRole(); |
||
| 85 | } |
||
| 86 | |||
| 87 | $class = get_class($entity); |
||
| 88 | if (!$this->schema->defines($class)) { |
||
|
|
|||
| 89 | throw new ORMException("Unable to resolve role of `$class`"); |
||
| 90 | } |
||
| 91 | |||
| 92 | $entity = $class; |
||
| 93 | } |
||
| 94 | |||
| 95 | return $this->schema->resolveAlias($entity); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @inheritdoc |
||
| 100 | */ |
||
| 101 | public function get(string $role, string $key, $value, bool $load = true) |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @inheritdoc |
||
| 117 | */ |
||
| 118 | public function make(string $role, array $data = [], int $node = Node::NEW) |
||
| 119 | { |
||
| 120 | $m = $this->getMapper($role); |
||
| 121 | |||
| 122 | // unique entity identifier |
||
| 123 | $pk = $this->schema->define($role, Schema::PRIMARY_KEY); |
||
| 124 | $id = $data[$pk] ?? null; |
||
| 125 | |||
| 126 | if ($node !== Node::NEW && !empty($id)) { |
||
| 127 | $e = $this->heap->find($role, $pk, $id); |
||
| 128 | if ($e !== null) { |
||
| 129 | $node = $this->heap->get($e); |
||
| 130 | |||
| 131 | // entity already been loaded, let's update it's relations with new context |
||
| 132 | return $m->hydrate($e, $this->getRelationMap($role)->init($node, $data)); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | // init entity class and prepared (typecasted) data |
||
| 137 | list($e, $prepared) = $m->init($data); |
||
| 138 | |||
| 139 | $node = new Node($node, $prepared, $m->getRole()); |
||
| 140 | |||
| 141 | $this->heap->attach($e, $node, $this->getIndexes($m->getRole())); |
||
| 142 | |||
| 143 | // hydrate entity with it's data, relations and proxies |
||
| 144 | return $m->hydrate($e, $this->getRelationMap($role)->init($node, $prepared)); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @inheritdoc |
||
| 149 | */ |
||
| 150 | public function withFactory(FactoryInterface $factory): ORMInterface |
||
| 151 | { |
||
| 152 | $orm = clone $this; |
||
| 153 | $orm->factory = $factory; |
||
| 154 | |||
| 155 | return $orm; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @inheritdoc |
||
| 160 | */ |
||
| 161 | public function getFactory(): FactoryInterface |
||
| 162 | { |
||
| 163 | return $this->factory; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @inheritdoc |
||
| 168 | */ |
||
| 169 | public function withSchema(SchemaInterface $schema): ORMInterface |
||
| 170 | { |
||
| 171 | $orm = clone $this; |
||
| 172 | $orm->schema = $schema; |
||
| 173 | |||
| 174 | return $orm; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @inheritdoc |
||
| 179 | */ |
||
| 180 | public function getSchema(): SchemaInterface |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @inheritdoc |
||
| 191 | */ |
||
| 192 | public function withHeap(HeapInterface $heap): ORMInterface |
||
| 193 | { |
||
| 194 | $orm = clone $this; |
||
| 195 | $orm->heap = $heap; |
||
| 196 | |||
| 197 | return $orm; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritdoc |
||
| 202 | */ |
||
| 203 | public function getHeap(): HeapInterface |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @inheritdoc |
||
| 210 | */ |
||
| 211 | public function getMapper($entity): MapperInterface |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @inheritdoc |
||
| 223 | */ |
||
| 224 | public function getRepository($entity): RepositoryInterface |
||
| 225 | { |
||
| 226 | $role = $this->resolveRole($entity); |
||
| 227 | if (isset($this->repositories[$role])) { |
||
| 228 | return $this->repositories[$role]; |
||
| 229 | } |
||
| 230 | |||
| 231 | $repository = $this->getSchema()->define($role, Schema::REPOSITORY) ?? Repository::class; |
||
| 232 | $params = ['orm' => $this, 'role' => $role]; |
||
| 233 | |||
| 234 | if ($this->getSchema()->define($role, Schema::TABLE) !== null) { |
||
| 235 | $params['select'] = new Select($this, $role); |
||
| 236 | $params['select']->constrain($this->getSource($role)->getConstrain()); |
||
| 237 | } |
||
| 238 | |||
| 239 | return $this->repositories[$role] = $this->factory->make($repository, $params); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @inheritdoc |
||
| 244 | */ |
||
| 245 | public function getSource(string $role): SourceInterface |
||
| 246 | { |
||
| 247 | if (isset($this->sources[$role])) { |
||
| 248 | return $this->sources[$role]; |
||
| 249 | } |
||
| 250 | |||
| 251 | $source = $this->schema->define($role, Schema::SOURCE) ?? Source::class; |
||
| 252 | if ($source !== Source::class) { |
||
| 253 | // custom implementation |
||
| 254 | return $this->factory->make($source, ['orm' => $this, 'role' => $role]); |
||
| 255 | } |
||
| 256 | |||
| 257 | $source = new Source( |
||
| 258 | $this->factory->database($this->schema->define($role, Schema::DATABASE)), |
||
| 259 | $this->schema->define($role, Schema::TABLE) |
||
| 260 | ); |
||
| 261 | |||
| 262 | $constrain = $this->schema->define($role, Schema::CONSTRAIN); |
||
| 263 | if ($constrain !== null) { |
||
| 264 | $source = $source->withConstrain( |
||
| 265 | is_object($constrain) ? $constrain : $this->factory->make($constrain) |
||
| 266 | ); |
||
| 267 | } |
||
| 268 | |||
| 269 | return $this->sources[$role] = $source; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Overlay existing promise factory. |
||
| 274 | * |
||
| 275 | * @param PromiseFactoryInterface $promiseFactory |
||
| 276 | * @return ORM |
||
| 277 | */ |
||
| 278 | public function withPromiseFactory(PromiseFactoryInterface $promiseFactory): self |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @inheritdoc |
||
| 288 | * |
||
| 289 | * Returns references by default. |
||
| 290 | */ |
||
| 291 | public function promise(string $role, array $scope) |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @inheritdoc |
||
| 307 | */ |
||
| 308 | public function queueStore($entity, int $mode = TransactionInterface::MODE_CASCADE): ContextCarrierInterface |
||
| 309 | { |
||
| 310 | if ($entity instanceof ReferenceInterface) { |
||
| 311 | // we do not expect to store promises |
||
| 312 | return new Nil(); |
||
| 313 | } |
||
| 314 | |||
| 315 | $mapper = $this->getMapper($entity); |
||
| 316 | |||
| 317 | $node = $this->heap->get($entity); |
||
| 318 | if (is_null($node)) { |
||
| 319 | // automatic entity registration |
||
| 320 | $node = new Node(Node::NEW, [], $mapper->getRole()); |
||
| 321 | $this->heap->attach($entity, $node); |
||
| 322 | } |
||
| 323 | |||
| 324 | $cmd = $this->generator->generateStore($mapper, $entity, $node); |
||
| 325 | if ($mode != TransactionInterface::MODE_CASCADE) { |
||
| 326 | return $cmd; |
||
| 327 | } |
||
| 328 | |||
| 329 | $rMap = $this->getRelationMap($node->getRole()); |
||
| 330 | if ($rMap === null) { |
||
| 331 | return $cmd; |
||
| 332 | } |
||
| 333 | |||
| 334 | // generate set of commands required to store entity relations |
||
| 335 | return $rMap->queueRelations( |
||
| 336 | $cmd, |
||
| 337 | $entity, |
||
| 338 | $node, |
||
| 339 | $mapper->extract($entity) |
||
| 340 | ); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @inheritdoc |
||
| 345 | */ |
||
| 346 | public function queueDelete($entity, int $mode = TransactionInterface::MODE_CASCADE): CommandInterface |
||
| 347 | { |
||
| 348 | $node = $this->heap->get($entity); |
||
| 349 | if ($entity instanceof ReferenceInterface || is_null($node)) { |
||
| 350 | // nothing to do, what about promises? |
||
| 351 | return new Nil(); |
||
| 352 | } |
||
| 353 | |||
| 354 | // currently we rely on db to delete all nested records (or soft deletes) |
||
| 355 | return $this->generator->generateDelete($this->getMapper($node->getRole()), $entity, $node); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Reset related objects cache. |
||
| 360 | */ |
||
| 361 | public function __clone() |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Get list of keys entity must be indexed in a Heap by. |
||
| 372 | * |
||
| 373 | * @param string $role |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | protected function getIndexes(string $role): array |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get relation map associated with the given class. |
||
| 390 | * |
||
| 391 | * @param string $entity |
||
| 392 | * @return RelationMap|null |
||
| 393 | */ |
||
| 394 | protected function getRelationMap($entity): ?RelationMap |
||
| 413 | } |
||
| 414 | } |
||
| 415 |
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.