Conditions | 6 |
Paths | 4 |
Total Lines | 78 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 deleteAssociations( |
||
30 | EntityManagerInterface $entityManager, |
||
31 | string $entityName, |
||
32 | EntityInterface $entity, |
||
33 | array $deleteOptions = [], |
||
34 | array $deleteCollectionOptions = [] |
||
35 | ): void { |
||
36 | $deleteOptions = array_replace_recursive($this->options, $deleteOptions); |
||
37 | $deleteCollectionOptions = array_replace_recursive($this->collectionOptions, $deleteCollectionOptions); |
||
38 | |||
39 | /** @var ClassMetadata<EntityInterface> $classMetadata */ |
||
40 | $classMetadata = $this->getClassMetadata($entityManager, $entityName); |
||
41 | |||
42 | /** @var array<string, mixed> $mappings */ |
||
43 | $mappings = $classMetadata->getAssociationMappings(); |
||
44 | |||
45 | $this->logger->info( |
||
46 | sprintf('Processing cascade delete operations for for entity class \'%s\'', $entityName) |
||
47 | ); |
||
48 | |||
49 | foreach ($mappings as $mapping) { |
||
50 | if ( |
||
51 | !isset( |
||
52 | $mapping['targetEntity'], |
||
53 | $mapping['fieldName'], |
||
54 | $mapping['type'], |
||
55 | $mapping['isCascadeRemove'] |
||
56 | ) |
||
57 | || true !== $mapping['isCascadeRemove'] |
||
58 | ) { |
||
59 | // We only want to save associations that are configured to cascade delete/remove |
||
60 | continue; |
||
61 | } |
||
62 | |||
63 | $this->logger->info( |
||
64 | sprintf( |
||
65 | 'The entity field \'%s::%s\' is configured for cascade delete operations for target entity \'%s\'', |
||
66 | $entityName, |
||
67 | $mapping['fieldName'], |
||
68 | $mapping['targetEntity'] |
||
69 | ) |
||
70 | ); |
||
71 | |||
72 | /** @var ClassMetadata<EntityInterface> $targetMetadata */ |
||
73 | $targetMetadata = $this->getClassMetadata($entityManager, $mapping['targetEntity']); |
||
74 | |||
75 | $targetEntityOrCollection = $this->resolveTargetEntityOrCollection( |
||
76 | $entity, |
||
77 | $mapping['fieldName'], |
||
78 | $classMetadata, |
||
79 | $targetMetadata |
||
80 | ); |
||
81 | |||
82 | if (!$this->isValidAssociation($targetEntityOrCollection, $mapping)) { |
||
83 | $errorMessage = sprintf( |
||
84 | 'The entity field \'%s::%s\' value could not be resolved', |
||
85 | $entityName, |
||
86 | $mapping['fieldName'] |
||
87 | ); |
||
88 | |||
89 | $this->logger->error($errorMessage); |
||
90 | |||
91 | throw new PersistenceException($errorMessage); |
||
92 | } |
||
93 | |||
94 | $this->logger->info( |
||
95 | sprintf( |
||
96 | 'Performing cascading delete operations for field \'%s::%s\'', |
||
97 | $entityName, |
||
98 | $mapping['fieldName'] |
||
99 | ) |
||
100 | ); |
||
101 | |||
102 | $this->deleteAssociation( |
||
103 | $entityManager, |
||
104 | $mapping['targetEntity'], |
||
105 | $targetEntityOrCollection, |
||
106 | (is_iterable($targetEntityOrCollection) ? $deleteCollectionOptions : $deleteOptions) |
||
107 | ); |
||
145 |