From 460407b3bcd94e6ba764c35d9c9bfdf8d944d09c Mon Sep 17 00:00:00 2001 From: Alberto Barba Date: Tue, 19 Jun 2018 23:52:45 +0200 Subject: [PATCH] refs #68: Add User model and UserApi --- .../CloseIoApiWrapper/Api/UserApi.php | 120 +++++++++++ .../CloseIoApiWrapper/CloseIoApiWrapper.php | 15 +- .../CloseIoApiWrapper/Model/User.php | 196 ++++++++++++++++++ .../CloseIoApiWrapperTest.php | 91 ++++++++ 4 files changed, 421 insertions(+), 1 deletion(-) create mode 100644 src/LooplineSystems/CloseIoApiWrapper/Api/UserApi.php create mode 100644 src/LooplineSystems/CloseIoApiWrapper/Model/User.php diff --git a/src/LooplineSystems/CloseIoApiWrapper/Api/UserApi.php b/src/LooplineSystems/CloseIoApiWrapper/Api/UserApi.php new file mode 100644 index 0000000..effc344 --- /dev/null +++ b/src/LooplineSystems/CloseIoApiWrapper/Api/UserApi.php @@ -0,0 +1,120 @@ +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; + } + +} diff --git a/src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php b/src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php index 1a0a4b8..5b895a3 100644 --- a/src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php +++ b/src/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapper.php @@ -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; @@ -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; } @@ -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; + } } diff --git a/src/LooplineSystems/CloseIoApiWrapper/Model/User.php b/src/LooplineSystems/CloseIoApiWrapper/Model/User.php new file mode 100644 index 0000000..117f03c --- /dev/null +++ b/src/LooplineSystems/CloseIoApiWrapper/Model/User.php @@ -0,0 +1,196 @@ +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; + } +} diff --git a/tests/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapperTest.php b/tests/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapperTest.php index dfb44ba..9e2118d 100644 --- a/tests/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapperTest.php +++ b/tests/LooplineSystems/CloseIoApiWrapper/CloseIoApiWrapperTest.php @@ -9,6 +9,15 @@ namespace Tests\LooplineSystems\CloseIoApiWrapper; +use LooplineSystems\CloseIoApiWrapper\Api\ActivityApi; +use LooplineSystems\CloseIoApiWrapper\Api\ContactApi; +use LooplineSystems\CloseIoApiWrapper\Api\CustomFieldApi; +use LooplineSystems\CloseIoApiWrapper\Api\LeadApi; +use LooplineSystems\CloseIoApiWrapper\Api\LeadStatusApi; +use LooplineSystems\CloseIoApiWrapper\Api\OpportunityApi; +use LooplineSystems\CloseIoApiWrapper\Api\OpportunityStatusApi; +use LooplineSystems\CloseIoApiWrapper\Api\TaskApi; +use LooplineSystems\CloseIoApiWrapper\Api\UserApi; use LooplineSystems\CloseIoApiWrapper\CloseIoApiWrapper; use LooplineSystems\CloseIoApiWrapper\CloseIoConfig; @@ -22,4 +31,86 @@ public function testCreateCloseIoWrapper() $this->assertInstanceOf(CloseIoApiWrapper::class, $closeIoApiWrapper); } + + public function testItReturnsUserApi() + { + $config = new CloseIoConfig(); + $config->setApiKey('testkey'); + $closeIoApiWrapper = new CloseIoApiWrapper($config); + + $this->assertInstanceOf(UserApi::class, $closeIoApiWrapper->getUserApi()); + } + + public function testItReturnsLeadApi() + { + $config = new CloseIoConfig(); + $config->setApiKey('testkey'); + $closeIoApiWrapper = new CloseIoApiWrapper($config); + + $this->assertInstanceOf(LeadApi::class, $closeIoApiWrapper->getLeadApi()); + } + + public function testItReturnsCustomFieldApi() + { + $config = new CloseIoConfig(); + $config->setApiKey('testkey'); + $closeIoApiWrapper = new CloseIoApiWrapper($config); + + $this->assertInstanceOf(CustomFieldApi::class, $closeIoApiWrapper->getCustomFieldApi()); + } + + public function testItReturnsOpportunityApi() + { + $config = new CloseIoConfig(); + $config->setApiKey('testkey'); + $closeIoApiWrapper = new CloseIoApiWrapper($config); + + $this->assertInstanceOf(OpportunityApi::class, $closeIoApiWrapper->getOpportunityApi()); + } + + public function testItReturnsLeadStatusApi() + { + $config = new CloseIoConfig(); + $config->setApiKey('testkey'); + $closeIoApiWrapper = new CloseIoApiWrapper($config); + + $this->assertInstanceOf(LeadStatusApi::class, $closeIoApiWrapper->getLeadStatusesApi()); + } + + public function testItReturnsOpportunityStatusApi() + { + $config = new CloseIoConfig(); + $config->setApiKey('testkey'); + $closeIoApiWrapper = new CloseIoApiWrapper($config); + + $this->assertInstanceOf(OpportunityStatusApi::class, $closeIoApiWrapper->getOpportunityStatusesApi()); + } + + public function testItReturnsContactApi() + { + $config = new CloseIoConfig(); + $config->setApiKey('testkey'); + $closeIoApiWrapper = new CloseIoApiWrapper($config); + + $this->assertInstanceOf(ContactApi::class, $closeIoApiWrapper->getContactApi()); + } + + public function testItReturnsActivityApi() + { + $config = new CloseIoConfig(); + $config->setApiKey('testkey'); + $closeIoApiWrapper = new CloseIoApiWrapper($config); + + $this->assertInstanceOf(ActivityApi::class, $closeIoApiWrapper->getActivitiesApi()); + } + + public function testItReturnsTaskApi() + { + $config = new CloseIoConfig(); + $config->setApiKey('testkey'); + $closeIoApiWrapper = new CloseIoApiWrapper($config); + + $this->assertInstanceOf(TaskApi::class, $closeIoApiWrapper->getTaskApi()); + } + }