| Conditions | 8 |
| Paths | 36 |
| Total Lines | 69 |
| 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 |
||
| 31 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 32 | { |
||
| 33 | /* @var MailChimpApi $mailchimp */ |
||
| 34 | /* @var TicketRepository $ticketRepo */ |
||
| 35 | /* @var EventRepository $eventRepo */ |
||
| 36 | $mailchimp = $this->getContainer()->get('mailchimp'); |
||
| 37 | |||
| 38 | // There is no "clear subscribers" api endpoint, so we have to batch unsubscribe the non-participants |
||
| 39 | |||
| 40 | $output->writeln('Fetching subscribers …'); |
||
| 41 | $subscribers = new ArrayCollection(); |
||
| 42 | $page = 0; |
||
| 43 | do { |
||
| 44 | $result = $mailchimp->listsMembers( |
||
| 45 | array( |
||
| 46 | 'id' => $input->getArgument('list'), |
||
| 47 | 'opts' => array( |
||
| 48 | 'start' => $page++ |
||
| 49 | ) |
||
| 50 | ) |
||
| 51 | ); |
||
| 52 | foreach ($result->data as $subscriber) { |
||
| 53 | $subscribers->add(strtolower($subscriber->email)); |
||
| 54 | } |
||
| 55 | } while (count($result->data) > 0); |
||
| 56 | $output->writeln(sprintf("%d subscribers in list.", $subscribers->count())); |
||
| 57 | |||
| 58 | $eventRepo = $this->getContainer()->get('bcrm.backend.repo.event'); |
||
| 59 | $ticketRepo = $this->getContainer()->get('bcrm.backend.repo.ticket'); |
||
| 60 | $participants = new ArrayCollection(); |
||
| 61 | foreach ($ticketRepo->getTicketsForEvent($eventRepo->getNextEvent()->getOrThrow( |
||
| 62 | new BadMethodCallException('No event.') |
||
| 63 | )) as $ticket) { |
||
| 64 | if ($participants->contains(strtolower($ticket->getEmail()))) continue; |
||
| 65 | $participants->add(strtolower($ticket->getEmail())); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Unsubscribe former participants |
||
| 69 | $unsubscribe = new ArrayCollection(array_diff($subscribers->toArray(), $participants->toArray())); |
||
| 70 | $output->writeln(sprintf('Unsubscribing %d participants.', $unsubscribe->count())); |
||
| 71 | $result = $mailchimp->listsBatch_unsubscribe( |
||
| 72 | array( |
||
| 73 | 'id' => $input->getArgument('list'), |
||
| 74 | 'batch' => $this->toBatch($unsubscribe, false), |
||
| 75 | 'delete_member' => true, |
||
| 76 | 'send_goodbye' => false, |
||
| 77 | ) |
||
| 78 | ); |
||
| 79 | if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) { |
||
| 80 | $output->writeln(print_r($result, true)); |
||
| 81 | } |
||
| 82 | if ($result->error_count > 0) { |
||
| 83 | throw new CommandException(sprintf('Failed to unsubscribe %d participants!', $result->error_count)); |
||
| 84 | } |
||
| 85 | |||
| 86 | // Subscribe new participiants |
||
| 87 | $newSubcsribers = new ArrayCollection(array_diff($participants->toArray(), $subscribers->toArray())); |
||
| 88 | $output->writeln(sprintf('Subscribing %d new participants.', $newSubcsribers->count())); |
||
| 89 | $result = $mailchimp->listsBatch_subscribe( |
||
| 90 | array( |
||
| 91 | 'id' => $input->getArgument('list'), |
||
| 92 | 'batch' => $this->toBatch($newSubcsribers), |
||
| 93 | 'double_optin' => false |
||
| 94 | ) |
||
| 95 | ); |
||
| 96 | if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) { |
||
| 97 | $output->writeln(print_r($result, true)); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 116 |