Complex classes like ModelManager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ModelManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class ModelManager implements ModelManagerInterface, LockInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var RegistryInterface |
||
| 39 | */ |
||
| 40 | protected $registry; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var EntityManager[] |
||
| 44 | */ |
||
| 45 | protected $cache = array(); |
||
| 46 | |||
| 47 | const ID_SEPARATOR = '~'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param RegistryInterface $registry |
||
| 51 | */ |
||
| 52 | public function __construct(RegistryInterface $registry) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param string $class |
||
| 59 | * |
||
| 60 | * @return ClassMetadata |
||
| 61 | */ |
||
| 62 | public function getMetadata($class) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Returns the model's metadata holding the fully qualified property, and the last |
||
| 69 | * property name. |
||
| 70 | * |
||
| 71 | * @param string $baseClass The base class of the model holding the fully qualified property. |
||
| 72 | * @param string $propertyFullName The name of the fully qualified property (dot ('.') separated |
||
| 73 | * property string) |
||
| 74 | * |
||
| 75 | * @return array( |
||
|
|
|||
| 76 | * \Doctrine\ORM\Mapping\ClassMetadata $parentMetadata, |
||
| 77 | * string $lastPropertyName, |
||
| 78 | * array $parentAssociationMappings |
||
| 79 | * ) |
||
| 80 | */ |
||
| 81 | public function getParentMetadataForProperty($baseClass, $propertyFullName) |
||
| 82 | { |
||
| 83 | $nameElements = explode('.', $propertyFullName); |
||
| 84 | $lastPropertyName = array_pop($nameElements); |
||
| 85 | $class = $baseClass; |
||
| 86 | $parentAssociationMappings = array(); |
||
| 87 | |||
| 88 | foreach ($nameElements as $nameElement) { |
||
| 89 | $metadata = $this->getMetadata($class); |
||
| 90 | |||
| 91 | if (isset($metadata->associationMappings[$nameElement])) { |
||
| 92 | $parentAssociationMappings[] = $metadata->associationMappings[$nameElement]; |
||
| 93 | $class = $metadata->getAssociationTargetClass($nameElement); |
||
| 94 | } elseif (isset($metadata->embeddedClasses[$nameElement])) { |
||
| 95 | $parentAssociationMappings = array(); |
||
| 96 | $lastPropertyName = $propertyFullName; |
||
| 97 | $class = $baseClass; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | return array($this->getMetadata($class), $lastPropertyName, $parentAssociationMappings); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $class |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | public function hasMetadata($class) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * {@inheritdoc} |
||
| 116 | */ |
||
| 117 | public function getNewFieldDescriptionInstance($class, $name, array $options = array()) |
||
| 118 | { |
||
| 119 | if (!is_string($name)) { |
||
| 120 | throw new \RunTimeException('The name argument must be a string'); |
||
| 121 | } |
||
| 122 | |||
| 123 | if (!isset($options['route']['name'])) { |
||
| 124 | $options['route']['name'] = 'edit'; |
||
| 125 | } |
||
| 126 | |||
| 127 | if (!isset($options['route']['parameters'])) { |
||
| 128 | $options['route']['parameters'] = array(); |
||
| 129 | } |
||
| 130 | |||
| 131 | list($metadata, $propertyName, $parentAssociationMappings) = $this->getParentMetadataForProperty($class, $name); |
||
| 132 | |||
| 133 | $fieldDescription = new FieldDescription(); |
||
| 134 | $fieldDescription->setName($name); |
||
| 135 | $fieldDescription->setOptions($options); |
||
| 136 | $fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
||
| 137 | |||
| 138 | if (isset($metadata->associationMappings[$propertyName])) { |
||
| 139 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$propertyName]); |
||
| 140 | } |
||
| 141 | |||
| 142 | if (isset($metadata->fieldMappings[$propertyName])) { |
||
| 143 | $fieldDescription->setFieldMapping($metadata->fieldMappings[$propertyName]); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $fieldDescription; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * {@inheritdoc} |
||
| 151 | */ |
||
| 152 | public function create($object) |
||
| 153 | { |
||
| 154 | try { |
||
| 155 | $entityManager = $this->getEntityManager($object); |
||
| 156 | $entityManager->persist($object); |
||
| 157 | $entityManager->flush(); |
||
| 158 | } catch (\PDOException $e) { |
||
| 159 | throw new ModelManagerException(sprintf('Failed to create object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
| 160 | } catch (DBALException $e) { |
||
| 161 | throw new ModelManagerException(sprintf('Failed to create object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | public function update($object) |
||
| 169 | { |
||
| 170 | try { |
||
| 171 | $entityManager = $this->getEntityManager($object); |
||
| 172 | $entityManager->persist($object); |
||
| 173 | $entityManager->flush(); |
||
| 174 | } catch (\PDOException $e) { |
||
| 175 | throw new ModelManagerException(sprintf('Failed to update object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
| 176 | } catch (DBALException $e) { |
||
| 177 | throw new ModelManagerException(sprintf('Failed to update object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritdoc} |
||
| 183 | */ |
||
| 184 | public function delete($object) |
||
| 185 | { |
||
| 186 | try { |
||
| 187 | $entityManager = $this->getEntityManager($object); |
||
| 188 | $entityManager->remove($object); |
||
| 189 | $entityManager->flush(); |
||
| 190 | } catch (\PDOException $e) { |
||
| 191 | throw new ModelManagerException(sprintf('Failed to delete object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
| 192 | } catch (DBALException $e) { |
||
| 193 | throw new ModelManagerException(sprintf('Failed to delete object: %s', ClassUtils::getClass($object)), $e->getCode(), $e); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | public function getLockVersion($object) |
||
| 201 | { |
||
| 202 | $metadata = $this->getMetadata(ClassUtils::getClass($object)); |
||
| 203 | |||
| 204 | if (!$metadata->isVersioned) { |
||
| 205 | return; |
||
| 206 | } |
||
| 207 | |||
| 208 | return $metadata->reflFields[$metadata->versionField]->getValue($object); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | public function lock($object, $expectedVersion) |
||
| 215 | { |
||
| 216 | $metadata = $this->getMetadata(ClassUtils::getClass($object)); |
||
| 217 | |||
| 218 | if (!$metadata->isVersioned) { |
||
| 219 | return; |
||
| 220 | } |
||
| 221 | |||
| 222 | try { |
||
| 223 | $entityManager = $this->getEntityManager($object); |
||
| 224 | $entityManager->lock($object, LockMode::OPTIMISTIC, $expectedVersion); |
||
| 225 | } catch (OptimisticLockException $e) { |
||
| 226 | throw new LockException($e->getMessage(), $e->getCode(), $e); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * {@inheritdoc} |
||
| 232 | */ |
||
| 233 | public function find($class, $id) |
||
| 234 | { |
||
| 235 | if (!isset($id)) { |
||
| 236 | return; |
||
| 237 | } |
||
| 238 | |||
| 239 | $values = array_combine($this->getIdentifierFieldNames($class), explode(self::ID_SEPARATOR, $id)); |
||
| 240 | |||
| 241 | return $this->getEntityManager($class)->getRepository($class)->find($values); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritdoc} |
||
| 246 | */ |
||
| 247 | public function findBy($class, array $criteria = array()) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function findOneBy($class, array $criteria = array()) |
||
| 256 | { |
||
| 257 | return $this->getEntityManager($class)->getRepository($class)->findOneBy($criteria); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $class |
||
| 262 | * |
||
| 263 | * @return EntityManager |
||
| 264 | */ |
||
| 265 | public function getEntityManager($class) |
||
| 266 | { |
||
| 267 | if (is_object($class)) { |
||
| 268 | $class = get_class($class); |
||
| 269 | } |
||
| 270 | |||
| 271 | if (!isset($this->cache[$class])) { |
||
| 272 | $em = $this->registry->getManagerForClass($class); |
||
| 273 | |||
| 274 | if (!$em) { |
||
| 275 | throw new \RuntimeException(sprintf('No entity manager defined for class %s', $class)); |
||
| 276 | } |
||
| 277 | |||
| 278 | $this->cache[$class] = $em; |
||
| 279 | } |
||
| 280 | |||
| 281 | return $this->cache[$class]; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | public function getParentFieldDescription($parentAssociationMapping, $class) |
||
| 288 | { |
||
| 289 | $fieldName = $parentAssociationMapping['fieldName']; |
||
| 290 | |||
| 291 | $metadata = $this->getMetadata($class); |
||
| 292 | |||
| 293 | $associatingMapping = $metadata->associationMappings[$parentAssociationMapping]; |
||
| 294 | |||
| 295 | $fieldDescription = $this->getNewFieldDescriptionInstance($class, $fieldName); |
||
| 296 | $fieldDescription->setName($parentAssociationMapping); |
||
| 297 | $fieldDescription->setAssociationMapping($associatingMapping); |
||
| 298 | |||
| 299 | return $fieldDescription; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | public function createQuery($class, $alias = 'o') |
||
| 306 | { |
||
| 307 | $repository = $this->getEntityManager($class)->getRepository($class); |
||
| 308 | |||
| 309 | return new ProxyQuery($repository->createQueryBuilder($alias)); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritdoc} |
||
| 314 | */ |
||
| 315 | public function executeQuery($query) |
||
| 316 | { |
||
| 317 | if ($query instanceof QueryBuilder) { |
||
| 318 | return $query->getQuery()->execute(); |
||
| 319 | } |
||
| 320 | |||
| 321 | return $query->execute(); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritdoc} |
||
| 326 | */ |
||
| 327 | public function getModelIdentifier($class) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * {@inheritdoc} |
||
| 334 | */ |
||
| 335 | public function getIdentifierValues($entity) |
||
| 336 | { |
||
| 337 | // Fix code has an impact on performance, so disable it ... |
||
| 338 | //$entityManager = $this->getEntityManager($entity); |
||
| 339 | //if (!$entityManager->getUnitOfWork()->isInIdentityMap($entity)) { |
||
| 340 | // throw new \RuntimeException('Entities passed to the choice field must be managed'); |
||
| 341 | //} |
||
| 342 | |||
| 343 | $class = ClassUtils::getClass($entity); |
||
| 344 | $metadata = $this->getMetadata($class); |
||
| 345 | $platform = $this->getEntityManager($class)->getConnection()->getDatabasePlatform(); |
||
| 346 | |||
| 347 | $identifiers = array(); |
||
| 348 | |||
| 349 | foreach ($metadata->getIdentifierValues($entity) as $name => $value) { |
||
| 350 | if (!is_object($value)) { |
||
| 351 | $identifiers[] = $value; |
||
| 352 | continue; |
||
| 353 | } |
||
| 354 | |||
| 355 | $fieldType = $metadata->getTypeOfField($name); |
||
| 356 | $type = Type::getType($fieldType); |
||
|
1 ignored issue
–
show
|
|||
| 357 | if ($type) { |
||
| 358 | $identifiers[] = $type->convertToDatabaseValue($value, $platform); |
||
| 359 | continue; |
||
| 360 | } |
||
| 361 | |||
| 362 | $metadata = $this->getMetadata(ClassUtils::getClass($value)); |
||
| 363 | |||
| 364 | foreach ($metadata->getIdentifierValues($value) as $value) { |
||
| 365 | $identifiers[] = $value; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | return $identifiers; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * {@inheritdoc} |
||
| 374 | */ |
||
| 375 | public function getIdentifierFieldNames($class) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * {@inheritdoc} |
||
| 382 | */ |
||
| 383 | public function getNormalizedIdentifier($entity) |
||
| 384 | { |
||
| 385 | if (is_scalar($entity)) { |
||
| 386 | throw new \RunTimeException('Invalid argument, object or null required'); |
||
| 387 | } |
||
| 388 | |||
| 389 | // the entities is not managed |
||
| 390 | if (!$entity || !$this->getEntityManager($entity)->getUnitOfWork()->isInIdentityMap($entity)) { |
||
| 391 | return; |
||
| 392 | } |
||
| 393 | |||
| 394 | $values = $this->getIdentifierValues($entity); |
||
| 395 | |||
| 396 | if (count($values) === 0) { |
||
| 397 | return; |
||
| 398 | } |
||
| 399 | |||
| 400 | return implode(self::ID_SEPARATOR, $values); |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * {@inheritdoc} |
||
| 405 | * |
||
| 406 | * The ORM implementation does nothing special but you still should use |
||
| 407 | * this method when using the id in a URL to allow for future improvements. |
||
| 408 | */ |
||
| 409 | public function getUrlsafeIdentifier($entity) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * {@inheritdoc} |
||
| 416 | */ |
||
| 417 | public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, array $idx) |
||
| 418 | { |
||
| 419 | $fieldNames = $this->getIdentifierFieldNames($class); |
||
| 420 | $qb = $queryProxy->getQueryBuilder(); |
||
| 421 | |||
| 422 | $prefix = uniqid(); |
||
| 423 | $sqls = array(); |
||
| 424 | foreach ($idx as $pos => $id) { |
||
| 425 | $ids = explode(self::ID_SEPARATOR, $id); |
||
| 426 | |||
| 427 | $ands = array(); |
||
| 428 | foreach ($fieldNames as $posName => $name) { |
||
| 429 | $parameterName = sprintf('field_%s_%s_%d', $prefix, $name, $pos); |
||
| 430 | $ands[] = sprintf('%s.%s = :%s', $qb->getRootAlias(), $name, $parameterName); |
||
| 431 | $qb->setParameter($parameterName, $ids[$posName]); |
||
| 432 | } |
||
| 433 | |||
| 434 | $sqls[] = implode(' AND ', $ands); |
||
| 435 | } |
||
| 436 | |||
| 437 | $qb->andWhere(sprintf('( %s )', implode(' OR ', $sqls))); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * {@inheritdoc} |
||
| 442 | */ |
||
| 443 | public function batchDelete($class, ProxyQueryInterface $queryProxy) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | */ |
||
| 472 | public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * {@inheritdoc} |
||
| 492 | */ |
||
| 493 | public function getExportFields($class) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * {@inheritdoc} |
||
| 502 | */ |
||
| 503 | public function getModelInstance($class) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * {@inheritdoc} |
||
| 515 | */ |
||
| 516 | public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * {@inheritdoc} |
||
| 537 | */ |
||
| 538 | public function getPaginationParameters(DatagridInterface $datagrid, $page) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * {@inheritdoc} |
||
| 550 | */ |
||
| 551 | public function getDefaultSortValues($class) |
||
| 552 | { |
||
| 553 | return array( |
||
| 554 | '_sort_order' => 'ASC', |
||
| 555 | '_sort_by' => implode(',', $this->getModelIdentifier($class)), |
||
| 556 | '_page' => 1, |
||
| 557 | '_per_page' => 25, |
||
| 558 | ); |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * {@inheritdoc} |
||
| 563 | */ |
||
| 564 | public function modelTransform($class, $instance) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * {@inheritdoc} |
||
| 571 | */ |
||
| 572 | public function modelReverseTransform($class, array $array = array()) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * method taken from PropertyPath. |
||
| 619 | * |
||
| 620 | * @param string $property |
||
| 621 | * |
||
| 622 | * @return mixed |
||
| 623 | */ |
||
| 624 | protected function camelize($property) |
||
| 628 | |||
| 629 | /** |
||
| 630 | * {@inheritdoc} |
||
| 631 | */ |
||
| 632 | public function getModelCollectionInstance($class) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * {@inheritdoc} |
||
| 639 | */ |
||
| 640 | public function collectionClear(&$collection) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * {@inheritdoc} |
||
| 647 | */ |
||
| 648 | public function collectionHasElement(&$collection, &$element) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * {@inheritdoc} |
||
| 655 | */ |
||
| 656 | public function collectionAddElement(&$collection, &$element) |
||
| 660 | |||
| 661 | /** |
||
| 662 | * {@inheritdoc} |
||
| 663 | */ |
||
| 664 | public function collectionRemoveElement(&$collection, &$element) |
||
| 668 | } |
||
| 669 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.