| Total Complexity | 41 |
| Total Lines | 268 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MailAdmin 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.
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 MailAdmin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | final class MailAdmin extends AbstractAdmin |
||
| 22 | { |
||
| 23 | private $savedEvents; |
||
| 24 | private $savedAudiences; |
||
| 25 | /** |
||
| 26 | * Default values to the datagrid. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $datagridValues = array( |
||
| 31 | '_sort_by' => 'id', |
||
| 32 | '_sort_order' => 'DESC', |
||
| 33 | ); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @return array |
||
| 37 | */ |
||
| 38 | public function getBatchActions() |
||
| 39 | { |
||
| 40 | return array(); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * {@inheritdoc} |
||
| 45 | */ |
||
| 46 | public function postPersist($mail) |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritdoc} |
||
| 55 | */ |
||
| 56 | public function preUpdate($object) |
||
| 57 | { |
||
| 58 | /** @var Mail $object */ |
||
| 59 | $container = $this->getConfigurationPool()->getContainer(); |
||
| 60 | $em = $container->get('doctrine')->getManager(); |
||
| 61 | /** @var UnitOfWork $uow */ |
||
| 62 | $uow = $em->getUnitOfWork(); |
||
| 63 | $originalObject = $uow->getOriginalEntityData($object); |
||
| 64 | |||
| 65 | $eventsChange = count($this->savedEvents) !== $object->getEvents()->count(); |
||
| 66 | $audiencesChange = count($this->savedAudiences) !== $object->getAudiences()->count(); |
||
| 67 | |||
| 68 | if (!$eventsChange) { |
||
| 69 | foreach ($this->savedEvents as $savedEvent) { |
||
| 70 | $founded = false; |
||
| 71 | foreach ($object->getEvents() as $event) { |
||
| 72 | $founded = $savedEvent === $event->getId(); |
||
| 73 | if ($founded) { |
||
| 74 | break; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | $eventsChange = !$founded; |
||
| 78 | if ($eventsChange) { |
||
| 79 | break; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | if (!$audiencesChange) { |
||
| 85 | foreach ($this->savedAudiences as $savedAudience) { |
||
| 86 | $founded = false; |
||
| 87 | /** @var EventAudience $audience */ |
||
| 88 | foreach ($object->getAudiences() as $audience) { |
||
| 89 | $founded = $savedAudience === $audience->getId(); |
||
| 90 | if ($founded) { |
||
| 91 | break; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | $audiencesChange = !$founded; |
||
| 95 | if ($audiencesChange) { |
||
| 96 | break; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($eventsChange || |
||
| 102 | $audiencesChange || |
||
| 103 | $originalObject['wantsVisitEvent'] !== $object->isWantsVisitEvent() || |
||
| 104 | $originalObject['paymentStatus'] !== $object->getPaymentStatus() |
||
| 105 | ) { |
||
| 106 | $objectStatus = $object->getStart(); |
||
| 107 | if (true === $objectStatus) { |
||
| 108 | $object->setStart(false); |
||
| 109 | $em->flush(); |
||
| 110 | } |
||
| 111 | /** @var $queueRepository \Stfalcon\Bundle\EventBundle\Repository\MailQueueRepository */ |
||
| 112 | $queueRepository = $em->getRepository('StfalconEventBundle:MailQueue'); |
||
| 113 | $deleteCount = $queueRepository->deleteAllNotSentMessages($object); |
||
| 114 | $object->setTotalMessages($object->getTotalMessages() - $deleteCount); |
||
| 115 | $usersInMail = $em->getRepository('ApplicationUserBundle:User')->getUsersFromMail($object); |
||
| 116 | $newUsers = $this->getUsersForEmail($object); |
||
| 117 | $addUsers = array_diff($newUsers, $usersInMail); |
||
| 118 | |||
| 119 | $this->addUsersToEmail($object, $addUsers); |
||
| 120 | |||
| 121 | if (true === $objectStatus) { |
||
| 122 | $object->setStart(true); |
||
| 123 | $em->flush(); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param RouteCollection $collection |
||
| 130 | */ |
||
| 131 | protected function configureRoutes(RouteCollection $collection) |
||
| 132 | { |
||
| 133 | $collection->add('admin_send', $this->getRouterIdParameter().'/admin-send'); |
||
| 134 | $collection->add('user_send', 'user-send'); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param ListMapper $listMapper |
||
| 139 | */ |
||
| 140 | protected function configureListFields(ListMapper $listMapper) |
||
| 141 | { |
||
| 142 | $listMapper |
||
| 143 | ->addIdentifier('id', null, ['label' => 'id']) |
||
| 144 | ->addIdentifier('title', null, ['label' => 'Название']) |
||
| 145 | ->add('statistic', 'string', ['label' => 'всего/отправлено/открыли/отписались']) |
||
| 146 | ->add('audiences', null, ['label' => 'Аудитории']) |
||
| 147 | ->add('events', null, ['label' => 'События']) |
||
| 148 | ->add('_action', 'actions', [ |
||
| 149 | 'label' => 'Действие', |
||
| 150 | 'actions' => [ |
||
| 151 | 'edit' => [], |
||
| 152 | 'delete' => [], |
||
| 153 | 'ispremium' => [ |
||
| 154 | 'template' => 'StfalconEventBundle:Admin:list__action_adminsend.html.twig', |
||
| 155 | ], |
||
| 156 | 'start' => [ |
||
| 157 | 'template' => 'StfalconEventBundle:Admin:list__action_start.html.twig', |
||
| 158 | ], |
||
| 159 | ], |
||
| 160 | ]); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param FormMapper $formMapper |
||
| 165 | */ |
||
| 166 | protected function configureFormFields(FormMapper $formMapper) |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param MenuItemInterface $menu Menu |
||
| 209 | * @param string $action Action |
||
| 210 | * @param AdminInterface $childAdmin Child admin |
||
| 211 | */ |
||
| 212 | protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null) |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param Mail $mail |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | private function getUsersForEmail($mail) |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param Mail $mail |
||
| 261 | * @param array $users |
||
| 262 | * |
||
| 263 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 264 | */ |
||
| 265 | private function addUsersToEmail($mail, $users) |
||
| 289 | } |
||
| 290 | } |
||
| 292 |