<?php
use Luracast\Restler\RestException;

require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';

class ApiUserHandler
{
    public $db;

    public function __construct()
    {
        global $db;
        
        $this->db = $db;
    }

    /**
     * Summary of getUser
     * @param int $id
     * @param string $apikey
     * 
     * @throws Exception
     * 
     * @return User
     */
    public function getUser(int $id, string $apikey): User
    {
        $user = new User($this->db);

		if (($user->fetch($id) < 1) 
            || ($user->api_key !== $apikey)
            || (intval($user->statut) !== User::STATUS_ENABLED)) {

			throw new RestException(404, 'User not found');
		}

        return $user;
    }


}