-
-
Save JayPHP/25214ed9bd54601e51f3d93223d8975a to your computer and use it in GitHub Desktop.
Class to communicate with an API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller\Admin; | |
use Cake\ORM\TableRegistry; | |
use Cake\Core\Configure; | |
use App\Exceptions\EBSCommunicationFailureException; | |
use App\Exceptions\UnauthorisedHostException; | |
use App\Controller\Admin\AdminController; | |
use App\Utility\CurlAdapter; | |
use App\Utility\Storage\EBSMatches; | |
class EBSController extends AdminController { | |
private $authToken; | |
private $application; | |
public function initialize() | |
{ | |
parent::initialize(); | |
$this->application = $this->getApplicationEntity(); | |
$this->authToken = $this->requestAuthToken(); | |
} | |
public function getEBSMatches() | |
{ | |
$this->authoriseAJAXRequest(); | |
$matches = new EBSMatches(); | |
$matches->setMatched( | |
$this->getFuzzyMatches([ | |
'studentID' => ['value' => $this->application->studentID, 'type' => 'int'], | |
'firstname' => ['value' => $this->application->firstname, 'type' => 'string'], | |
'lastname' => ['value' => $this->application->lastname, 'type' => 'string'], | |
'middlenames' => ['value' => $this->application->middlenames, 'type' => 'string'], | |
'dob' => ['value' => $this->application->dob, 'type' => 'string'], | |
'postcode' => ['value' => $this->application->postcode, 'type' => 'string'] | |
]) | |
); | |
$matches->setUsers( | |
$this->getUsersFromMatched($matches->getMatched()) | |
); | |
$this->set('users', $matches->getJsonData()); | |
$this->set('_serialize', ['users']); | |
} | |
private function authoriseAJAXRequest() | |
{ | |
$protocol = $this->request->is('ssl') ? 'https://' : 'http://'; | |
$requestURL = $protocol.$this->request->env('http_host'); | |
if (!in_array($requestURL, Configure::read('App.ajaxHosts'))) { | |
throw new UnauthorisedHostException("Host $requestURL is not authorised"); | |
} | |
return true; | |
} | |
private function getApplicationEntity() | |
{ | |
$applications = $this->loadModel('Applications'); | |
if (isset($this->request->params['pass'])) { | |
return $applications->get($this->request->params['pass'][0]); | |
} | |
throw new \Exception('Missing ID parameter'); | |
} | |
private function requestAuthToken() | |
{ | |
$response = $this->EBSServerRequest( | |
Configure::read('ebs.api.auth'), | |
null, | |
Configure::read('ebs.username').':'.Configure::read('ebs.password') | |
); | |
return $response->Token; | |
} | |
private function getFuzzyMatches($data) | |
{ | |
foreach ($data as $key => $value) { | |
$data[$key] = $this->formatDataForEBS($value['value'], $value['type']); | |
} | |
$response = $this->EBSServerRequest( | |
sprintf(Configure::read('ebs.api.fuzzy'), | |
$data['studentid'], | |
$data['firstname'], | |
$data['lastname'], | |
$data['middlenames'], | |
$data['dob'], | |
$data['postcode'] | |
), | |
"Authorization: $this->authToken", | |
null | |
); | |
return $response->FUZZY_MATCH; | |
} | |
private function getUsersFromMatched($matches) | |
{ | |
$users = array(); | |
foreach ($matches as $match) { | |
$user = $this->EBSServerRequest( | |
sprintf( | |
Configure::read('ebs.api.student'), | |
$this->formatDataForEBS($match->PERSON_CODE, 'int') | |
), | |
"Authorization: $this->authToken" | |
); | |
if (empty($user)) { | |
continue; | |
} | |
$person = $user->People[0]; | |
$person->formattedDOB = $this->formatDOB($person->DATE_OF_BIRTH); | |
$person->genderID = $this->getGenderID($person->GENDER); | |
$person->nationalityID = $this->getNationalityID($person->NATIONALITY); | |
$users[] = $person; | |
} | |
return $users; | |
} | |
private function getGenderID($gender) | |
{ | |
$genders = $this->loadModel('Genders'); | |
$gender = $genders->find('all') | |
->where([ | |
'Genders.name' => $gender, | |
'Genders.acadyear' => $this->application->acadyear | |
]) | |
->first(); | |
if (isset($gender)) { | |
return $gender->code; | |
} | |
return null; | |
} | |
private function getNationalityID($nationality) | |
{ | |
$nationalities = $this->loadModel('Nationalities'); | |
$nationality = $nationalities->find('all') | |
->where([ | |
'Nationalities.name' => $nationality, | |
'Nationalities.acadyear' => $this->application->acadyear | |
]) | |
->first(); | |
if (isset($nationality)) { | |
return $nationality->code; | |
} | |
return null; | |
} | |
private function formatDOB($date = null) | |
{ | |
if (is_null($date)) { | |
return false; | |
} | |
$dateParts = explode('/', $date); | |
if (count($dateParts) !== 3) { | |
return false; | |
} | |
$date = array(); | |
$date['day'] = $dateParts[0]; | |
$date['month'] = $dateParts[1]; | |
$date['year'] = $dateParts[2]; | |
return $date; | |
} | |
private function formatDataForEBS($data, $type) | |
{ | |
if (empty($data)) { | |
if ($type == 'int') { | |
return 0; | |
} | |
return '"1"'; | |
} | |
return urlencode($data); | |
} | |
private function EBSServerRequest($url, $header = null, $password = null) | |
{ | |
$headers = ['Accept: application/json']; | |
if (isset($header)) { | |
$headers[] = $header; | |
} | |
$url = Configure::read('ebs.url').$url; | |
$curl = new CurlAdapter(); | |
$response = $curl->request($url, $headers, $password); | |
if (!$response = json_decode($response)) { | |
throw new EBSCommunicationFailureException("No response from $url"); | |
} | |
return $response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment