Example #1
0
 public function sync(array $subscriptions)
 {
     $synched = $failed = 0;
     if (!empty($subscriptions)) {
         $stack = HandlerStack::create();
         $middleware = new Oauth1(['consumer_key' => $this->consumerKey, 'consumer_secret' => $this->consumerSecret, 'token' => '', 'token_secret' => '']);
         $stack->push($middleware);
         $client = new GuzzleClient(['base_uri' => 'https://restapi.mailplus.nl', 'handler' => $stack, 'auth' => 'oauth', 'headers' => ['Accept' => 'application/json', 'Content-Type' => 'application/json']]);
         foreach ($subscriptions as $subscription) {
             try {
                 $contact = ['update' => true, 'purge' => false, 'contact' => ['externalId' => $subscription->getId(), 'properties' => ['email' => $subscription->getEmail()]]];
                 $response = $client->post('/integrationservice-1.1.0/contact', ['body' => json_encode($contact)]);
                 if ($response->getStatusCode() == '204') {
                     //Contact added successfully status code
                     $this->subscriptionManager->updateStatus($subscription, Subscription::STATUS_SYNCED);
                     ++$synched;
                 } else {
                     $this->subscriptionManager->updateStatus($subscription, Subscription::STATUS_FAILED);
                     ++$failed;
                 }
             } catch (\Exception $e) {
                 $this->subscriptionManager->updateStatus($subscription, Subscription::STATUS_FAILED);
                 ++$failed;
             }
         }
     }
     return sprintf('Synched %d,failed %d subscriptions of %d total', $synched, $failed, count($subscriptions));
 }
Example #2
0
 public function synchroniseList(MailingList $list, \Closure $logger)
 {
     $now = new \DateTime();
     $size = 10;
     $lastSync = $list->getSyncedAt() ? $list->getSyncedAt()->format('Y-m-d H:i:s') : null;
     $updates = $this->client()->get(sprintf('lists/%s/members', $list->getRemoteListId()), ['since_last_changed' => $lastSync, 'offset' => 0, 'count' => $size]);
     $count = $updates['total_items'];
     $i = 0;
     for ($offset = 0; $offset < $count; $offset += $size) {
         if ($offset > 0) {
             $updates = $this->client()->get(sprintf('lists/%s/members', $list->getRemoteListId()), ['since_last_changed' => $lastSync, 'offset' => $offset - 1, 'count' => $size]);
             $count = $updates['total_items'];
         }
         foreach ($updates['members'] as $member) {
             ++$i;
             $subscription = $this->subscriptionManager->findOrCreate($list, $member['email_address']);
             $subscription->setEmail($member['email_address']);
             $subscription->setStatus($this->statusMap[$member['status']]);
             $subscription->setUpdatedAt(new \DateTime($member['last_changed']));
             $subscription->setSyncedAt($now);
             $this->subscriptionManager->save($subscription);
             $logger(sprintf('Processed updates from MailChimp for %s - %d/%d', $member['email_address'], $i, $updates['total_items']));
         }
     }
     $subscriptions = $this->subscriptionManager->findOutOfSync($list, $now);
     $i = 0;
     foreach ($subscriptions as $subscription) {
         ++$i;
         $this->client()->post(sprintf('lists/%s/members', $list->getRemoteListId()), ['email_address' => $subscription->getEmail(), 'status' => array_search($subscription->getStatus(), $this->statusMap)]);
         $logger(sprintf('Processed updates to MailChimp for %s - %d/%d', $subscription->getEmail(), $i, count($subscriptions)));
     }
     $list->setSyncedAt($now);
     $this->subscriptionManager->saveList($list);
 }
Example #3
0
 /**
  * Synchronize a subscription
  *
  * @param Subscription $subscription
  * @return bool
  */
 public function synchronise(Subscription $subscription)
 {
     try {
         $contact = ['update' => true, 'purge' => false, 'contact' => ['externalId' => $subscription->getId(), 'properties' => ['email' => $subscription->getEmail()]]];
         $response = $this->post('contact', $contact);
         if ($response->getStatusCode() == '204') {
             //Contact added successfully status code
             $this->subscriptionManager->updateStatus($subscription, Subscription::STATUS_SYNCED);
             return true;
         } else {
             $this->subscriptionManager->updateStatus($subscription, Subscription::STATUS_FAILED);
             return false;
         }
     } catch (\Exception $e) {
         $this->subscriptionManager->updateStatus($subscription, Subscription::STATUS_FAILED);
         return true;
     }
 }
Example #4
0
 /**
  * This method is called right after the post is stored in the database during the Form submit.
  *
  * @param FormSubmitEvent $event
  */
 public function postFormSubmit(FormSubmitEvent $event)
 {
     $post = $event->getPost();
     $mailinglists = $email = null;
     foreach ($post->getValueSet()->getValues() as $value) {
         if ($value instanceof MailingListSubscribeValue && $value->getValue() == true) {
             $parameters = $value->getAttribute()->getParameters();
             if (isset($parameters['mailingLists'])) {
                 $mailinglists = $this->mailingListManager->getRepository()->findByIds($parameters['mailingLists']);
             }
         } elseif ($value instanceof EmailValue) {
             $email = $value->getValue();
         }
     }
     if ($email && $mailinglists) {
         foreach ($mailinglists as $mailinglist) {
             $subscription = new Subscription();
             $subscription->setEmail($email);
             $subscription->setMailingList($mailinglist);
             $this->subscriptionManager->save($subscription);
         }
     }
 }
Example #5
0
 /**
  * @param BlockInterface $block
  */
 public function load(BlockInterface $block)
 {
     $properties = $block->getProperties();
     $opts = array();
     if (isset($properties['responseType']) && $properties['responseType'] == 'redirect') {
         $opts['action'] = $this->router->generate('opifer_mailing_list_subscribe_block', ['id' => $block->getId()]);
     }
     $this->form = $this->formFactory->create(SubscribeType::class, $this->subscription, $opts);
     $this->form->handleRequest($this->request);
     if ($this->form->isValid()) {
         foreach ($this->getMailingLists($block) as $list) {
             $subscription = $this->subscriptionManager->findOrCreate($list, $this->subscription->getEmail());
             $this->subscriptionManager->save($subscription);
         }
         $this->subscribed = true;
     }
 }