Skip to content

Commit

Permalink
Update CRM.php
Browse files Browse the repository at this point in the history
  • Loading branch information
torleif authored Mar 22, 2021
1 parent 1c4c4f6 commit 39f4dac
Showing 1 changed file with 15 additions and 65 deletions.
80 changes: 15 additions & 65 deletions code/CRM.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class CRM
// the token used to send and retrive data from the CRM API REST service
private $access_token = '';

// connection paramaters (if overwritten)
private $paramsoverwrite = null;

/*
* @config default headers used for communication
*/
Expand All @@ -30,22 +27,13 @@ class CRM
"Accept" => "application/json"
];

/*
* @config url of the target
*/
private static $endpoint = "https://login.microsoftonline.com/common/oauth2/token";

/**
* Builds the query used to fetch the token. is able to use
* client_credentials or password authentication methods
*
* @param array $params optional associative array in the format of
* ['client_secret'=>, 'client_id'=>, 'username'=>, 'password'=>, 'endpoint'=>, 'grant_type'=>]
*
*/
public function __construct($params = [])
public function __construct()
{
$this->paramsoverwrite = $params;

$cache = new FilesystemCache('OP');

Expand Down Expand Up @@ -75,61 +63,40 @@ public function getCacheKey()
*/
protected function CreateTokenHTTPQuery()
{
// you can set the paramaters manually, or via yml config
$params = $this->paramsoverwrite;

// fetch the paramaters from the yml config, or from the paramaters override
$resource = $this->getResourceURL();
$client_secret = isset($params['client_secret']) ? $params['client_secret'] : $this->config()->client_secret;
$client_id = isset($params['client_id']) ? $params['client_id'] : $this->config()->client_id;
$username = isset($params['username']) ? $params['username'] : $this->config()->username;
$password = isset($params['password']) ? $params['password'] : $this->config()->password;

$grant_type = isset($params['grant_type']) ? $params['grant_type'] : "password";

if ($grant_type === "client_credentials") {
return [
"grant_type" => $grant_type,
"resource" => $resource,
"client_secret" => $client_secret,
"client_id" => $client_id
];
}

// build the post paramaters
return [
"grant_type" => $grant_type,
"resource" => $resource,
"client_secret" => $client_secret,
"client_id" => $client_id,
"username" => $username,
"password" => $password
"grant_type" => 'client_credentials',
"resource" => Environment::getEnv('AZUREAPPLICATIONRESOURCELOCATION'),
"client_secret" => Environment::getEnv('AZUREAPPLICATIONSECRET'),
"client_id" => Environment::getEnv('AZUREAPPLICATIONCLIENT')
];
}

/**
* returns the resource of where the data is coming from
*
* @return string
*/
public function getResourceURL() {
return Environment::getEnv('AZUREAPPLICATIONRESOURCELOCATION');
}

/**
* Creates the authentication token used for communication with CRM
*
* @return string a uniqued hashed key used for authentication
*/
private function RequestAccessToken()
{
$params = $this->paramsoverwrite;

// endpoint url
$endpoint = isset($params['endpoint']) ? $params['endpoint'] : $this->config()->endpoint;
$endpoint = Environment::getEnv('AZUREAPPLICATIONENDPOINT');
$session = curl_init($endpoint);

curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, $this->HeadersDeAssociative($this->config()->headers));
curl_setopt($session, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($session, CURLOPT_POST, true);
//curl_setopt($session, CURLOPT_IPRESOLVE, true);
// curl_setopt($session, CURL_IPRESOLVE_V4, true);
curl_setopt($session, CURLOPT_ENCODING, true);



// build the post paramaters
curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query($this->CreateTokenHTTPQuery()));

Expand Down Expand Up @@ -159,23 +126,6 @@ private function RequestAccessToken()
return $content->access_token;
}

/**
* the resouce you want access too
*
* @param string the url of the resource
*/
public function getResourceURL()
{
if (Director::isTest()) {
return $this->config()->locationTest;
} else if (Director::isDev()) {
return $this->config()->locationDev;
} else if (Director::isLive()) {
return $this->config()->locationLive;
}
return '';
}

/**
*
*/
Expand Down

0 comments on commit 39f4dac

Please sign in to comment.