| Conditions | 6 |
| Paths | 4 |
| Total Lines | 76 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 29 | public function saveAssociations( |
||
| 30 | EntityManagerInterface $entityManager, |
||
| 31 | string $entityName, |
||
| 32 | EntityInterface $entity, |
||
| 33 | array $options = [], |
||
| 34 | array $collectionOptions = [] |
||
| 35 | ): void { |
||
| 36 | $options = array_replace_recursive($this->options, $options); |
||
| 37 | $collectionOptions = array_replace_recursive($this->collectionOptions, $collectionOptions); |
||
| 38 | |||
| 39 | /** @var ClassMetadata<EntityInterface> $classMetadata */ |
||
| 40 | $classMetadata = $this->getClassMetadata($entityManager, $entityName); |
||
| 41 | $mappings = $classMetadata->getAssociationMappings(); |
||
| 42 | |||
| 43 | $this->logger->info( |
||
| 44 | sprintf('Processing cascade save operations for for entity class \'%s\'', $entityName) |
||
| 45 | ); |
||
| 46 | |||
| 47 | foreach ($mappings as $mapping) { |
||
| 48 | if ( |
||
| 49 | !isset( |
||
| 50 | $mapping['targetEntity'], |
||
| 51 | $mapping['fieldName'], |
||
| 52 | $mapping['type'], |
||
| 53 | $mapping['isCascadePersist'] |
||
| 54 | ) |
||
| 55 | || true !== $mapping['isCascadePersist'] |
||
| 56 | ) { |
||
| 57 | // We only want to save associations that are configured to cascade persist |
||
| 58 | continue; |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->logger->info( |
||
| 62 | sprintf( |
||
| 63 | 'The entity field \'%s::%s\' is configured for cascade operations for target entity \'%s\'', |
||
| 64 | $entityName, |
||
| 65 | $mapping['fieldName'], |
||
| 66 | $mapping['targetEntity'] |
||
| 67 | ) |
||
| 68 | ); |
||
| 69 | |||
| 70 | /** @var ClassMetadata<EntityInterface> $targetMetadata */ |
||
| 71 | $targetMetadata = $this->getClassMetadata($entityManager, $mapping['targetEntity']); |
||
| 72 | |||
| 73 | $targetEntityOrCollection = $this->resolveTargetEntityOrCollection( |
||
| 74 | $entity, |
||
| 75 | $mapping['fieldName'], |
||
| 76 | $classMetadata, |
||
| 77 | $targetMetadata |
||
| 78 | ); |
||
| 79 | |||
| 80 | if (!$this->isValidAssociation($targetEntityOrCollection, $mapping)) { |
||
|
|
|||
| 81 | $errorMessage = sprintf( |
||
| 82 | 'The entity field \'%s::%s\' value could not be resolved', |
||
| 83 | $entityName, |
||
| 84 | $mapping['fieldName'] |
||
| 85 | ); |
||
| 86 | |||
| 87 | $this->logger->error($errorMessage); |
||
| 88 | |||
| 89 | throw new PersistenceException($errorMessage); |
||
| 90 | } |
||
| 91 | |||
| 92 | $this->logger->info( |
||
| 93 | sprintf( |
||
| 94 | 'Performing cascading save operations for field \'%s::%s\'', |
||
| 95 | $entityName, |
||
| 96 | $mapping['fieldName'] |
||
| 97 | ) |
||
| 98 | ); |
||
| 99 | |||
| 100 | $this->saveAssociation( |
||
| 101 | $entityManager, |
||
| 102 | $mapping['targetEntity'], |
||
| 103 | $targetEntityOrCollection, |
||
| 104 | (is_iterable($targetEntityOrCollection) ? $collectionOptions : $options) |
||
| 105 | ); |
||
| 141 |