From eb43a79d75ca03bfb164f586e28c1a6d58f179c2 Mon Sep 17 00:00:00 2001 From: Lukas Mestan Date: Mon, 31 Jan 2022 14:32:57 +0100 Subject: [PATCH] init --- .gitignore | 1 + README.md | 59 +++++++ composer.json | 28 ++++ src/SmartEmailing.php | 294 +++++++++++++++++++++++++++++++++ src/SmartEmailingException.php | 8 + 5 files changed, 390 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/SmartEmailing.php create mode 100644 src/SmartEmailingException.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..be19e3a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/nbproject/* diff --git a/README.md b/README.md new file mode 100644 index 0000000..10f2fa8 --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# SmartEmailing + +Easy way to interact with SmartEmailing API from PHP + +## Installation + +The best way to install this component is using [Composer](http://getcomposer.org/): + +```sh +$ composer require belenka/smartemailing +``` + +Then it is required to add the following lines to config.neon: + +``` +parameters: + smartemailing: + username: + token: + +services: + - BeLenka\SmartEmailing(%smartemailing.username%, %smartemailing.token%) +``` + +## Usage + +Insert a new contact into SmartEmailing lists: + +``` +$this->smartEmailing->importContact($email, $name, $surname, $language, $contactLists, $properties, $customFields, $purposes, $settings); +``` + +Insert a order data into SmartEmailing lists: + +``` +$this->smartEmailing->importOrders($data); +``` + +Get an exisitng contact by email: + +``` +$this->smartEmailing->getContactsByEmail($email); +``` + +Get all contacts: + +``` +$this->smartEmailing->getContacts(); +``` + +Get an exisitng contact by user's ID: + +``` +$this->smartEmailing->getContact($id); +``` + + + + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a19f2c8 --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "belenka/smartemailing", + "type": "library", + "description": "Easy way to interact with SmartEmailing API from PHP", + "license": ["MIT"], + "authors": [ + { + "name": "Apps Dev Team", + "email": "hello@appsdevteam.com", + "homepage": "https://www.appsdevteam.com" + }, + { + "name": "BeLenka Dev Team", + "email": "hello@belenka.dev", + "homepage": "https://www.belenka.com" + } + ], + "require": { + "guzzlehttp/guzzle": "^6.3", + "nette/utils": "^2.2" + }, + "minimum-stability": "stable", + "autoload": { + "psr-4": { + "belenka\\": "src/" + } + } +} diff --git a/src/SmartEmailing.php b/src/SmartEmailing.php new file mode 100644 index 0000000..c72035d --- /dev/null +++ b/src/SmartEmailing.php @@ -0,0 +1,294 @@ +username = $username; + $this->token = $token; + } + + /** + * Aliveness test + * + * @return array + * @throws SmartEmailingException + */ + public function ping() + { + return $this->call(self::METHOD_GET, self::NODE_PING); + } + + /** + * Login test + * + * @return array + * @throws SmartEmailingException + */ + public function checkCredentials() + { + return $this->call(self::METHOD_GET, self::NODE_CHECK_CREDENTIALS); + } + + /** + * @param string $name + * @param string $senderName + * @param string $senderEmail email + * @param string $replyTo email + * @param string|null $publicName + * @throws SmartEmailingException + */ + public function createContactlist($name, $senderName, $senderEmail, $replyTo, $publicName = NULL) + { + $data = [ + 'name' => $name, + 'sendername' => $senderName, + 'senderemail' => $senderEmail, + 'replyto' => $replyTo, + ]; + if (is_string($publicName)) { + $data['publicname'] = $publicName; + } + return $this->call(self::METHOD_POST, self::NODE_CONTACTLISTS, $data); + } + + /** + * @return array + * @throws SmartEmailingException + */ + public function getContactlists() + { + return $this->call(self::METHOD_GET, self::NODE_CONTACTLISTS); + } + + /** + * @param $id + * @return array + * @throws SmartEmailingException + */ + public function getContactlist($id) + { + return $this->call(self::METHOD_GET, self::NODE_CONTACTLISTS . "/" . $id); + } + + /** + * @param $id + * @param string|null $name + * @param string|null $senderName + * @param string|null $senderEmail + * @param string|null $replyTo + * @param string|null $publicName + * @return array + * @throws SmartEmailingException + */ + public function updateContactlist($id, $name = NULL, $senderName = NULL, $senderEmail = NULL, $replyTo = NULL, $publicName = NULL) + { + $data = []; + if ($name !== NULL) + $data['name'] = $name; + if ($senderName !== NULL) + $data['sendername'] = $senderName; + if ($senderEmail !== NULL) + $data['senderemail'] = $senderEmail; + if ($replyTo !== NULL) + $data['replyto'] = $replyTo; + if ($publicName !== NULL) + $data['publicname'] = $publicName; + if ($publicName !== NULL) + $data['publicname'] = $publicName; + + return $this->call(self::METHOD_PUT, self::NODE_CONTACTLISTS . "/" . $id, $data); + } + + /** + * @param string $from + * @param string $to + * @return array + * @throws SmartEmailingException + */ + public function changeContactEmail($from, $to) + { + return $this->call(self::METHOD_POST, self::NODE_CHANGE_EMAILADDRESS, [ + 'from' => $from, + 'to' => $to, + ]); + } + + /** + * Deletes contact and anonymizes all his leftover data. This action cannot be undone. + * This is GDPR complaint method to secure contact's right to be forgotten. + * + * @param $id + * @return array + * @throws SmartEmailingException + */ + public function deleteContact($id) + { + return $this->call(self::METHOD_DELETE, self::NODE_CONTACT_FORGET . "/" . $id); + } + + /** + * @return array + * @throws SmartEmailingException + */ + public function getContacts($query = []) + { + return $this->call(self::METHOD_GET, self::NODE_CONTACTS, null, $query); + } + + /** + * @return array + * @throws SmartEmailingException + */ + public function getContactsByEmail($email) + { + $query = ['emailaddress' => $email]; + return $this->call(self::METHOD_GET, self::NODE_CONTACTS, null, $query); + } + + /** + * @return array + * @throws SmartEmailingException + */ + public function getContact($id) + { + return $this->call(self::METHOD_GET, self::NODE_CONTACTS . "/" . $id); + } + + /** + * https://app.smartemailing.cz/docs/api/v3/index.html#api-Import-Import_contacts + * + * @param $email + * @param array|NULL $contactLists + * @param array|NULL $properties + * @param array|NULL $customFields + * @param array|NULL $purposes + * @param array|NULL $settings + */ + public function importContact($email, $name = NULL, $surname = NULL, $language = 'cz_CZ', array $contactLists = NULL, array $properties = NULL, array $customFields = NULL, array $purposes = NULL, array $settings = NULL) + { + $contact = [ + 'emailaddress' => $email, + ]; + + if (is_array($contactLists)) { + $contact['contactlists'] = []; + foreach ($contactLists as $id => $status) { + $contact['contactlists'][] = [ + 'id' => $id, + 'status' => $status, + 'name' => $name, + 'surname' => $surname, + 'language' => $language + ]; + } + } + + if (is_array($properties)) { + foreach ($properties as $name => $value) { + $contact[$name] = $value; +// $contact['data'][$name] = $value; + } + } + + if (is_array($customFields)) { + $contact['customfields'] = $customFields; + } + + if (is_array($purposes)) { + $contact['purposes'] = $purposes; + } + + $data = [ + 'data' => [ + $contact, + ], + ]; + + if (is_array($settings)) { + $data['settings'] = $settings; + } + + return $this->call(self::METHOD_POST, self::NODE_IMPORT, $data); + } + + public function importOrders(array $data) + { + return $this->call(self::METHOD_POST, self::NODE_ORDERS, $data); + } + + /** + * connect to Smartemailing API v3 + * + * @param string $method + * @param string $node + * @param array|null $data + * @param array|null $query + * @return array + * @throws SmartEmailingException + */ + protected function call($method, $node, $data = NULL, $query = NULL) + { + $options = [ + 'auth' => [$this->username, $this->token], + ]; + if (is_array($data)) { + $options['json'] = $data; + } + if (is_array($query)) { + $options['query'] = $query; + } + + $client = new Client(); + + try { + $response = $client->request($method, $this->url . "/" . $node, $options); + } catch (ClientException $e) { + $response = $e->getResponse(); + $body = Json::decode((string) $response->getBody(), Json::FORCE_ARRAY); + throw new SmartEmailingException($body['message'], $response->getStatusCode()); + } + + return Json::decode((string) $response->getBody(), Json::FORCE_ARRAY); + } + +} diff --git a/src/SmartEmailingException.php b/src/SmartEmailingException.php new file mode 100644 index 0000000..879db21 --- /dev/null +++ b/src/SmartEmailingException.php @@ -0,0 +1,8 @@ +