Loading hubspot_integration.services.yml +1 −1 Original line number Diff line number Diff line services: hubspot_integration.api: class: Drupal\hubspot_integration\Services\HubspotAPI arguments: ['@database', '@session', '@config.factory'] arguments: ['@database', '@session', '@config.factory', '@messenger', '@http_client', '@request_stack'] src/Services/HubspotAPI.php +170 −76 Original line number Diff line number Diff line Loading @@ -3,26 +3,40 @@ namespace Drupal\hubspot_integration\Services; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Database\Connection; use Symfony\Component\HttpFoundation\Session\Session; use Exception; use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Database\Connection; use Drupal\Core\Http\RequestStack; use Drupal\Core\Messenger\MessengerInterface; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; use GuzzleHttp\ClientInterface; use Symfony\Component\HttpFoundation\Session\Session; /** * The Hubspot API service. */ class HubspotAPI { /* * @var \Drupal\Core\Database\Connection $database /** * The database connection service. * * @var \Drupal\Core\Database\Connection */ protected $database; /* * @var \Symfony\Component\HttpFoundation\Session\Session $session /** * The session object. * * @var \Symfony\Component\HttpFoundation\Session\Session */ protected $session; /* * @var \Drupal\Core\Config\ConfigFactory $config_factory /** * The config factory service. * * @var \Drupal\Core\Config\ConfigFactory */ protected $config_factory; protected $configFactory; /** * Static cache for cookies. Loading @@ -31,16 +45,50 @@ class HubspotAPI { */ protected $cookies; /** * The messenger service. * * @var \Drupal\Core\Messenger\MessengerInterface */ protected MessengerInterface $messenger; /** * The http client service. * * @var \GuzzleHttp\ClientInterface */ protected ClientInterface $httpClient; /** * The request stack service. * * @var \Drupal\Core\Http\RequestStack */ protected RequestStack $requestStack; /** * Constructs a new HubspotAPI object. * * @param \Drupal\Core\Database\Connection $connection * The database connection service. * @param \Symfony\Component\HttpFoundation\Session\Session $session * The session object. * @param \Drupal\Core\Config\ConfigFactory $config_factory * The config factory service. * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The messenger service. * @param \GuzzleHttp\ClientInterface $http_client * The http client service. * @param \Drupal\Core\Http\RequestStack $request_stack * The request stack service. */ public function __construct(Connection $connection, Session $session, ConfigFactory $config_factory) { public function __construct(Connection $connection, Session $session, ConfigFactory $config_factory, MessengerInterface $messenger, ClientInterface $http_client, RequestStack $request_stack) { $this->database = $connection; $this->session = $session; $this->config_factory = $config_factory; $this->configFactory = $config_factory; $this->messenger = $messenger; $this->httpClient = $http_client; $this->requestStack = $request_stack; } /** Loading @@ -54,8 +102,7 @@ class HubspotAPI { */ public function getCookie($cookieName = 'hubspotutk') { if (!isset($this->cookies[$cookieName])) { $this->cookies[$cookieName] = \Drupal::request()->cookies->get($cookieName); $this->cookies[$cookieName] = $this->requestStack->getCurrentRequest()->cookies->get($cookieName); // Ensure data in cookie is authorized / safe for hubspot_integration. if (($cookieName == 'hubspot_integration') && ($tids = explode('_', ltrim($this->cookies[$cookieName], 'hubspot_'))) Loading @@ -69,7 +116,6 @@ class HubspotAPI { } } } $tids = array_intersect($tids, $allowedValues); $this->cookies[$cookieName] = 'hubspot_' . implode('_', $tids); } Loading @@ -79,7 +125,7 @@ class HubspotAPI { } /** * Get current session * Get current session. */ public function getSession() { return $this->session; Loading @@ -93,7 +139,6 @@ class HubspotAPI { */ public function isContact() { $result = FALSE; if ($user = $this->getContactInfo()) { $property = 'is-contact'; Loading @@ -106,12 +151,15 @@ class HubspotAPI { } /** * Get Husbspot values for the current user * Get Husbspot values for the current user. * * @param boolean $forced_update * @return NULL|mixed * @param bool $forced_update * Forced update boolean. * * @return null|mixed * Null or user object. */ public function getContactInfo($forced_update=false) { public function getContactInfo($forced_update = FALSE) { $user = NULL; if (!$this->isDemoMode() && !$forced_update && $this->session->get('hubspot_integration.user')) { $user = $this->session->get('hubspot_integration.user'); Loading @@ -126,41 +174,59 @@ class HubspotAPI { } /** * Get the config name for the hubspot integration module settings * Get the config name for the hubspot integration module settings. * * @param string $name * The config name. * * @param unknown $name * @return mixed|NULL|array|\Drupal\Component\Render\MarkupInterface|string|unknown[]|array[] * @return mixed|null|array|\Drupal\Component\Render\MarkupInterface|string|unknown[]|array[] * The config property value. */ public function getConfig($name) { return $this->config_factory->get('hubspot_integration.settings')->get($name); public function getConfig(string $name) { return $this->configFactory->get('hubspot_integration.settings')->get($name); } /** * Set the config name for the hubspot integration module settings * Set the config name for the hubspot integration module settings. * * @param unknown $name * @return mixed|NULL|array|\Drupal\Component\Render\MarkupInterface|string|unknown[]|array[] * @param string $name * The config name. * @param string $value * The config value. * * @return mixed|null|array|\Drupal\Component\Render\MarkupInterface|string|array[] * The config object. */ public function setConfig($name, $value) { return $this->config_factory->getEditable('hubspot_integration.settings')->set($name, $value)->save(); public function setConfig(string $name, string $value) { return $this->configFactory->getEditable('hubspot_integration.settings')->set($name, $value)->save(); } /** * Get the list of the possible contact properties and their definitions * Get the list of the possible contact properties and their definitions. * * @return mixed * Mixed. */ public function getContactProperties() { return $this->request('properties/v1/contacts/properties'); } /** * Function to make calls on hubspot * Function to make calls on hubspot. * * @param unknown $api * @return mixed * @param string $api * Api. * @param array $options * Options. * @param string $type * Type. * @param array $datas * Datas. * * @return mixed|null * Mixed. */ public function request($api, $options = [], $type='GET', $datas = []) { public function request(string $api, array $options = [], string $type = 'GET', array $datas = []) { $api_key = $this->getConfig('api_key'); $ret = NULL; if (!empty($api_key)) { Loading @@ -172,15 +238,16 @@ class HubspotAPI { try { switch ($type) { case 'GET': $request = \Drupal::httpClient()->get($url, []); $request = $this->httpClient->get($url, []); break; case 'POST': $request = \Drupal::httpClient()->post($url, ['json' => $datas], $options); $request = $this->httpClient->post($url, ['json' => $datas], $options); break; } $ret = json_decode($request->getBody()); } catch (Exception $e) { catch (\Exception $e) { \Drupal::logger('hubspot_integration')->error('Error : ' . $e->getCode() . ' : ' . $e->getMessage()); } } Loading @@ -188,39 +255,43 @@ class HubspotAPI { } /** * Get the mapping done between Drupal and Hubspot * Get the mapping done between Drupal and Hubspot. * * @return mixed * Mixed. */ public function getMapping() { return $this->getConfig('mapping'); } /** * Get the sort configuration * Get the sort configuration. * * @return mixed * Mixed. */ public function getSort($tids = array()) { public function getSort() { return $this->getConfig('sort'); } /** * Get the sort configuration * Get the sort configuration. * * @param array $tids * Array. * * @return mixed * Mixed. */ public function getTidsSort($tids) { public function getTidsSort(array $tids) { $sort = $this->getConfig('sort'); $tids_sort = array(); if (!empty($tids[0])) { // compile the sort in order to get the min values for the tids $tids_sort = []; // Compile the sort in order to get the min values for the tids. if (!empty($tids[0])) { foreach ($tids as $tid) { /** @var \Drupal\taxonomy\TermInterface $term */ $term = Term::load($tid); $vid = $term->bundle(); foreach ($sort[$vid][$tid] as $term_weight_id => $term_weight) { if (!isset($tids_sort[$term_weight_id])) { $tids_sort[$term_weight_id] = $sort[$vid][$tid][$term_weight_id]; Loading @@ -236,23 +307,29 @@ class HubspotAPI { } /** * Utility function to transform a text as a machine name * Utility function to transform a text as a machine name. * * @param string $string * String. * * @param unknown $string * @return string * String. */ public static function machineName($string) { public static function machineName(string $string) { return preg_replace('@[^a-z0-9-]+@', '-', strtolower($string)); } /** * Utility function to check a vocabulary is mapped with hubspot * Utility function to check a vocabulary is mapped with hubspot. * * @param string $vocabulary * @return boolean * Vocabulary name. * * @return bool * Bool. */ public function isVocabularyMapped($vocabulary) { public function isVocabularyMapped(string $vocabulary) { $mapping = $this->getMapping(); if (!empty($mapping)) { foreach ($mapping as $mapping) { if (isset($mapping['#vocabulary']) && $mapping['#vocabulary'] === $vocabulary) { Loading @@ -261,17 +338,18 @@ class HubspotAPI { } } return false; return FALSE; } /** * Utility function to get mapped vocabularies * Utility function to get mapped vocabularies. * * @return array * Array. */ function getMappedVocabularies() { public function getMappedVocabularies() { $vocabularies = Vocabulary::loadMultiple(); $vocabulariesList = []; foreach ($vocabularies as $vid => $vocablary) { if ($this->isVocabularyMapped($vid)) { $vocabulariesList[$vid] = $vocablary->get('name'); Loading @@ -281,20 +359,32 @@ class HubspotAPI { return $vocabulariesList; } /** * Description. */ public function isDemoMode() { return $this->getConfig('demo_mode'); } /** * Description. */ public function anonymousContactCreationEnabled() { return $this->getConfig('anonymous_contact'); } /** * Description. */ public function demoMessage($message) { if ($this->isDemoMode()) { \Drupal::messenger()->addStatus("Demo Hubspot : $message"); $this->messenger->addStatus("Demo Hubspot: $message"); } } /** * Description. */ public function getUserTids() { $tids = []; Loading @@ -303,7 +393,6 @@ class HubspotAPI { && !empty($mapping) ) { $taxonomy = []; foreach ($mapping as $hub_voc => $map) { if (isset($hub_user->properties->{$hub_voc})) { foreach ($map['#terms'] as $hub_term_id => $taxonomy_term_id) { Loading @@ -314,7 +403,6 @@ class HubspotAPI { } } } foreach ($taxonomy as $tid => $vocab) { $tids[] = $tid; } Loading @@ -323,8 +411,11 @@ class HubspotAPI { return $tids; } public function getAnonymousContactNumber($update = false) { if ($update == true) { /** * Description. */ public function getAnonymousContactNumber($update = FALSE) { if ($update == TRUE) { $request = $this->request('contacts/v1/search/query', ['q' => $this->getAnonymousContactMailTemplate('*')]); if (!empty($request)) { $this->setConfig('anonymous_contacts_count', $request->total); Loading @@ -339,12 +430,15 @@ class HubspotAPI { } } /** * Description. */ public function getAnonymousContactMailTemplate($hub_cookie = NULL) { $template = $this->getConfig('anonymous_mail_template'); if (!empty($hub_cookie)) { $template = str_replace('[hub_cookie]', $hub_cookie, $template); } return $template; } } Loading
hubspot_integration.services.yml +1 −1 Original line number Diff line number Diff line services: hubspot_integration.api: class: Drupal\hubspot_integration\Services\HubspotAPI arguments: ['@database', '@session', '@config.factory'] arguments: ['@database', '@session', '@config.factory', '@messenger', '@http_client', '@request_stack']
src/Services/HubspotAPI.php +170 −76 Original line number Diff line number Diff line Loading @@ -3,26 +3,40 @@ namespace Drupal\hubspot_integration\Services; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Database\Connection; use Symfony\Component\HttpFoundation\Session\Session; use Exception; use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Database\Connection; use Drupal\Core\Http\RequestStack; use Drupal\Core\Messenger\MessengerInterface; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; use GuzzleHttp\ClientInterface; use Symfony\Component\HttpFoundation\Session\Session; /** * The Hubspot API service. */ class HubspotAPI { /* * @var \Drupal\Core\Database\Connection $database /** * The database connection service. * * @var \Drupal\Core\Database\Connection */ protected $database; /* * @var \Symfony\Component\HttpFoundation\Session\Session $session /** * The session object. * * @var \Symfony\Component\HttpFoundation\Session\Session */ protected $session; /* * @var \Drupal\Core\Config\ConfigFactory $config_factory /** * The config factory service. * * @var \Drupal\Core\Config\ConfigFactory */ protected $config_factory; protected $configFactory; /** * Static cache for cookies. Loading @@ -31,16 +45,50 @@ class HubspotAPI { */ protected $cookies; /** * The messenger service. * * @var \Drupal\Core\Messenger\MessengerInterface */ protected MessengerInterface $messenger; /** * The http client service. * * @var \GuzzleHttp\ClientInterface */ protected ClientInterface $httpClient; /** * The request stack service. * * @var \Drupal\Core\Http\RequestStack */ protected RequestStack $requestStack; /** * Constructs a new HubspotAPI object. * * @param \Drupal\Core\Database\Connection $connection * The database connection service. * @param \Symfony\Component\HttpFoundation\Session\Session $session * The session object. * @param \Drupal\Core\Config\ConfigFactory $config_factory * The config factory service. * @param \Drupal\Core\Messenger\MessengerInterface $messenger * The messenger service. * @param \GuzzleHttp\ClientInterface $http_client * The http client service. * @param \Drupal\Core\Http\RequestStack $request_stack * The request stack service. */ public function __construct(Connection $connection, Session $session, ConfigFactory $config_factory) { public function __construct(Connection $connection, Session $session, ConfigFactory $config_factory, MessengerInterface $messenger, ClientInterface $http_client, RequestStack $request_stack) { $this->database = $connection; $this->session = $session; $this->config_factory = $config_factory; $this->configFactory = $config_factory; $this->messenger = $messenger; $this->httpClient = $http_client; $this->requestStack = $request_stack; } /** Loading @@ -54,8 +102,7 @@ class HubspotAPI { */ public function getCookie($cookieName = 'hubspotutk') { if (!isset($this->cookies[$cookieName])) { $this->cookies[$cookieName] = \Drupal::request()->cookies->get($cookieName); $this->cookies[$cookieName] = $this->requestStack->getCurrentRequest()->cookies->get($cookieName); // Ensure data in cookie is authorized / safe for hubspot_integration. if (($cookieName == 'hubspot_integration') && ($tids = explode('_', ltrim($this->cookies[$cookieName], 'hubspot_'))) Loading @@ -69,7 +116,6 @@ class HubspotAPI { } } } $tids = array_intersect($tids, $allowedValues); $this->cookies[$cookieName] = 'hubspot_' . implode('_', $tids); } Loading @@ -79,7 +125,7 @@ class HubspotAPI { } /** * Get current session * Get current session. */ public function getSession() { return $this->session; Loading @@ -93,7 +139,6 @@ class HubspotAPI { */ public function isContact() { $result = FALSE; if ($user = $this->getContactInfo()) { $property = 'is-contact'; Loading @@ -106,12 +151,15 @@ class HubspotAPI { } /** * Get Husbspot values for the current user * Get Husbspot values for the current user. * * @param boolean $forced_update * @return NULL|mixed * @param bool $forced_update * Forced update boolean. * * @return null|mixed * Null or user object. */ public function getContactInfo($forced_update=false) { public function getContactInfo($forced_update = FALSE) { $user = NULL; if (!$this->isDemoMode() && !$forced_update && $this->session->get('hubspot_integration.user')) { $user = $this->session->get('hubspot_integration.user'); Loading @@ -126,41 +174,59 @@ class HubspotAPI { } /** * Get the config name for the hubspot integration module settings * Get the config name for the hubspot integration module settings. * * @param string $name * The config name. * * @param unknown $name * @return mixed|NULL|array|\Drupal\Component\Render\MarkupInterface|string|unknown[]|array[] * @return mixed|null|array|\Drupal\Component\Render\MarkupInterface|string|unknown[]|array[] * The config property value. */ public function getConfig($name) { return $this->config_factory->get('hubspot_integration.settings')->get($name); public function getConfig(string $name) { return $this->configFactory->get('hubspot_integration.settings')->get($name); } /** * Set the config name for the hubspot integration module settings * Set the config name for the hubspot integration module settings. * * @param unknown $name * @return mixed|NULL|array|\Drupal\Component\Render\MarkupInterface|string|unknown[]|array[] * @param string $name * The config name. * @param string $value * The config value. * * @return mixed|null|array|\Drupal\Component\Render\MarkupInterface|string|array[] * The config object. */ public function setConfig($name, $value) { return $this->config_factory->getEditable('hubspot_integration.settings')->set($name, $value)->save(); public function setConfig(string $name, string $value) { return $this->configFactory->getEditable('hubspot_integration.settings')->set($name, $value)->save(); } /** * Get the list of the possible contact properties and their definitions * Get the list of the possible contact properties and their definitions. * * @return mixed * Mixed. */ public function getContactProperties() { return $this->request('properties/v1/contacts/properties'); } /** * Function to make calls on hubspot * Function to make calls on hubspot. * * @param unknown $api * @return mixed * @param string $api * Api. * @param array $options * Options. * @param string $type * Type. * @param array $datas * Datas. * * @return mixed|null * Mixed. */ public function request($api, $options = [], $type='GET', $datas = []) { public function request(string $api, array $options = [], string $type = 'GET', array $datas = []) { $api_key = $this->getConfig('api_key'); $ret = NULL; if (!empty($api_key)) { Loading @@ -172,15 +238,16 @@ class HubspotAPI { try { switch ($type) { case 'GET': $request = \Drupal::httpClient()->get($url, []); $request = $this->httpClient->get($url, []); break; case 'POST': $request = \Drupal::httpClient()->post($url, ['json' => $datas], $options); $request = $this->httpClient->post($url, ['json' => $datas], $options); break; } $ret = json_decode($request->getBody()); } catch (Exception $e) { catch (\Exception $e) { \Drupal::logger('hubspot_integration')->error('Error : ' . $e->getCode() . ' : ' . $e->getMessage()); } } Loading @@ -188,39 +255,43 @@ class HubspotAPI { } /** * Get the mapping done between Drupal and Hubspot * Get the mapping done between Drupal and Hubspot. * * @return mixed * Mixed. */ public function getMapping() { return $this->getConfig('mapping'); } /** * Get the sort configuration * Get the sort configuration. * * @return mixed * Mixed. */ public function getSort($tids = array()) { public function getSort() { return $this->getConfig('sort'); } /** * Get the sort configuration * Get the sort configuration. * * @param array $tids * Array. * * @return mixed * Mixed. */ public function getTidsSort($tids) { public function getTidsSort(array $tids) { $sort = $this->getConfig('sort'); $tids_sort = array(); if (!empty($tids[0])) { // compile the sort in order to get the min values for the tids $tids_sort = []; // Compile the sort in order to get the min values for the tids. if (!empty($tids[0])) { foreach ($tids as $tid) { /** @var \Drupal\taxonomy\TermInterface $term */ $term = Term::load($tid); $vid = $term->bundle(); foreach ($sort[$vid][$tid] as $term_weight_id => $term_weight) { if (!isset($tids_sort[$term_weight_id])) { $tids_sort[$term_weight_id] = $sort[$vid][$tid][$term_weight_id]; Loading @@ -236,23 +307,29 @@ class HubspotAPI { } /** * Utility function to transform a text as a machine name * Utility function to transform a text as a machine name. * * @param string $string * String. * * @param unknown $string * @return string * String. */ public static function machineName($string) { public static function machineName(string $string) { return preg_replace('@[^a-z0-9-]+@', '-', strtolower($string)); } /** * Utility function to check a vocabulary is mapped with hubspot * Utility function to check a vocabulary is mapped with hubspot. * * @param string $vocabulary * @return boolean * Vocabulary name. * * @return bool * Bool. */ public function isVocabularyMapped($vocabulary) { public function isVocabularyMapped(string $vocabulary) { $mapping = $this->getMapping(); if (!empty($mapping)) { foreach ($mapping as $mapping) { if (isset($mapping['#vocabulary']) && $mapping['#vocabulary'] === $vocabulary) { Loading @@ -261,17 +338,18 @@ class HubspotAPI { } } return false; return FALSE; } /** * Utility function to get mapped vocabularies * Utility function to get mapped vocabularies. * * @return array * Array. */ function getMappedVocabularies() { public function getMappedVocabularies() { $vocabularies = Vocabulary::loadMultiple(); $vocabulariesList = []; foreach ($vocabularies as $vid => $vocablary) { if ($this->isVocabularyMapped($vid)) { $vocabulariesList[$vid] = $vocablary->get('name'); Loading @@ -281,20 +359,32 @@ class HubspotAPI { return $vocabulariesList; } /** * Description. */ public function isDemoMode() { return $this->getConfig('demo_mode'); } /** * Description. */ public function anonymousContactCreationEnabled() { return $this->getConfig('anonymous_contact'); } /** * Description. */ public function demoMessage($message) { if ($this->isDemoMode()) { \Drupal::messenger()->addStatus("Demo Hubspot : $message"); $this->messenger->addStatus("Demo Hubspot: $message"); } } /** * Description. */ public function getUserTids() { $tids = []; Loading @@ -303,7 +393,6 @@ class HubspotAPI { && !empty($mapping) ) { $taxonomy = []; foreach ($mapping as $hub_voc => $map) { if (isset($hub_user->properties->{$hub_voc})) { foreach ($map['#terms'] as $hub_term_id => $taxonomy_term_id) { Loading @@ -314,7 +403,6 @@ class HubspotAPI { } } } foreach ($taxonomy as $tid => $vocab) { $tids[] = $tid; } Loading @@ -323,8 +411,11 @@ class HubspotAPI { return $tids; } public function getAnonymousContactNumber($update = false) { if ($update == true) { /** * Description. */ public function getAnonymousContactNumber($update = FALSE) { if ($update == TRUE) { $request = $this->request('contacts/v1/search/query', ['q' => $this->getAnonymousContactMailTemplate('*')]); if (!empty($request)) { $this->setConfig('anonymous_contacts_count', $request->total); Loading @@ -339,12 +430,15 @@ class HubspotAPI { } } /** * Description. */ public function getAnonymousContactMailTemplate($hub_cookie = NULL) { $template = $this->getConfig('anonymous_mail_template'); if (!empty($hub_cookie)) { $template = str_replace('[hub_cookie]', $hub_cookie, $template); } return $template; } }