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 |
||
| 29 | class ModelManager implements ModelManagerInterface |
||
| 30 | { |
||
| 31 | public const ID_SEPARATOR = '-'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ManagerRegistry |
||
| 35 | */ |
||
| 36 | protected $registry; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var PropertyAccessorInterface |
||
| 40 | */ |
||
| 41 | private $propertyAccessor; |
||
| 42 | |||
| 43 | public function __construct(ManagerRegistry $registry, ?PropertyAccessorInterface $propertyAccessor = null) |
||
| 44 | { |
||
| 45 | $this->registry = $registry; |
||
| 46 | $this->propertyAccessor = $propertyAccessor ?? PropertyAccess::createPropertyAccessor(); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritdoc} |
||
| 51 | */ |
||
| 52 | public function getMetadata($class) |
||
| 53 | { |
||
| 54 | return $this->getDocumentManager($class)->getMetadataFactory()->getMetadataFor($class); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Returns the model's metadata holding the fully qualified property, and the last |
||
| 59 | * property name. |
||
| 60 | * |
||
| 61 | * @param string $baseClass The base class of the model holding the fully qualified property |
||
| 62 | * @param string $propertyFullName The name of the fully qualified property (dot ('.') separated |
||
| 63 | * property string) |
||
| 64 | * |
||
| 65 | * @return array( |
||
|
|
|||
| 66 | * \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $parentMetadata, |
||
| 67 | * string $lastPropertyName, |
||
| 68 | * array $parentAssociationMappings |
||
| 69 | * ) |
||
| 70 | */ |
||
| 71 | public function getParentMetadataForProperty($baseClass, $propertyFullName) |
||
| 72 | { |
||
| 73 | $nameElements = explode('.', $propertyFullName); |
||
| 74 | $lastPropertyName = array_pop($nameElements); |
||
| 75 | $class = $baseClass; |
||
| 76 | $parentAssociationMappings = []; |
||
| 77 | |||
| 78 | foreach ($nameElements as $nameElement) { |
||
| 79 | $metadata = $this->getMetadata($class); |
||
| 80 | $parentAssociationMappings[] = $metadata->associationMappings[$nameElement]; |
||
| 81 | $class = $metadata->getAssociationTargetClass($nameElement); |
||
| 82 | } |
||
| 83 | |||
| 84 | return [$this->getMetadata($class), $lastPropertyName, $parentAssociationMappings]; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | public function hasMetadata($class) |
||
| 91 | { |
||
| 92 | return $this->getDocumentManager($class)->getMetadataFactory()->hasMetadataFor($class); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | public function getNewFieldDescriptionInstance($class, $name, array $options = []) |
||
| 99 | { |
||
| 100 | if (!\is_string($name)) { |
||
| 101 | throw new \RuntimeException('The name argument must be a string'); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!isset($options['route']['name'])) { |
||
| 105 | $options['route']['name'] = 'edit'; |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!isset($options['route']['parameters'])) { |
||
| 109 | $options['route']['parameters'] = []; |
||
| 110 | } |
||
| 111 | |||
| 112 | list($metadata, $propertyName, $parentAssociationMappings) = $this->getParentMetadataForProperty($class, $name); |
||
| 113 | |||
| 114 | $fieldDescription = new FieldDescription(); |
||
| 115 | $fieldDescription->setName($name); |
||
| 116 | $fieldDescription->setOptions($options); |
||
| 117 | $fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
||
| 118 | |||
| 119 | /* @var ClassMetadata */ |
||
| 120 | if (isset($metadata->associationMappings[$propertyName])) { |
||
| 121 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$propertyName]); |
||
| 122 | } |
||
| 123 | |||
| 124 | if (isset($metadata->fieldMappings[$propertyName])) { |
||
| 125 | $fieldDescription->setFieldMapping($metadata->fieldMappings[$propertyName]); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $fieldDescription; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function create($object) |
||
| 135 | { |
||
| 136 | $documentManager = $this->getDocumentManager($object); |
||
| 137 | $documentManager->persist($object); |
||
| 138 | $documentManager->flush(); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritdoc} |
||
| 143 | */ |
||
| 144 | public function update($object) |
||
| 145 | { |
||
| 146 | $documentManager = $this->getDocumentManager($object); |
||
| 147 | $documentManager->persist($object); |
||
| 148 | $documentManager->flush(); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritdoc} |
||
| 153 | */ |
||
| 154 | public function delete($object) |
||
| 155 | { |
||
| 156 | $documentManager = $this->getDocumentManager($object); |
||
| 157 | $documentManager->remove($object); |
||
| 158 | $documentManager->flush(); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | public function find($class, $id) |
||
| 165 | { |
||
| 166 | if (!isset($id)) { |
||
| 167 | return null; |
||
| 168 | } |
||
| 169 | |||
| 170 | $documentManager = $this->getDocumentManager($class); |
||
| 171 | |||
| 172 | if (is_numeric($id)) { |
||
| 173 | $value = $documentManager->getRepository($class)->find((int) $id); |
||
| 174 | |||
| 175 | if (!empty($value)) { |
||
| 176 | return $value; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | return $documentManager->getRepository($class)->find($id); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | public function findBy($class, array $criteria = []) |
||
| 187 | { |
||
| 188 | return $this->getDocumentManager($class)->getRepository($class)->findBy($criteria); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | */ |
||
| 194 | public function findOneBy($class, array $criteria = []) |
||
| 195 | { |
||
| 196 | return $this->getDocumentManager($class)->getRepository($class)->findOneBy($criteria); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param object|string $class |
||
| 201 | * |
||
| 202 | * @throw \RuntimeException |
||
| 203 | * |
||
| 204 | * @return \Doctrine\ODM\MongoDB\DocumentManager |
||
| 205 | */ |
||
| 206 | public function getDocumentManager($class) |
||
| 207 | { |
||
| 208 | if (\is_object($class)) { |
||
| 209 | $class = \get_class($class); |
||
| 210 | } |
||
| 211 | |||
| 212 | $dm = $this->registry->getManagerForClass($class); |
||
| 213 | |||
| 214 | if (!$dm) { |
||
| 215 | throw new \RuntimeException(sprintf('No document manager defined for class %s', $class)); |
||
| 216 | } |
||
| 217 | |||
| 218 | return $dm; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | public function getParentFieldDescription($parentAssociationMapping, $class) |
||
| 225 | { |
||
| 226 | $fieldName = $parentAssociationMapping['fieldName']; |
||
| 227 | |||
| 228 | $metadata = $this->getMetadata($class); |
||
| 229 | |||
| 230 | $associatingMapping = $metadata->associationMappings[$parentAssociationMapping]; |
||
| 231 | |||
| 232 | $fieldDescription = $this->getNewFieldDescriptionInstance($class, $fieldName); |
||
| 233 | $fieldDescription->setName($parentAssociationMapping); |
||
| 234 | $fieldDescription->setAssociationMapping($associatingMapping); |
||
| 235 | |||
| 236 | return $fieldDescription; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | public function createQuery($class, $alias = 'o') |
||
| 243 | { |
||
| 244 | $repository = $this->getDocumentManager($class)->getRepository($class); |
||
| 245 | |||
| 246 | return new ProxyQuery($repository->createQueryBuilder()); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | */ |
||
| 252 | public function executeQuery($query) |
||
| 253 | { |
||
| 254 | if ($query instanceof Builder) { |
||
| 255 | return $query->getQuery()->execute(); |
||
| 256 | } |
||
| 257 | |||
| 258 | return $query->execute(); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | public function getModelIdentifier($class) |
||
| 265 | { |
||
| 266 | return $this->getMetadata($class)->identifier; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * {@inheritdoc} |
||
| 271 | */ |
||
| 272 | public function getIdentifierValues($document) |
||
| 273 | { |
||
| 274 | return [$this->getDocumentManager($document)->getUnitOfWork()->getDocumentIdentifier($document)]; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * {@inheritdoc} |
||
| 279 | */ |
||
| 280 | public function getIdentifierFieldNames($class) |
||
| 281 | { |
||
| 282 | return [$this->getMetadata($class)->getIdentifier()]; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * {@inheritdoc} |
||
| 287 | */ |
||
| 288 | public function getNormalizedIdentifier($document) |
||
| 289 | { |
||
| 290 | if (null === $document) { |
||
| 291 | return null; |
||
| 292 | } |
||
| 293 | |||
| 294 | if (!\is_object($document)) { |
||
| 295 | throw new \RuntimeException('Invalid argument, object or null required'); |
||
| 296 | } |
||
| 297 | |||
| 298 | // the document is not managed |
||
| 299 | if (!$this->getDocumentManager($document)->getUnitOfWork()->isInIdentityMap($document)) { |
||
| 300 | return null; |
||
| 301 | } |
||
| 302 | |||
| 303 | $values = $this->getIdentifierValues($document); |
||
| 304 | |||
| 305 | return implode(self::ID_SEPARATOR, $values); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * {@inheritdoc} |
||
| 310 | */ |
||
| 311 | public function getUrlSafeIdentifier($document) |
||
| 312 | { |
||
| 313 | return $this->getNormalizedIdentifier($document); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * {@inheritdoc} |
||
| 318 | */ |
||
| 319 | public function addIdentifiersToQuery($class, ProxyQueryInterface $queryProxy, array $idx) |
||
| 320 | { |
||
| 321 | $queryBuilder = $queryProxy->getQueryBuilder(); |
||
| 322 | $queryBuilder->field('_id')->in($idx); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * {@inheritdoc} |
||
| 327 | */ |
||
| 328 | public function batchDelete($class, ProxyQueryInterface $queryProxy) |
||
| 329 | { |
||
| 330 | /** @var Query $queryBuilder */ |
||
| 331 | $queryBuilder = $queryProxy->getQuery(); |
||
| 332 | |||
| 333 | $documentManager = $this->getDocumentManager($class); |
||
| 334 | |||
| 335 | $i = 0; |
||
| 336 | foreach ($queryBuilder->execute() as $object) { |
||
| 337 | $documentManager->remove($object); |
||
| 338 | |||
| 339 | if (0 === (++$i % 20)) { |
||
| 340 | $documentManager->flush(); |
||
| 341 | $documentManager->clear(); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | $documentManager->flush(); |
||
| 346 | $documentManager->clear(); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * {@inheritdoc} |
||
| 351 | */ |
||
| 352 | public function getDataSourceIterator(DatagridInterface $datagrid, array $fields, $firstResult = null, $maxResult = null) |
||
| 353 | { |
||
| 354 | $datagrid->buildPager(); |
||
| 355 | $query = $datagrid->getQuery(); |
||
| 356 | |||
| 357 | $query->setFirstResult($firstResult); |
||
| 358 | $query->setMaxResults($maxResult); |
||
| 359 | |||
| 360 | return new DoctrineODMQuerySourceIterator($query instanceof ProxyQuery ? $query->getQuery() : $query, $fields); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | */ |
||
| 366 | public function getExportFields($class) |
||
| 367 | { |
||
| 368 | $metadata = $this->getDocumentManager($class)->getClassMetadata($class); |
||
| 369 | |||
| 370 | return $metadata->getFieldNames(); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * {@inheritdoc} |
||
| 375 | */ |
||
| 376 | public function getModelInstance($class) |
||
| 377 | { |
||
| 378 | if (!class_exists($class)) { |
||
| 379 | throw new \InvalidArgumentException(sprintf('Class "%s" not found', $class)); |
||
| 380 | } |
||
| 381 | |||
| 382 | $r = new \ReflectionClass($class); |
||
| 383 | if ($r->isAbstract()) { |
||
| 384 | throw new \InvalidArgumentException(sprintf('Cannot initialize abstract class: %s', $class)); |
||
| 385 | } |
||
| 386 | |||
| 387 | $constructor = $r->getConstructor(); |
||
| 388 | |||
| 389 | if (null !== $constructor && (!$constructor->isPublic() || $constructor->getNumberOfRequiredParameters() > 0)) { |
||
| 390 | return $r->newInstanceWithoutConstructor(); |
||
| 391 | } |
||
| 392 | |||
| 393 | return new $class(); |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * {@inheritdoc} |
||
| 398 | */ |
||
| 399 | public function getSortParameters(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid) |
||
| 400 | { |
||
| 401 | $values = $datagrid->getValues(); |
||
| 402 | |||
| 403 | if ($this->isFieldAlreadySorted($fieldDescription, $datagrid)) { |
||
| 404 | if ('ASC' === $values['_sort_order']) { |
||
| 405 | $values['_sort_order'] = 'DESC'; |
||
| 406 | } else { |
||
| 407 | $values['_sort_order'] = 'ASC'; |
||
| 408 | } |
||
| 409 | } else { |
||
| 410 | $values['_sort_order'] = 'ASC'; |
||
| 411 | } |
||
| 412 | |||
| 413 | $values['_sort_by'] = \is_string($fieldDescription->getOption('sortable')) ? $fieldDescription->getOption('sortable') : $fieldDescription->getName(); |
||
| 414 | |||
| 415 | return ['filter' => $values]; |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * {@inheritdoc} |
||
| 420 | */ |
||
| 421 | public function getPaginationParameters(DatagridInterface $datagrid, $page) |
||
| 422 | { |
||
| 423 | $values = $datagrid->getValues(); |
||
| 424 | |||
| 425 | if (isset($values['_sort_by']) && $values['_sort_by'] instanceof FieldDescriptionInterface) { |
||
| 426 | $values['_sort_by'] = $values['_sort_by']->getName(); |
||
| 427 | } |
||
| 428 | $values['_page'] = $page; |
||
| 429 | |||
| 430 | return ['filter' => $values]; |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * {@inheritdoc} |
||
| 435 | */ |
||
| 436 | public function getDefaultSortValues($class) |
||
| 437 | { |
||
| 438 | return [ |
||
| 439 | '_sort_order' => 'ASC', |
||
| 440 | '_sort_by' => $this->getModelIdentifier($class), |
||
| 441 | '_page' => 1, |
||
| 442 | '_per_page' => 25, |
||
| 443 | ]; |
||
| 444 | } |
||
| 445 | |||
| 446 | public function getDefaultPerPageOptions(string $class): array |
||
| 447 | { |
||
| 448 | return [10, 25, 50, 100, 250]; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * {@inheritdoc} |
||
| 453 | */ |
||
| 454 | public function modelTransform($class, $instance) |
||
| 455 | { |
||
| 456 | return $instance; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * {@inheritdoc} |
||
| 461 | */ |
||
| 462 | public function modelReverseTransform($class, array $array = []) |
||
| 463 | { |
||
| 464 | $instance = $this->getModelInstance($class); |
||
| 465 | $metadata = $this->getMetadata($class); |
||
| 466 | |||
| 467 | foreach ($array as $name => $value) { |
||
| 468 | // property or association ? |
||
| 469 | if (\array_key_exists($name, $metadata->fieldMappings)) { |
||
| 470 | $property = $metadata->fieldMappings[$name]['fieldName']; |
||
| 471 | } elseif (\array_key_exists($name, $metadata->associationMappings)) { |
||
| 472 | $property = $metadata->associationMappings[$name]['fieldName']; |
||
| 473 | } else { |
||
| 474 | $property = $name; |
||
| 475 | } |
||
| 476 | |||
| 477 | $this->propertyAccessor->setValue($instance, $property, $value); |
||
| 478 | } |
||
| 479 | |||
| 480 | return $instance; |
||
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * {@inheritdoc} |
||
| 485 | */ |
||
| 486 | public function getModelCollectionInstance($class) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * {@inheritdoc} |
||
| 493 | */ |
||
| 494 | public function collectionClear(&$collection) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * {@inheritdoc} |
||
| 501 | */ |
||
| 502 | public function collectionHasElement(&$collection, &$element) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * {@inheritdoc} |
||
| 509 | */ |
||
| 510 | public function collectionAddElement(&$collection, &$element) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * {@inheritdoc} |
||
| 517 | */ |
||
| 518 | public function collectionRemoveElement(&$collection, &$element) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * NEXT_MAJOR: Remove this method. |
||
| 525 | * |
||
| 526 | * @deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x, to be removed in 4.0.'. |
||
| 527 | * |
||
| 528 | * @param string $property |
||
| 529 | * |
||
| 530 | * @return mixed |
||
| 531 | */ |
||
| 532 | protected function camelize($property) |
||
| 533 | { |
||
| 534 | @trigger_error(sprintf( |
||
| 535 | 'Method "%s()" is deprecated since sonata-project/doctrine-mongodb-admin-bundle 3.x and will be removed in version 4.0.', |
||
| 536 | __METHOD__ |
||
| 537 | ), E_USER_DEPRECATED); |
||
| 538 | |||
| 539 | return str_replace(' ', '', ucwords(str_replace('_', ' ', $property))); |
||
| 540 | } |
||
| 541 | |||
| 542 | private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid): bool |
||
| 543 | { |
||
| 544 | $values = $datagrid->getValues(); |
||
| 545 | |||
| 546 | if (!isset($values['_sort_by']) || !$values['_sort_by'] instanceof FieldDescriptionInterface) { |
||
| 547 | return false; |
||
| 548 | } |
||
| 549 | |||
| 550 | return $values['_sort_by']->getName() === $fieldDescription->getName() |
||
| 551 | || $values['_sort_by']->getName() === $fieldDescription->getOption('sortable'); |
||
| 552 | } |
||
| 553 | } |
||
| 554 |
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.