Overview

Namespaces

  • Comprobo
    • Verify
      • Auth
      • Exceptions
      • HTTP

Classes

  • Comprobo\Verify\Api
  • Comprobo\Verify\Auth\Server
  • Comprobo\Verify\Factory
  • Comprobo\Verify\HTTP\Request
  • Comprobo\Verify\State
  • Comprobo\Verify\User
  • Comprobo\Verify\Workflow

Exceptions

  • Comprobo\Verify\Exceptions\Auth
  • Comprobo\Verify\Exceptions\Misconfiguration
  • Comprobo\Verify\Exceptions\Upload
  • Comprobo\Verify\Exceptions\User
  • Overview
  • Namespace
  • Class
  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;

/**
 * User service integration
 */
class User
{
    private $config;
    private $factory;
    private $state;

    public function __construct(Factory $factory)
    {
        $this->factory = $factory;
        $this->config  = $factory->getConfig();
        $this->state   = $factory->getState();
    }

    /**
     * Create a new user in Comprobo. This user will not be given a direct login of their own,
     * but must be impersonated to be used.
     *
     * @see  Comprobo\Verify\Api::impersonate method to impersonate a user when we have their unique ID.
     *
     * @throws Exceptions\Auth if the state is invalid
     *
     * @param  string $userId    the local ID you would like to be associated with this user.
     * @param  string $firstName assigned to the user account and may be displayed in reports or other feedback.
     * @param  string $lastName  assigned to the user account and may be displayed in reports or other feedback.
     * @param  string $email     assigned to the user account and may be displayed in reports or other feedback.
     * @return array             the service response. Responsibilty falls on the caller to validate
     *                           and extract relevant information
     */
    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,
        ];

        // email address is now optional
        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;
    }

    /**
     * Store an uploaded file against the given user to be used in monitoring as a reference photograph
     *
     * @param string $userId Your user id, as specified in the create call
     * @param array  $file   matching the $_FILES['my-file'] format, an array representing a file.
     * @throws Exceptions\Upload if the file is empty or an upload error has occurred.
     */
    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)) {
            // no file uploaded top lol
            throw new Exceptions\Upload('No reference photo supplied');
        }

        if (isset($file['error']) && $file['error'] === \UPLOAD_ERR_OK) {
            return true;
        }

        $message = '';

        // something wrong with the uploaded file
        switch($file['error']) {
            case \UPLOAD_ERR_INI_SIZE:
                // The uploaded file exceeds the upload_max_filesize directive in php.ini.
                $max = ini_get('upload_max_filesize');
                $sent =  $file['size'];
                $message = 'Uploaded file is too large, maximum size is ' . $max;
                break;
            case \UPLOAD_ERR_PARTIAL:
                // 3; The uploaded file was only partially uploaded.
                $message = 'Incomplete file upload, please try again';
                break;
            case \UPLOAD_ERR_NO_FILE:
                // 4; No file was uploaded.
                $message = 'No file was uploaded';
                break;
            case \UPLOAD_ERR_NO_TMP_DIR:
                // temp folder missing, check upload_tmp_dir!
                // fall through
            case \UPLOAD_ERR_CANT_WRITE:
                // couldn't write the file to disk, is your disk full?
                // fall through
            case \UPLOAD_ERR_EXTENSION:
                // some extension broke, but we don't know which one
                $message = 'Could not save uploaded file';
                break;

            default:
                $message = "An unknown upload error occurred";
        }

        throw new Exceptions\Upload($message);
    }
}
API documentation generated by ApiGen