Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like BusinessEntitySubscriber 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 BusinessEntitySubscriber, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class BusinessEntitySubscriber implements EventSubscriber |
||
|
|||
25 | { |
||
26 | protected $businessPageBuilder; |
||
27 | protected $dispatcher; |
||
28 | protected $businessEntityHelper; |
||
29 | protected $flushedBusinessEntities; |
||
30 | protected $businessPageHelper; |
||
31 | protected $flushedBusinessTemplates; |
||
32 | |||
33 | /** |
||
34 | * @param BusinessPageBuilder $businessPageBuilder |
||
35 | * @param BusinessEntityHelper $businessEntityHelper |
||
36 | * @param BusinessPageHelper $businessPageHelper |
||
37 | * @param EventDispatcherInterface $dispatcher |
||
38 | */ |
||
39 | public function __construct( |
||
40 | BusinessPageBuilder $businessPageBuilder, |
||
41 | BusinessEntityHelper $businessEntityHelper, |
||
42 | BusinessPageHelper $businessPageHelper, |
||
43 | EventDispatcherInterface $dispatcher |
||
44 | ) { |
||
45 | $this->businessPageBuilder = $businessPageBuilder; |
||
46 | $this->businessEntityHelper = $businessEntityHelper; |
||
47 | $this->businessPageHelper = $businessPageHelper; |
||
48 | $this->dispatcher = $dispatcher; |
||
49 | $this->flushedBusinessEntities = new ArrayCollection(); |
||
50 | $this->flushedBusinessTemplates = new ArrayCollection(); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * bind to LoadClassMetadata method. |
||
55 | * |
||
56 | * @return string[] The subscribed events |
||
57 | */ |
||
58 | public function getSubscribedEvents() |
||
59 | { |
||
60 | return [ |
||
61 | 'postUpdate', |
||
62 | 'postPersist', |
||
63 | 'preRemove', |
||
64 | 'postFlush', |
||
65 | ]; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param LifecycleEventArgs $eventArgs |
||
70 | */ |
||
71 | public function postUpdate(LifecycleEventArgs $eventArgs) |
||
72 | { |
||
73 | /** @var EntityManager $entityManager */ |
||
74 | $entityManager = $eventArgs->getEntityManager(); |
||
75 | /** @var UnitOfWork $uow */ |
||
76 | $uow = $entityManager->getUnitOfWork(); |
||
77 | |||
78 | foreach ($uow->getScheduledEntityInsertions() as $entity) { |
||
79 | $businessEntity = $this->businessEntityHelper->findByEntityInstance($entity); |
||
80 | if ($businessEntity) { |
||
81 | $this->updateBusinessPages( |
||
82 | $entity, |
||
83 | $businessEntity, |
||
84 | $entityManager, |
||
85 | $uow->getScheduledEntityDeletions() |
||
86 | ); |
||
87 | } |
||
88 | } |
||
89 | $this->updateViewReference($eventArgs); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param LifecycleEventArgs $eventArgs |
||
94 | */ |
||
95 | public function postPersist(LifecycleEventArgs $eventArgs) |
||
96 | { |
||
97 | $this->updateViewReference($eventArgs); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * get BusinessTemplate concerned by this entity (if so) |
||
102 | * then get BusinessPages |
||
103 | * for each BusinessPage, update its slug according to the new slug (if so). |
||
104 | * |
||
105 | * @param $entity |
||
106 | * @param BusinessEntity $businessEntity |
||
107 | * @param EntityManager $entityManager |
||
108 | * @param array $deletions |
||
109 | * |
||
110 | * @throws \Exception |
||
111 | * |
||
112 | * @internal param LifecycleEventArgs $eventArgs |
||
113 | */ |
||
114 | public function updateBusinessPages($entity, BusinessEntity $businessEntity, EntityManager $entityManager, $deletions) |
||
115 | { |
||
116 | $businessTemplates = $entityManager->getRepository('VictoireBusinessPageBundle:BusinessTemplate')->findPagePatternByBusinessEntity($businessEntity); |
||
117 | foreach ($businessTemplates as $businessTemplate) { |
||
118 | // Generate a viewReference for each BT translation |
||
119 | /** @var ViewTranslation $translation */ |
||
120 | foreach ($businessTemplate->getTranslations() as $translation) { |
||
121 | $businessTemplate->setCurrentLocale($translation->getLocale()); |
||
122 | if ($this->businessPageHelper->isEntityAllowed($businessTemplate, $entity, $entityManager)) { |
||
123 | /** @var BusinessPageRepository $bepRepo */ |
||
124 | $bepRepo = $entityManager->getRepository('VictoireBusinessPageBundle:BusinessPage'); |
||
125 | $virtualBusinessPage = $this->businessPageBuilder->generateEntityPageFromTemplate( |
||
126 | $businessTemplate, |
||
127 | $entity, |
||
128 | $entityManager |
||
129 | ); |
||
130 | // Get the BusinessPage if exists for the given entity |
||
131 | /** @var BusinessPage $businessPage */ |
||
132 | $businessPage = $bepRepo->findPageByBusinessEntityAndPattern( |
||
133 | $businessTemplate, |
||
134 | $entity, |
||
135 | $businessEntity |
||
136 | ); |
||
137 | $businessPage->setCurrentLocale($translation->getLocale()); |
||
138 | // If there is diff between persisted BEP and computed, persist the change |
||
139 | $scheduledForRemove = false; |
||
140 | foreach ($deletions as $deletion) { |
||
141 | if (get_class($deletion) == get_class($businessPage) |
||
142 | && $deletion->getId() === $businessPage->getId() |
||
143 | ) { |
||
144 | $scheduledForRemove = true; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | if ($businessPage && !$scheduledForRemove) { |
||
149 | $businessPage->setName($virtualBusinessPage->getName()); |
||
150 | $businessPage->setSlug($virtualBusinessPage->getSlug()); |
||
151 | |||
152 | $entityManager->persist($businessPage); |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | } |
||
157 | $entityManager->flush(); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Iterate over inserted BusinessEntities and BusinessTemplates catched by postPersist |
||
162 | * and dispatch event to generate the needed ViewReferences. |
||
163 | * |
||
164 | * @param PostFlushEventArgs $eventArgs |
||
165 | * |
||
166 | * @throws \Exception |
||
167 | */ |
||
168 | public function postFlush(PostFlushEventArgs $eventArgs) |
||
169 | { |
||
170 | $em = $eventArgs->getEntityManager(); |
||
171 | foreach ($this->flushedBusinessEntities as $entity) { |
||
172 | $businessEntity = $this->businessEntityHelper->findByEntityInstance($entity); |
||
173 | //find all BT that can represent the businessEntity |
||
174 | $businessTemplates = $em->getRepository('VictoireBusinessPageBundle:BusinessTemplate')->findPagePatternByBusinessEntity($businessEntity); |
||
175 | View Code Duplication | foreach ($businessTemplates as $businessTemplate) { |
|
176 | // Generate a viewReference for each BT translation |
||
177 | foreach ($businessTemplate->getTranslations() as $translation) { |
||
178 | $businessTemplate->setCurrentLocale($translation->getLocale()); |
||
179 | |||
180 | if ($page = $em->getRepository( |
||
181 | 'Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage' |
||
182 | )->findPageByBusinessEntityAndPattern($businessTemplate, $entity, $businessEntity) |
||
183 | ) { |
||
184 | //if it's a BP we update the BP |
||
185 | $this->businessPageBuilder->updatePageParametersByEntity($page, $entity); |
||
186 | } else { |
||
187 | $page = $this->businessPageBuilder->generateEntityPageFromTemplate( |
||
188 | $businessTemplate, |
||
189 | $entity, |
||
190 | $em |
||
191 | ); |
||
192 | } |
||
193 | if ($this->businessPageHelper->isEntityAllowed($businessTemplate, $entity, $em)) { |
||
194 | //update the reference |
||
195 | $event = new ViewReferenceEvent($page); |
||
196 | $this->dispatcher->dispatch(ViewReferenceEvents::UPDATE_VIEW_REFERENCE, $event); |
||
197 | } |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | |||
202 | foreach ($this->flushedBusinessTemplates as $entity) { |
||
203 | $em = $eventArgs->getEntityManager(); |
||
204 | $businessEntityId = $entity->getBusinessEntityName(); |
||
205 | $businessEntity = $eventArgs->getEntityManager()->getRepository('VictoireBusinessEntityBundle:BusinessEntity')->findOneBy(['name' => $businessEntityId]); |
||
206 | //find all entities |
||
207 | $entities = $this->businessPageHelper->getEntitiesAllowed($entity, $em); |
||
208 | // Generate a viewReference for each BT translation |
||
209 | View Code Duplication | foreach ($entity->getTranslations() as $translation) { |
|
210 | $entity->setCurrentLocale($translation->getLocale()); |
||
211 | foreach ($entities as $be) { |
||
212 | if ($this->businessPageHelper->isEntityAllowed($entity, $be, $em)) { |
||
213 | if ($page = $em->getRepository( |
||
214 | 'Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage' |
||
215 | )->findPageByBusinessEntityAndPattern($entity, $be, $businessEntity) |
||
216 | ) { |
||
217 | //rebuild page if its a BP |
||
218 | $this->businessPageBuilder->updatePageParametersByEntity($page, $be); |
||
219 | } else { |
||
220 | $page = $this->businessPageBuilder->generateEntityPageFromTemplate( |
||
221 | $entity, |
||
222 | $be, |
||
223 | $em |
||
224 | ); |
||
225 | } |
||
226 | // update reference |
||
227 | $event = new ViewReferenceEvent($page); |
||
228 | $this->dispatcher->dispatch(ViewReferenceEvents::UPDATE_VIEW_REFERENCE, $event); |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | $this->flushedBusinessEntities->clear(); |
||
234 | $this->flushedBusinessTemplates->clear(); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * This method throw an event if needed for a view related to a businessEntity. |
||
239 | * |
||
240 | * @param LifecycleEventArgs $eventArgs |
||
241 | * |
||
242 | * @throws \Exception |
||
243 | */ |
||
244 | private function updateViewReference(LifecycleEventArgs $eventArgs) |
||
262 | |||
263 | /** |
||
264 | * @param LifecycleEventArgs $eventArgs |
||
265 | * |
||
266 | * @throws \Exception |
||
267 | */ |
||
268 | public function preRemove(LifecycleEventArgs $eventArgs) |
||
334 | } |
||
335 |