MailchimpEcommerceHelper.php in Mailchimp E-Commerce 8
File
src/MailchimpEcommerceHelper.php
View source
<?php
namespace Drupal\mailchimp_ecommerce;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Mailchimp\MailchimpCampaigns;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class MailchimpEcommerceHelper implements ContainerInjectionInterface {
protected $mcCampaigns;
protected $messenger;
protected $request;
protected $config;
public function __construct(MailchimpCampaigns $mc_campaigns, MessengerInterface $messenger, Request $request, ImmutableConfig $config) {
$this->mcCampaigns = $mc_campaigns;
$this->messenger = $messenger;
$this->request = $request;
$this->config = $config;
}
public static function create(ContainerInterface $container) {
return new static(\mailchimp_get_api_object('MailchimpCampaigns'), $container
->get('messenger'), $container
->get('request_stack')
->getCurrentRequest(), $container
->get('config.factory')
->get('mailchimp_ecommerce.settings'));
}
public function validateCustomer(array $customer) {
return isset($customer['id']) && !empty($customer['id']);
}
public function getCampaignId() {
$session_campaign = $this->request
->getSession()
->get('mc_cid', '');
$campaign_id = '';
try {
if (!empty($session_campaign)) {
$campaign = $this->mcCampaigns
->getCampaign($session_campaign);
$campaign_id = $campaign->id;
}
} catch (\Exception $e) {
if ($e
->getCode() == 404) {
}
else {
$this->messenger
->addError($e
->getMessage());
}
}
return $campaign_id;
}
public function getCurrencyCodes() {
$currencyfile = drupal_get_path('module', 'mailchimp_ecommerce') . '/' . 'currency-codes-iso4217.xml';
$currencydata = simplexml_load_file($currencyfile);
$json_string = json_encode($currencydata);
$result_array = json_decode($json_string, TRUE);
$currencycodes = [];
foreach ($result_array['CcyTbl']['CcyNtry'] as $item) {
if (!empty($item['Ccy'])) {
$currencycodes[$item['Ccy']] = $item['CcyNm'] . ' (' . $item['Ccy'] . ')';
}
}
return $currencycodes;
}
public function getStoreId() {
return $this->config
->get('mailchimp_ecommerce_store_id');
}
public function isEnabled() {
return $this->config
->get('mailchimp_ecommerce_integration') !== '';
}
}