Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
refs #68: Add User model and UserApi
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbertoBarba authored and mavimo committed Jun 19, 2018
1 parent a07872d commit 460407b
Show file tree
Hide file tree
Showing 4 changed files with 421 additions and 1 deletion.
120 changes: 120 additions & 0 deletions src/LooplineSystems/CloseIoApiWrapper/Api/UserApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Close.io Api Wrapper - LLS Internet GmbH - Loopline Systems
*
* @link https://github.com/loopline-systems/closeio-api-wrapper for the canonical source repository
* @copyright Copyright (c) 2014 LLS Internet GmbH - Loopline Systems (http://www.loopline-systems.com)
* @license https://github.com/loopline-systems/closeio-api-wrapper/blob/master/LICENSE (MIT Licence)
*/

namespace LooplineSystems\CloseIoApiWrapper\Api;

use LooplineSystems\CloseIoApiWrapper\CloseIoResponse;
use LooplineSystems\CloseIoApiWrapper\Library\Api\AbstractApi;
use LooplineSystems\CloseIoApiWrapper\Library\Curl\Curl;
use LooplineSystems\CloseIoApiWrapper\Library\Exception\BadApiRequestException;
use LooplineSystems\CloseIoApiWrapper\Library\Exception\InvalidParamException;
use LooplineSystems\CloseIoApiWrapper\Library\Exception\ResourceNotFoundException;
use LooplineSystems\CloseIoApiWrapper\Library\Exception\UrlNotSetException;
use LooplineSystems\CloseIoApiWrapper\Model\User;

class UserApi extends AbstractApi
{
const NAME = 'UserApi';

/**
* {@inheritdoc}
*/
protected function initUrls()
{
$this->urls = [
'get-current-user' => '/me/',
'get-users' => '/user/',
'get-user' => '/user/[:id]',
];
}

/**
* @return User
*
* @throws BadApiRequestException
* @throws InvalidParamException
* @throws ResourceNotFoundException
* @throws UrlNotSetException
*/
public function getCurrentUser()
{
$apiRequest = $this->prepareRequest('get-current-user');

$result = $this->triggerGet($apiRequest);

if ($result->getReturnCode() == 200 && (!empty($result->getData()))) {
$user = new User($result->getData());
} else {
throw new ResourceNotFoundException();
}

return $user;
}

/**
* @return User[]
*
* @throws BadApiRequestException
* @throws InvalidParamException
* @throws UrlNotSetException
* @throws ResourceNotFoundException
*/
public function getAllUsers()
{
/** @var User[] $users */
$users = array();

$apiRequest = $this->prepareRequest('get-users');

$result = $this->triggerGet($apiRequest);

if ($result->getReturnCode() == 200) {
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];
foreach ($rawData as $user) {
$users[] = new User($user);
}
}

return $users;
}

/**
* @param string $id
* @return User
*
* @throws BadApiRequestException
* @throws InvalidParamException
* @throws ResourceNotFoundException
* @throws UrlNotSetException
*/
public function getUser($id)
{
$apiRequest = $this->prepareRequest('get-user', null, ['id' => $id]);

$result = $this->triggerGet($apiRequest);

if ($result->getReturnCode() == 200 && (!empty($result->getData()))) {
$user = new User($result->getData());
} else {
throw new ResourceNotFoundException();
}

return $user;
}

/**
* @param User $user
* @return bool
*/
public function validateUserForPost(User $user)
{
return true;
}

}
15 changes: 14 additions & 1 deletion src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use LooplineSystems\CloseIoApiWrapper\Api\OpportunityApi;
use LooplineSystems\CloseIoApiWrapper\Api\OpportunityStatusApi;
use LooplineSystems\CloseIoApiWrapper\Api\TaskApi;
use LooplineSystems\CloseIoApiWrapper\Library\Api\AbstractApi;
use LooplineSystems\CloseIoApiWrapper\Api\UserApi;
use LooplineSystems\CloseIoApiWrapper\Library\Api\ApiHandler;
use LooplineSystems\CloseIoApiWrapper\Library\Exception\ApiNotFoundException;
use LooplineSystems\CloseIoApiWrapper\Library\Exception\InvalidParamException;
Expand Down Expand Up @@ -57,6 +57,7 @@ protected function initApiHandler(CloseIoConfig $config)
$apiHandler->setApi(new ContactApi($apiHandler));
$apiHandler->setApi(new ActivityApi($apiHandler));
$apiHandler->setApi(new TaskApi($apiHandler));
$apiHandler->setApi(new UserApi($apiHandler));

return $apiHandler;
}
Expand Down Expand Up @@ -156,4 +157,16 @@ public function getApiHandler()
{
return $this->apiHandler;
}

/**
* @return UserApi
* @throws Library\Exception\ApiNotFoundException
*/
public function getUserApi()
{
/** @var UserApi $api */
$api = $this->apiHandler->getApi(UserApi::NAME);

return $api;
}
}
196 changes: 196 additions & 0 deletions src/LooplineSystems/CloseIoApiWrapper/Model/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
<?php
/**
* Close.io Api Wrapper - LLS Internet GmbH - Loopline Systems
*
* @link https://github.com/loopline-systems/closeio-api-wrapper for the canonical source repository
* @copyright Copyright (c) 2014 LLS Internet GmbH - Loopline Systems (http://www.loopline-systems.com)
* @license https://github.com/loopline-systems/closeio-api-wrapper/blob/master/LICENSE (MIT Licence)
*/

namespace LooplineSystems\CloseIoApiWrapper\Model;

use LooplineSystems\CloseIoApiWrapper\Library\ObjectHydrateHelperTrait;
use LooplineSystems\CloseIoApiWrapper\Library\JsonSerializableHelperTrait;

class User implements \JsonSerializable
{
use ObjectHydrateHelperTrait;
use JsonSerializableHelperTrait;

/**
* @var string
*/
private $id;

/**
* @var string
*/
private $email;

/**
* @var string
*/
private $first_name;

/**
* @var string
*/
private $last_name;

/**
* @var string
*/
private $image;

/**
* @var \DateTime
*/
private $date_created;

/**
* @var \DateTime
*/
private $date_updated;
/**
* @var array
*/
private $organizations;

/**
* @param array $data
*/
public function __construct(array $data = null)
{
if ($data) {
$this->hydrate($data);
}
}

/**
* @return string
*/
public function getId()
{
return $this->id;
}

/**
* @param string $id
*/
public function setId($id)
{
$this->id = $id;
}

/**
* @return string
*/
public function getEmail()
{
return $this->email;
}

/**
* @param string $email
*/
public function setEmail($email)
{
$this->email = $email;
}

/**
* @return string
*/
public function getFirstName()
{
return $this->first_name;
}

/**
* @param string $first_name
*/
public function setFirstName($first_name)
{
$this->first_name = $first_name;
}

/**
* @return string
*/
public function getLastName()
{
return $this->last_name;
}

/**
* @param string $last_name
*/
public function setLastName($last_name)
{
$this->last_name = $last_name;
}

/**
* @return string
*/
public function getImage()
{
return $this->image;
}

/**
* @param string $image
*/
public function setImage($image)
{
$this->image = $image;
}

/**
* @return \DateTime
*/
public function getDateCreated()
{
return $this->date_created;
}

/**
* @param \DateTime $date_created
*/
public function setDateCreated($date_created)
{
$this->date_created = $date_created;
}

/**
* @return \DateTime
*/
public function getDateUpdated()
{
return $this->date_updated;
}

/**
* @param \DateTime $date_updated
*/
public function setDateUpdated($date_updated)
{
$this->date_updated = $date_updated;
}

/**
* @return array
*/
public function getOrganizations()
{
return $this->organizations;
}

/**
* @param array $organizations
*/
public function setOrganizations($organizations)
{
$this->organizations = $organizations;
}
}
Loading

0 comments on commit 460407b

Please sign in to comment.