Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace MauticPlugin\MauticFullContactBundle\Services;
|
||||
|
||||
use MauticPlugin\MauticFullContactBundle\Exception\NoCreditException;
|
||||
use MauticPlugin\MauticFullContactBundle\Exception\NotImplementedException;
|
||||
|
||||
/**
|
||||
* This class handles the actually HTTP request to the FullContact endpoint.
|
||||
*
|
||||
* @author Keith Casey <contrib@caseysoftware.com>
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache
|
||||
*/
|
||||
class FullContact_Base
|
||||
{
|
||||
public const REQUEST_LATENCY = 0.2;
|
||||
|
||||
public const USER_AGENT = 'caseysoftware/fullcontact-php-0.9.0';
|
||||
|
||||
private \DateTime $_next_req_time;
|
||||
|
||||
// protected $_baseUri = 'https://requestbin.fullcontact.com/1ailj6d1?';
|
||||
protected $_baseUri = 'https://api.fullcontact.com/';
|
||||
|
||||
protected $_version = 'v2';
|
||||
|
||||
protected $_resourceUri = '';
|
||||
|
||||
protected $_webhookUrl;
|
||||
|
||||
protected $_webhookId;
|
||||
|
||||
protected $_webhookJson = false;
|
||||
|
||||
protected $_supportedMethods = [];
|
||||
|
||||
public $response_obj;
|
||||
|
||||
public $response_code;
|
||||
|
||||
public $response_json;
|
||||
|
||||
/**
|
||||
* Slow down calls to the FullContact API if needed.
|
||||
*/
|
||||
private function _wait_for_rate_limit(): void
|
||||
{
|
||||
$now = new \DateTime();
|
||||
if ($this->_next_req_time->getTimestamp() > $now->getTimestamp()) {
|
||||
$t = $this->_next_req_time->getTimestamp() - $now->getTimestamp();
|
||||
sleep($t);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $hdr
|
||||
*/
|
||||
private function _update_rate_limit($hdr): void
|
||||
{
|
||||
$remaining = (float) $hdr['X-Rate-Limit-Remaining'];
|
||||
$reset = (float) $hdr['X-Rate-Limit-Reset'];
|
||||
$spacing = $reset / (1.0 + $remaining);
|
||||
$delay = $spacing - self::REQUEST_LATENCY;
|
||||
$this->_next_req_time = new \DateTime('now + '.$delay.' seconds');
|
||||
}
|
||||
|
||||
/**
|
||||
* The base constructor Sets the API key available from here:
|
||||
* http://fullcontact.com/getkey.
|
||||
*
|
||||
* @param string $_apiKey
|
||||
*/
|
||||
public function __construct(
|
||||
protected $_apiKey,
|
||||
) {
|
||||
$this->_next_req_time = new \DateTime('@0');
|
||||
}
|
||||
|
||||
/**
|
||||
* This sets the webhook url for all requests made for this service
|
||||
* instance. To unset, just use setWebhookUrl(null).
|
||||
*
|
||||
* @author David Boskovic <me@david.gs> @dboskovic
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $id
|
||||
* @param bool $json
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function setWebhookUrl($url, $id = null, $json = false)
|
||||
{
|
||||
$this->_webhookUrl = $url;
|
||||
$this->_webhookId = $id;
|
||||
$this->_webhookJson = $json;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a pretty close copy of my work on the Contactually PHP library
|
||||
* available here: http://github.com/caseysoftware/contactually-php.
|
||||
*
|
||||
* @author Keith Casey <contrib@caseysoftware.com>
|
||||
* @author David Boskovic <me@david.gs> @dboskovic
|
||||
*
|
||||
* @param array $params
|
||||
* @param array $postData
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @throws NoCreditException
|
||||
* @throws NotImplementedException
|
||||
*/
|
||||
protected function _execute($params = [], $postData = null)
|
||||
{
|
||||
if (null === $postData && !in_array($params['method'], $this->_supportedMethods, true)) {
|
||||
throw new NotImplementedException(self::class.' does not support the ['.$params['method'].'] method');
|
||||
}
|
||||
|
||||
if (array_key_exists('method', $params)) {
|
||||
unset($params['method']);
|
||||
}
|
||||
|
||||
$this->_wait_for_rate_limit();
|
||||
|
||||
$params['apiKey'] = $this->_apiKey;
|
||||
|
||||
if ($this->_webhookUrl) {
|
||||
$params['webhookUrl'] = $this->_webhookUrl;
|
||||
}
|
||||
|
||||
if ($this->_webhookId) {
|
||||
$params['webhookId'] = $this->_webhookId;
|
||||
}
|
||||
|
||||
if ($this->_webhookJson) {
|
||||
$params['webhookBody'] = 'json';
|
||||
}
|
||||
|
||||
$fullUrl = $this->_baseUri.$this->_version.$this->_resourceUri.
|
||||
'?'.http_build_query($params);
|
||||
|
||||
// open connection
|
||||
$connection = curl_init($fullUrl);
|
||||
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($connection, CURLOPT_USERAGENT, self::USER_AGENT);
|
||||
curl_setopt($connection, CURLOPT_HEADER, true); // return HTTP headers with response
|
||||
|
||||
if (null !== $postData) {
|
||||
curl_setopt($connection, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||||
curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($postData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
||||
curl_setopt($connection, CURLOPT_POST, true);
|
||||
}
|
||||
|
||||
// execute request
|
||||
$resp = curl_exec($connection);
|
||||
|
||||
[$response_headers, $this->response_json] = explode("\r\n\r\n", $resp, 2);
|
||||
// $response_headers now has a string of the HTTP headers
|
||||
// $response_json is the body of the HTTP response
|
||||
|
||||
$headers = [];
|
||||
|
||||
foreach (explode("\r\n", $response_headers) as $i => $line) {
|
||||
if (0 === $i) {
|
||||
$headers['http_code'] = $line;
|
||||
} else {
|
||||
[$key, $value] = explode(': ', $line);
|
||||
$headers[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$this->response_code = curl_getinfo($connection, CURLINFO_HTTP_CODE);
|
||||
$this->response_obj = json_decode($this->response_json);
|
||||
|
||||
if ('403' === $this->response_code) {
|
||||
throw new NoCreditException($this->response_obj->message);
|
||||
} else {
|
||||
if ('200' === $this->response_code) {
|
||||
$this->_update_rate_limit($headers);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace MauticPlugin\MauticFullContactBundle\Services;
|
||||
|
||||
/**
|
||||
* This class handles everything related to the Company lookup API.
|
||||
*
|
||||
* @author Adam Curtis <me@alc.im>
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache
|
||||
*/
|
||||
class FullContact_Batch extends FullContact_Base
|
||||
{
|
||||
protected $_resourceUri = '/batch.json';
|
||||
|
||||
/**
|
||||
* @param array $requests
|
||||
*
|
||||
* @throws \MauticPlugin\MauticFullContactBundle\Exception\FullContact_Exception_NoCredit
|
||||
* @throws \MauticPlugin\MauticFullContactBundle\Exception\FullContact_Exception_NotImplemented
|
||||
*/
|
||||
public function sendRequests($requests)
|
||||
{
|
||||
$this->_execute([], ['requests' => $requests]);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace MauticPlugin\MauticFullContactBundle\Services;
|
||||
|
||||
/**
|
||||
* This class handles everything related to the Company lookup API.
|
||||
*
|
||||
* @author Adam Curtis <me@alc.im>
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache
|
||||
*/
|
||||
class FullContact_Company extends FullContact_Base
|
||||
{
|
||||
/**
|
||||
* Supported lookup methods.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_supportedMethods = ['domain'];
|
||||
|
||||
protected $_resourceUri = '/company/lookup.json';
|
||||
|
||||
public function lookupByDomain($search)
|
||||
{
|
||||
$this->_execute(['domain' => $search, 'method' => 'domain']);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace MauticPlugin\MauticFullContactBundle\Services;
|
||||
|
||||
/**
|
||||
* This class just tells us what icons we have available.
|
||||
*
|
||||
* @author Keith Casey <contrib@caseysoftware.com>
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache
|
||||
*/
|
||||
class FullContact_Icon extends FullContact_Base
|
||||
{
|
||||
protected $_supportedMethods = ['available'];
|
||||
|
||||
protected $_resourceUri = '/icon/';
|
||||
|
||||
public function available()
|
||||
{
|
||||
$this->_execute(['method' => 'available']);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace MauticPlugin\MauticFullContactBundle\Services;
|
||||
|
||||
/**
|
||||
* This class handles all the Location information.
|
||||
*
|
||||
* @author Keith Casey <contrib@caseysoftware.com>
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache
|
||||
*/
|
||||
class FullContact_Location extends FullContact_Base
|
||||
{
|
||||
/**
|
||||
* Supported lookup methods.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_supportedMethods = ['normalizer', 'enrichment'];
|
||||
|
||||
protected $_resourceUri = '';
|
||||
|
||||
/**
|
||||
* This takes a name and breaks it into its individual parts.
|
||||
*
|
||||
* @param type $casing -> valid values are uppercase, lowercase, titlecase
|
||||
*
|
||||
* @return type
|
||||
*/
|
||||
public function normalizer($place, $includeZeroPopulation = false, $casing = 'titlecase')
|
||||
{
|
||||
$includeZeroPopulation = ($includeZeroPopulation) ? 'true' : 'false';
|
||||
|
||||
$this->_resourceUri = '/address/locationNormalizer.json';
|
||||
$this->_execute(['place' => $place, 'includeZeroPopulation' => $includeZeroPopulation,
|
||||
'method' => 'normalizer', 'casing' => $casing, ]);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
|
||||
public function enrichment($place, $includeZeroPopulation = false, $casing = 'titlecase')
|
||||
{
|
||||
$includeZeroPopulation = ($includeZeroPopulation) ? 'true' : 'false';
|
||||
|
||||
$this->_resourceUri = '/address/locationEnrichment.json';
|
||||
$this->_execute(['place' => $place, 'includeZeroPopulation' => $includeZeroPopulation,
|
||||
'method' => 'enrichment', 'casing' => $casing, ]);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace MauticPlugin\MauticFullContactBundle\Services;
|
||||
|
||||
/**
|
||||
* This class handles everything related to names that aren't person-based info lookup.
|
||||
*
|
||||
* @author Keith Casey <contrib@caseysoftware.com>
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache
|
||||
*/
|
||||
class FullContact_Name extends FullContact_Base
|
||||
{
|
||||
/**
|
||||
* Supported lookup methods.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_supportedMethods = ['normalizer', 'deducer', 'similarity', 'stats', 'parser'];
|
||||
|
||||
protected $_resourceUri = '';
|
||||
|
||||
/**
|
||||
* This takes a name and breaks it into its individual parts.
|
||||
*
|
||||
* @param type $name
|
||||
* @param type $casing -> valid values are uppercase, lowercase, titlecase
|
||||
*
|
||||
* @return type
|
||||
*/
|
||||
public function normalizer($name, $casing = 'titlecase')
|
||||
{
|
||||
$this->_resourceUri = '/name/normalizer.json';
|
||||
$this->_execute(['q' => $name, 'method' => 'normalizer', 'casing' => $casing]);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* This resolves a person's name from either their email address or a
|
||||
* username. This is basically a wrapper for the Person lookup methods.
|
||||
*
|
||||
* @param type $type -> valid values are email and username
|
||||
* @param type $casing -> valid values are uppercase, lowercase, titlecase
|
||||
*
|
||||
* @return type
|
||||
*/
|
||||
public function deducer($value, $type = 'email', $casing = 'titlecase')
|
||||
{
|
||||
$this->_resourceUri = '/name/deducer.json';
|
||||
$this->_execute([$type => $value, 'method' => 'deducer', 'casing' => $casing]);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* These are two names to compare.
|
||||
*
|
||||
* @param type $name1
|
||||
* @param type $name2
|
||||
* @param type $casing
|
||||
*
|
||||
* @return type
|
||||
*/
|
||||
public function similarity($name1, $name2, $casing = 'titlecase')
|
||||
{
|
||||
$this->_resourceUri = '/name/similarity.json';
|
||||
$this->_execute(['q1' => $name1, 'q2' => $name2, 'method' => 'similarity', 'casing' => $casing]);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
|
||||
public function stats($value, $type = 'givenName', $casing = 'titlecase')
|
||||
{
|
||||
$this->_resourceUri = '/name/stats.json';
|
||||
$this->_execute([$type => $value, 'method' => 'stats', 'casing' => $casing]);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
|
||||
public function parser($name, $casing = 'titlecase')
|
||||
{
|
||||
$this->_resourceUri = '/name/parser.json';
|
||||
$this->_execute(['q' => $name, 'method' => 'parser', 'casing' => $casing]);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace MauticPlugin\MauticFullContactBundle\Services;
|
||||
|
||||
/**
|
||||
* This class handles everything related to the Person lookup API.
|
||||
*
|
||||
* @author Keith Casey <contrib@caseysoftware.com>
|
||||
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache
|
||||
*/
|
||||
class FullContact_Person extends FullContact_Base
|
||||
{
|
||||
/**
|
||||
* Supported lookup methods.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $_supportedMethods = ['email', 'phone', 'twitter'];
|
||||
|
||||
protected $_resourceUri = '/person.json';
|
||||
|
||||
public function lookupByEmail($search)
|
||||
{
|
||||
$this->_execute(['email' => $search, 'method' => 'email']);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
|
||||
public function lookupByEmailMD5($search)
|
||||
{
|
||||
$this->_execute(['emailMD5' => $search, 'method' => 'email']);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
|
||||
public function lookupByPhone($search)
|
||||
{
|
||||
$this->_execute(['phone' => $search, 'method' => 'phone']);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
|
||||
public function lookupByTwitter($search)
|
||||
{
|
||||
$this->_execute(['twitter' => $search, 'method' => 'twitter']);
|
||||
|
||||
return $this->response_obj;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user