Skip to content

Commit

Permalink
Split Transactional / Newsletter Api
Browse files Browse the repository at this point in the history
  • Loading branch information
kpitn committed Oct 22, 2019
1 parent 84685c0 commit 8a63035
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 29 deletions.
50 changes: 35 additions & 15 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
*/
class Config extends AbstractHelper
{
public const KIND_TRANSACTIONAL = 'TRANSACTIONAL';
public const KIND_NEWSLETTER = 'NEWSLETTER';
public const PATH_ACTIVE = 'mailjet/general/active';
public const PATH_API_KEY_PUBLIC = 'mailjet/general/api_key_public';
public const PATH_API_KEY_PRIVATE = 'mailjet/general/api_key_private';
public const PATH_CONTACT_LIST = 'mailjet/general/contact_list';
public const PATH_TRANSACTIONAL_API_KEY_PUBLIC = 'mailjet/transactional/api_key_public';
public const PATH_TRANSACTIONAL_API_KEY_PRIVATE = 'mailjet/transactional/api_key_private';
public const PATH_NEWSLETTER_API_KEY_PUBLIC = 'mailjet/newsletter/api_key_public';
public const PATH_NEWSLETTER_API_KEY_PRIVATE = 'mailjet/newsletter/api_key_private';
public const PATH_NEWSLETTER_CONTACT_LIST = 'mailjet/newsletter/contact_list';
public const PATH_TEST_IS_ACTIVE = 'mailjet/test_mode/is_active';
public const PATH_TEST_EMAIL = 'mailjet/test_mode/email';

Expand All @@ -43,9 +47,9 @@ class Config extends AbstractHelper
/**
* Config constructor.
*
* @param EncryptorInterface $encryptor
* @param ScopeConfigInterface $scopeConfig
* @param Context $context
* @param EncryptorInterface $encryptor
* @param ScopeConfigInterface $scopeConfig
* @param Context $context
*/
public function __construct(
EncryptorInterface $encryptor,
Expand All @@ -54,8 +58,8 @@ public function __construct(
) {
parent::__construct($context);

$this->encryptor = $encryptor;
$this->scopeConfig = $scopeConfig;
$this->encryptor = $encryptor;
$this->scopeConfig = $scopeConfig;
}

/**
Expand All @@ -72,23 +76,39 @@ public function active($storeId = null): bool
/**
* get Api Public Key
*
* @param null $storeId
* @param string $kind
* @param null $storeId
* @return string
*/
public function getApiKeyPublic($storeId = null): string
public function getApiKeyPublic(string $kind, $storeId = null): string
{
return (string)$this->getConfigValue(self::PATH_API_KEY_PUBLIC, $storeId);
/** @var string $key */
if ($kind === self::KIND_TRANSACTIONAL) {
$key = self::PATH_TRANSACTIONAL_API_KEY_PUBLIC;
} else {
$key = self::PATH_NEWSLETTER_API_KEY_PUBLIC;
}

return (string)$this->getConfigValue($key, $storeId);
}

/**
* get Api Secret Key
*
* @param null $storeId
* @param string $kind
* @param null $storeId
* @return string
*/
public function getApiKeyPrivate($storeId = null): string
public function getApiKeyPrivate(string $kind, $storeId = null): string
{
return (string)$this->encryptor->decrypt($this->getConfigValue(self::PATH_API_KEY_PRIVATE, $storeId));
/** @var string $key */
if ($kind === self::KIND_TRANSACTIONAL) {
$key = self::PATH_TRANSACTIONAL_API_KEY_PRIVATE;
} else {
$key = self::PATH_NEWSLETTER_API_KEY_PRIVATE;
}

return (string)$this->encryptor->decrypt($this->getConfigValue($key, $storeId));
}

/**
Expand All @@ -99,7 +119,7 @@ public function getApiKeyPrivate($storeId = null): string
*/
public function getContactList($storeId = null): string
{
return (string)$this->getConfigValue(self::PATH_CONTACT_LIST, $storeId);
return (string)$this->getConfigValue(self::PATH_NEWSLETTER_CONTACT_LIST, $storeId);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Model/Webservice/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Web200\Mailjet\Model\Webservice;

use Web200\Mailjet\Helper\Config;
use \Mailjet\Resources;

/**
Expand Down Expand Up @@ -52,7 +53,7 @@ public function update($subscribe, $email, $name, $properties)
}

try {
$api = $this->initApi();
$api = $this->initApi(Config::KIND_NEWSLETTER);
$body = [
'ContactsLists' => [
[
Expand Down
22 changes: 21 additions & 1 deletion Model/Webservice/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Web200\Mailjet\Model\Webservice;

use \Mailjet\Resources;
use Web200\Mailjet\Helper\Config;

/**
* Class Email
Expand Down Expand Up @@ -70,7 +71,7 @@ class Email extends Webservice
public function send()
{
try {
$api = $this->initApi();
$api = $this->initApi($this->getKind());

$message = [];
$message['From'] = [
Expand Down Expand Up @@ -118,6 +119,25 @@ public function send()
}
}

/**
* Get Kind Email
*
* @return string
*/
protected function getKind(): string
{
$kindEmail = Config::KIND_TRANSACTIONAL;
if ($this->getVariables() && count($this->getVariables()) > 0) {
if (isset($this->getVariables()['kind_email'])) {
$kindEmail = $this->getVariables()['kind_email'];
} elseif ($this->getVariables()['subscriber_id']) {
$kindEmail = Config::KIND_NEWSLETTER;
}
}

return $kindEmail;
}

/**
* Get From Email
*
Expand Down
3 changes: 2 additions & 1 deletion Model/Webservice/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Web200\Mailjet\Model\Webservice;

use \Mailjet\Resources;
use Web200\Mailjet\Helper\Config;

/**
* Class Template
Expand Down Expand Up @@ -87,7 +88,7 @@ public function getTemplates(): array
{
if ($this->cacheTemplates === null) {
try {
$api = $this->initApi();
$api = $this->initApi(Config::KIND_TRANSACTIONAL);
$filters = [
'OwnerType' => $this->getOwnerType(),
'Limit' => $this->getLimit()
Expand Down
16 changes: 9 additions & 7 deletions Model/Webservice/Webservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ public function __construct(
/**
* Init Api
*
* @param string $kind
* @return MailjetClient
* @throws LocalizedException
*/
protected function initApi(): MailjetClient
protected function initApi(string $kind): MailjetClient
{
$this->checkLogin();
$this->checkLogin($kind);

$api = new MailjetClient(
$this->config->getApiKeyPublic($this->getStoreId()),
$this->config->getApiKeyPrivate($this->getStoreId()),
$this->config->getApiKeyPublic($kind, $this->getStoreId()),
$this->config->getApiKeyPrivate($kind, $this->getStoreId()),
true,
['version' => $this->apiVersion]
);
Expand All @@ -74,15 +75,16 @@ protected function initApi(): MailjetClient
/**
* Check login information
*
* @param string $kind
* @return bool
* @throws LocalizedException
*/
protected function checkLogin(): bool
protected function checkLogin(string $kind): bool
{
if ($this->config->getApiKeyPublic($this->getStoreId()) === '') {
if ($this->config->getApiKeyPublic($kind, $this->getStoreId()) === '') {
throw new LocalizedException(__('Api Public Key empty'));
}
if ($this->config->getApiKeyPrivate($this->getStoreId()) === '') {
if ($this->config->getApiKeyPrivate($kind, $this->getStoreId()) === '') {
throw new LocalizedException(__('Api Secret Key empty'));
}
return true;
Expand Down
21 changes: 17 additions & 4 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,31 @@
<label>Enabled</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="api_key_public" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
</group>
<group id="transactional" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Mailjet Transactional Configuration</label>
<field id="api_key_public" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>API Key</label>
</field>
<field id="api_key_private" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Secret Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
</group>
<group id="newsletter" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Mailjet Newsletter Configuration</label>
<field id="api_key_public" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
<label>API Key</label>
</field>
<field id="api_key_private" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="api_key_private" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Secret Key</label>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
<field id="contact_list" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="contact_list" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Contact list</label>
</field>
</group>
<group id="test_mode" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="0">
<group id="test_mode" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Test Mode</label>
<field id="is_active" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Is Active</label>
Expand Down

0 comments on commit 8a63035

Please sign in to comment.