simplenews_mail_subscriptions api use fix
This commit is contained in:
parent
6372be828b
commit
6ef749f17c
|
@ -79,19 +79,31 @@ index 09abe13..4b2ee58 100644
|
|||
/**
|
||||
diff --git a/simplenews_mailjet_subscriptions.services.yml b/simplenews_mailjet_subscriptions.services.yml
|
||||
new file mode 100644
|
||||
index 0000000..cdd4137
|
||||
index 0000000..cee6a81
|
||||
--- /dev/null
|
||||
+++ b/simplenews_mailjet_subscriptions.services.yml
|
||||
@@ -0,0 +1,4 @@
|
||||
@@ -0,0 +1,7 @@
|
||||
+services:
|
||||
+ simplenews_mailjet_subscriptions.handler:
|
||||
+ class: Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandler
|
||||
+ arguments: ['@mailjet.client_factory']
|
||||
+ simplenews_mailjet_subscriptions.utilities:
|
||||
+ class: Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities
|
||||
+ arguments: [ '@mailjet.handler' ]
|
||||
+ arguments: [ '@simplenews_mailjet_subscriptions.handler' ]
|
||||
diff --git a/src/Entity/SimplenewsMailjetSubscriptionEntity.php b/src/Entity/SimplenewsMailjetSubscriptionEntity.php
|
||||
index 27b7737..b74f89e 100644
|
||||
index 27b7737..34c8df5 100644
|
||||
--- a/src/Entity/SimplenewsMailjetSubscriptionEntity.php
|
||||
+++ b/src/Entity/SimplenewsMailjetSubscriptionEntity.php
|
||||
@@ -94,53 +94,53 @@ class SimplenewsMailjetSubscriptionEntity extends ConfigEntityBase implements Si
|
||||
@@ -30,8 +30,6 @@ use Drupal\Core\Config\Entity\ConfigEntityBase;
|
||||
* config_export = {
|
||||
* "id",
|
||||
* "title",
|
||||
- * "api_key",
|
||||
- * "secret_key",
|
||||
* "mapping_table"
|
||||
* }
|
||||
* )
|
||||
@@ -94,53 +92,53 @@ class SimplenewsMailjetSubscriptionEntity extends ConfigEntityBase implements Si
|
||||
return $this->get('id');
|
||||
}
|
||||
|
||||
|
@ -350,15 +362,111 @@ index c727d26..37c082d 100644
|
|||
|
||||
/**
|
||||
* Sets the entity mapping table.
|
||||
diff --git a/src/SimplenewsMailjetSubscriptionsMailjetHandler.php b/src/SimplenewsMailjetSubscriptionsMailjetHandler.php
|
||||
new file mode 100644
|
||||
index 0000000..a68cbf1
|
||||
--- /dev/null
|
||||
+++ b/src/SimplenewsMailjetSubscriptionsMailjetHandler.php
|
||||
@@ -0,0 +1,47 @@
|
||||
+<?php
|
||||
+
|
||||
+namespace Drupal\simplenews_mailjet_subscriptions;
|
||||
+
|
||||
+use Mailjet\Resources;
|
||||
+use Drupal\mailjet\MailjetHandler;
|
||||
+
|
||||
+/**
|
||||
+ * Implement SimplenewsMailjetSubscriptionsMailjetHandler interface.
|
||||
+ */
|
||||
+class SimplenewsMailjetSubscriptionsMailjetHandler extends MailjetHandler implements SimplenewsMailjetSubscriptionsMailjetHandlerInterface{
|
||||
+
|
||||
+ /**
|
||||
+ * {@inheritdoc}
|
||||
+ *
|
||||
+ * @return array|null
|
||||
+ * Return array with result or null.
|
||||
+ */
|
||||
+ public function createMailjetContact($email): ?array {
|
||||
+
|
||||
+ $response = $this->mailjetClient->post(Resources::$Contact, ["body" => ['Email' => $email]]);
|
||||
+
|
||||
+ if ($response->success()) {
|
||||
+ return $response->getData();
|
||||
+ }
|
||||
+
|
||||
+ return NULL;
|
||||
+ }
|
||||
+
|
||||
+ /**
|
||||
+ * {@inheritdoc}
|
||||
+ *
|
||||
+ * @return array|null
|
||||
+ * Return array with result or null.
|
||||
+ */
|
||||
+ public function getMailjetContactByEmail($email): ?array {
|
||||
+
|
||||
+ $response = $this->mailjetClient->get(Resources::$Contact, ['id' => $email]);
|
||||
+
|
||||
+ if ($response->success()) {
|
||||
+ return $response->getData();
|
||||
+ }
|
||||
+
|
||||
+ return NULL;
|
||||
+ }
|
||||
+
|
||||
+}
|
||||
\ No newline at end of file
|
||||
diff --git a/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php b/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php
|
||||
new file mode 100644
|
||||
index 0000000..b6484d7
|
||||
--- /dev/null
|
||||
+++ b/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php
|
||||
@@ -0,0 +1,34 @@
|
||||
+<?php
|
||||
+
|
||||
+namespace Drupal\simplenews_mailjet_subscriptions;
|
||||
+
|
||||
+use Drupal\mailjet\MailjetHandlerInterface;
|
||||
+
|
||||
+/**
|
||||
+ * Interface for mailjet handler.
|
||||
+ */
|
||||
+interface SimplenewsMailjetSubscriptionsMailjetHandlerInterface extends MailjetHandlerInterface {
|
||||
+
|
||||
+ /**
|
||||
+ * create contact.
|
||||
+ *
|
||||
+ * @param string $email
|
||||
+ * email of the contact
|
||||
+ *
|
||||
+ * @return array|null
|
||||
+ * Return array with the result or null.
|
||||
+ */
|
||||
+ public function createMailjetContact($email): ?array;
|
||||
+
|
||||
+ /**
|
||||
+ * Get contact by email.
|
||||
+ *
|
||||
+ * @param string $email
|
||||
+ * email of the contact
|
||||
+ *
|
||||
+ * @return array|null
|
||||
+ * Return array with the result or null.
|
||||
+ */
|
||||
+ public function getMailjetContactByEmail($email): ?array;
|
||||
+
|
||||
+}
|
||||
\ No newline at end of file
|
||||
diff --git a/src/SimplenewsMailjetSubscriptionsUtilities.php b/src/SimplenewsMailjetSubscriptionsUtilities.php
|
||||
index 950ff8a..05601ca 100644
|
||||
index 950ff8a..82e15b5 100644
|
||||
--- a/src/SimplenewsMailjetSubscriptionsUtilities.php
|
||||
+++ b/src/SimplenewsMailjetSubscriptionsUtilities.php
|
||||
@@ -2,141 +2,79 @@
|
||||
@@ -2,141 +2,122 @@
|
||||
|
||||
namespace Drupal\simplenews_mailjet_subscriptions;
|
||||
|
||||
+use Drupal\mailjet\MailjetHandlerInterface;
|
||||
+// use Drupal\mailjet\MailjetHandlerInterface;
|
||||
+use Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface;
|
||||
+
|
||||
/**
|
||||
- * Provides a list of Simplenews Mailjet Subscription entities.
|
||||
|
@ -370,7 +478,15 @@ index 950ff8a..05601ca 100644
|
|||
- * Check Subscriber.
|
||||
+ * MailjetHandler service.
|
||||
+ *
|
||||
+ * @var \Drupal\mailjet\MailjetHandlerInterface
|
||||
+ * @var \Drupal\Simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface
|
||||
+ */
|
||||
+ protected $mailjetHandler;
|
||||
+
|
||||
+/**
|
||||
+ * SubscribeEmailForm constructor.
|
||||
+ *
|
||||
+ * @param \Drupal\Simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandlerInterface $mailjetHandler
|
||||
+ * The mailjet handler service.
|
||||
*/
|
||||
- private static function checkSubscriber($api_key, $secret_key, $encoded_mail) {
|
||||
-
|
||||
|
@ -386,16 +502,9 @@ index 950ff8a..05601ca 100644
|
|||
- $response = curl_exec($ch);
|
||||
- curl_close($ch);
|
||||
- $json = json_decode($response, TRUE);
|
||||
+ protected $mailjetHandler;
|
||||
|
||||
-
|
||||
- return $json;
|
||||
+/**
|
||||
+ * SubscribeEmailForm constructor.
|
||||
+ *
|
||||
+ * @param \Drupal\mailjet\MailjetHandlerInterface $mailjetHandler
|
||||
+ * The mailjet handler service.
|
||||
+ */
|
||||
+ public function __construct(MailjetHandlerInterface $mailjetHandler) {
|
||||
+ public function __construct(SimplenewsMailjetSubscriptionsMailjetHandlerInterface $mailjetHandler) {
|
||||
+ $this->mailjetHandler = $mailjetHandler;
|
||||
}
|
||||
|
||||
|
@ -431,8 +540,30 @@ index 950ff8a..05601ca 100644
|
|||
- return $json;
|
||||
+ public static function create(ContainerInterface $container) {
|
||||
+ return new static(
|
||||
+ $container->get('mailjet.handler')
|
||||
+ $container->get('simplenews_mailjet_subscriptions.handler')
|
||||
+ );
|
||||
+ }
|
||||
+
|
||||
+ public function checkSubscriber($encoded_mail) {
|
||||
+ $response = $this->mailjetHandler->getMailjetContactByEmail($encoded_mail);
|
||||
+ if ($response) {
|
||||
+ // contact alredy exists
|
||||
+ $contact_id = $response[0]['ID'];
|
||||
+ return $contact_id;
|
||||
+ }
|
||||
+
|
||||
+ return null;
|
||||
+ }
|
||||
+
|
||||
+ public function createSubscriber($email) {
|
||||
+ $response = $this->mailjetHandler->createMailjetContact($email);
|
||||
+ if ($response) {
|
||||
+ // contact alredy exists
|
||||
+ $contact_id = $response[0]['ID'];
|
||||
+ return $contact_id;
|
||||
+ }
|
||||
+
|
||||
+ return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -444,18 +575,23 @@ index 950ff8a..05601ca 100644
|
|||
$contact_property = $category['contact_property'];
|
||||
$newsletter_property_name = $category['newsletter_property_name'];
|
||||
$subscriber_email = $subscriber->getMail();
|
||||
- $encoded_mail = urlencode($subscriber_email);
|
||||
$encoded_mail = urlencode($subscriber_email);
|
||||
- $response = '';
|
||||
-
|
||||
- $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail);
|
||||
-
|
||||
- $options = $contact_info['Data'];
|
||||
-
|
||||
|
||||
- // Se existe - acrescenta a opções.
|
||||
- if ($options[0]['Data']) {
|
||||
- foreach ($options[0]['Data'] as $property) {
|
||||
- if ($property["Name"] == $contact_property) {
|
||||
-
|
||||
+ $contactid = $this->checkSubscriber($encoded_mail);
|
||||
+ if (!$contactid) {
|
||||
+ // subscriber does not exists yet, create it
|
||||
+ $contactid = $this->createSubscriber($subscriber_email);
|
||||
+ }
|
||||
|
||||
- if (is_int(strpos($property['Value'], $newsletter_property_name))) {
|
||||
- $newsletter_property_name = $property['Value'];
|
||||
- }
|
||||
|
@ -463,23 +599,31 @@ index 950ff8a..05601ca 100644
|
|||
- $newsletter_property_name = $property['Value'] . ';' . $newsletter_property_name;
|
||||
- }
|
||||
- }
|
||||
- }
|
||||
- }
|
||||
+ if ($contactid) {
|
||||
+ $contact = [
|
||||
+ 'Email' => $subscriber_email,
|
||||
+ 'Properties' => [
|
||||
+ $contact_property => $newsletter_property_name
|
||||
+ ]
|
||||
+ // 'Properties' => [
|
||||
+ // $contact_property => $newsletter_property_name
|
||||
+ // ]
|
||||
+ ];
|
||||
+
|
||||
+ $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'addnoforce');
|
||||
+ if ($response) {
|
||||
+ // $contact_id = $response['Data'][0]['ContactID'];
|
||||
+ \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' subscribed to the ' . $listID . ' list.');
|
||||
+ }else{
|
||||
+ \Drupal::logger('simplenews_mailjet_subscriptions')->warning('error while subscrining ' . $subscriber_email . ' to the ' . $listID . ' list.');
|
||||
}
|
||||
+
|
||||
+ } else {
|
||||
+ \Drupal::logger('simplenews_mailjet_subscriptions')->warning($subscriber_email . ' does not exists and can not be created on mailjet.');
|
||||
}
|
||||
|
||||
- $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce');
|
||||
- $contact_id = $response['Data'][0]['ContactID'];
|
||||
- \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' subscribed to the ' . $contact_property . ' properties with the ' . $newsletter_property_name . ' values in the list ' . $listID . '.');
|
||||
- return $response;
|
||||
+ // $response = $this->updateSubscriber($listID, $contact, 'addnoforce');
|
||||
+ $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'addnoforce');
|
||||
+ // $contact_id = $response['Data'][0]['ContactID'];
|
||||
+ \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' subscribed to the ' . $listID . ' list .');
|
||||
+
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -525,17 +669,19 @@ index 950ff8a..05601ca 100644
|
|||
- }
|
||||
+ $contact = [
|
||||
+ 'Email' => $subscriber_email,
|
||||
+ 'Properties' => [
|
||||
+ $contact_property => $newsletter_property_name
|
||||
+ ]
|
||||
+ // 'Properties' => [
|
||||
+ // $contact_property => $newsletter_property_name
|
||||
+ // ]
|
||||
+ ];
|
||||
|
||||
- return $response;
|
||||
+ // $response = $this->updateSubscriber($subscriber_email, $newsletter_property_name, 'remove');
|
||||
+ $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'remove');
|
||||
+ if ($response) {
|
||||
+ // $contact_id = $response['Data'][0]['ContactID'];
|
||||
+ \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' removed from the ' . $listID . ' list ');
|
||||
+ // return $response;
|
||||
+ \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' removed from the ' . $listID . ' list.');
|
||||
+ }else{
|
||||
+ \Drupal::logger('simplenews_mailjet_subscriptions')->warning('error while removing ' . $subscriber_email . ' from the ' . $listID . ' list.');
|
||||
+ }
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue