1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164:
<?php
namespace Comprobo\Verify;
use GuzzleHttp\Exception as GuzzleExceptions;
class User
{
private $config;
private $factory;
private $state;
public function __construct(Factory $factory)
{
$this->factory = $factory;
$this->config = $factory->getConfig();
$this->state = $factory->getState();
}
public function create($userId, $firstName, $lastName, $email=null)
{
$this->validateState();
$orgId = $this->state->get('organisationId');
$url = str_replace('{orgid}', $orgId, $this->config['urls']['userCreate']);
$details = [
'userId' => $userId,
'givenNames' => $firstName,
'familyName' => $lastName,
];
if ($email) {
$details['email'] = $email;
}
$request = $this->factory->getRequest();
$request->authorize($this->state->get('token'));
$response = $request->post($url, ['json' => $details]);
$body = json_decode((string) $response->getBody(), true);
return $body;
}
public function setReferencePhoto($userId, array $file)
{
$this->validateState();
$this->validateUpload($file);
$url = str_replace('{userId}', $userId, $this->config['urls']['setRefPhoto']);
$request = $this->factory->getRequest();
$request->authorize($this->state->get('token'));
$body = [
'multipart' => [
[
'name' => 'fileContents',
'contents' => fopen($file['tmp_name'], 'r'),
'filename' => $file['name']
],
[
'name' => 'fileData',
'contents' => json_encode([
'type' => $file['type'],
'size' => $file['size']
])
]
]
];
try {
$response = $request->post($url, $body);
} catch(GuzzleExceptions $e) {
$response = $e->getResponse();
}
$body = json_decode((string) $response->getBody(), true);
return $body;
}
private function validateState()
{
if (!$this->state->has('token')) {
throw new Exceptions\Auth('Could not validate user request');
}
if (!$this->state->has('organisationId')) {
throw new Exceptions\Auth('Could not complete user request');
}
}
private function validateUpload(array $file)
{
if (!isset($file) || empty($file)) {
throw new Exceptions\Upload('No reference photo supplied');
}
if (isset($file['error']) && $file['error'] === \UPLOAD_ERR_OK) {
return true;
}
$message = '';
switch($file['error']) {
case \UPLOAD_ERR_INI_SIZE:
$max = ini_get('upload_max_filesize');
$sent = $file['size'];
$message = 'Uploaded file is too large, maximum size is ' . $max;
break;
case \UPLOAD_ERR_PARTIAL:
$message = 'Incomplete file upload, please try again';
break;
case \UPLOAD_ERR_NO_FILE:
$message = 'No file was uploaded';
break;
case \UPLOAD_ERR_NO_TMP_DIR:
case \UPLOAD_ERR_CANT_WRITE:
case \UPLOAD_ERR_EXTENSION:
$message = 'Could not save uploaded file';
break;
default:
$message = "An unknown upload error occurred";
}
throw new Exceptions\Upload($message);
}
}