From db847fa073ec0245769c9b3734aeacd6e4ec57d6 Mon Sep 17 00:00:00 2001 From: Jozef Pistej Date: Wed, 29 Sep 2021 16:21:15 +0200 Subject: [PATCH 1/3] updated libs --- composer.json | 13 +- src/Humanity/Humanity.php | 145 +++++------------- .../OAuth2/Client/Provider/Humanity.php | 31 +++- 3 files changed, 81 insertions(+), 108 deletions(-) diff --git a/composer.json b/composer.json index 4edbb57..13ee670 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "shiftplanning/humanity-php-sdk", + "name": "pistej/humanity-php-sdk", "description": "Humanity PHP SDK", "license": "MIT", "authors": [ @@ -18,9 +18,14 @@ "humanity" ], "require": { - "php": ">=5.4.0", - "league/oauth2-client": "~0.11.0", - "guzzle/guzzle": "~3.7" + "php": ">=7.2", + "league/oauth2-client": "~2.6.0", + "guzzlehttp/guzzle": "~7.3.0", + "ext-json": "*" + } + , + "require-dev": { + "roave/security-advisories": "dev-latest" }, "autoload": { "psr-0": { diff --git a/src/Humanity/Humanity.php b/src/Humanity/Humanity.php index 2dd5411..974cd69 100644 --- a/src/Humanity/Humanity.php +++ b/src/Humanity/Humanity.php @@ -2,9 +2,8 @@ namespace Humanity; -use Guzzle\Http\Client; -use Guzzle\Http\Exception\BadResponseException; -use Guzzle\Http\Message\RequestInterface; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; use Humanity\Entity\Employee as EmployeeEntity; use Humanity\OAuth2\Client\Provider\Humanity as Provider; use Humanity\Repository\Account as AccountRepository; @@ -16,7 +15,6 @@ use Humanity\Repository\Timeclock as TimeclockRepository; use Humanity\Storage\Adapter\AdapterInterface; use Humanity\Storage\Adapter\Session as Storage; -use League\OAuth2\Client\Exception\IDPException; use League\OAuth2\Client\Token\AccessToken; /** @@ -77,7 +75,7 @@ public function __construct(array $options = []) { $this->setApiBaseUri($options['apiBaseUri']); } - $this->client = new Client($this->getApiBaseUri()); + $this->client = new Client(['base_uri' => $this->getApiBaseUri()]); } /** @@ -159,7 +157,6 @@ public function clearAccessToken() { * * @return AccessToken * @throws \Exception - * @throws IDPException */ public function obtainAccessToken() { $tokenStorage = $this->getStorage(); @@ -207,15 +204,14 @@ public function obtainAccessToken() { return $accessToken; } - /** - * @param string $method - * @param string $path - * @param mixed $binds - * @param array $options - * - * @return RequestInterface - */ - protected function prepare($method, $path, $binds, array $options = []) { + /** + * @param string $method + * @param string $path + * @param mixed $binds + * @param array $options + */ + protected function makeRequest($method, $path, $binds, array $options = []): Response + { if (null !== $binds) { if (is_scalar($binds)) { $binds = ['id' => $binds]; @@ -235,11 +231,34 @@ protected function prepare($method, $path, $binds, array $options = []) { } $options['query'] = array_merge($options['query'], [ - 'access_token' => $this->getAccessToken()->accessToken, + 'access_token' => $this->getAccessToken()->getToken(), 'suppress_response_codes' => 1, ]); - return $this->client->createRequest($method, $path, null, null, $options); + try { + $httpResponse = $this->client->request($method, $path, $options); + } catch (GuzzleException $exc) { + $response = new Response(); + $response->setError([ + 'code' => $exc->getCode(), + 'message' => $exc->getMessage(), + ]); + + return $response; + } + + $json = json_decode($httpResponse->getBody()->getContents(), true); + + $response = new Response(); + $response->setStatus($json['status']); + + if (isset($json['data']['items'])) { + $response->setData($json['data']['items']); + } else { + $response->setData($json['data']); + } + + return $response; } /** @@ -252,29 +271,9 @@ protected function prepare($method, $path, $binds, array $options = []) { * @return Response */ public function get($path, $binds = null, array $query = []) { - $httpRequest = $this->prepare('GET', $path, $binds, [ - 'query' => $query, - ]); - - try { - $httpResponse = $httpRequest->send(); - } catch (BadResponseException $exc) { - $httpResponse = $exc->getResponse(); - } - - $json = $httpResponse->json(); - - $response = new Response(); - $response->setStatus($json['status']); - $response->setError($json['error']); - - if (isset($json['data']['items'])) { - $response->setData($json['data']['items']); - } else { - $response->setData($json['data']); - } - - return $response; + return $this->makeRequest('GET', $path, $binds, [ + 'query' => $query, + ]); } /** @@ -287,29 +286,9 @@ public function get($path, $binds = null, array $query = []) { * @return Response */ public function post($path, $binds = null, array $data = []) { - $httpRequest = $this->prepare('POST', $path, $binds, [ + return $this->makeRequest('POST', $path, $binds, [ 'body' => $data ]); - - try { - $httpResponse = $httpRequest->send(); - } catch (BadResponseException $exc) { - $httpResponse = $exc->getResponse(); - } - - $json = $httpResponse->json(); - - $response = new Response(); - $response->setStatus($json['status']); - $response->setError($json['error']); - - if (isset($json['data']['items'])) { - $response->setData($json['data']['items']); - } else { - $response->setData($json['data']); - } - - return $response; } /** @@ -322,29 +301,9 @@ public function post($path, $binds = null, array $data = []) { * @return Response */ public function put($path, $binds = null, array $data = []) { - $httpRequest = $this->prepare('PUT', $path, $binds, [ + return $this->makeRequest('PUT', $path, $binds, [ 'body' => $data ]); - - try { - $httpResponse = $httpRequest->send(); - } catch (BadResponseException $exc) { - $httpResponse = $exc->getResponse(); - } - - $json = $httpResponse->json(); - - $response = new Response(); - $response->setStatus($json['status']); - $response->setError($json['error']); - - if (isset($json['data']['items'])) { - $response->setData($json['data']['items']); - } else { - $response->setData($json['data']); - } - - return $response; } /** @@ -356,27 +315,7 @@ public function put($path, $binds = null, array $data = []) { * @return Response */ public function delete($path, $binds = null) { - $httpRequest = $this->prepare('DELETE', $path, $binds); - - try { - $httpResponse = $httpRequest->send(); - } catch (BadResponseException $exc) { - $httpResponse = $exc->getResponse(); - } - - $json = $httpResponse->json(); - - $response = new Response(); - $response->setStatus($json['status']); - $response->setError($json['error']); - - if (isset($json['data']['items'])) { - $response->setData($json['data']['items']); - } else { - $response->setData($json['data']); - } - - return $response; + return $this->makeRequest('DELETE', $path, $binds); } /** diff --git a/src/Humanity/OAuth2/Client/Provider/Humanity.php b/src/Humanity/OAuth2/Client/Provider/Humanity.php index f403fdc..caf55bf 100644 --- a/src/Humanity/OAuth2/Client/Provider/Humanity.php +++ b/src/Humanity/OAuth2/Client/Provider/Humanity.php @@ -2,9 +2,9 @@ namespace Humanity\OAuth2\Client\Provider; -use \League\OAuth2\Client\Entity\User; use \League\OAuth2\Client\Provider\AbstractProvider; use \League\OAuth2\Client\Token\AccessToken; +use Psr\Http\Message\ResponseInterface; class Humanity extends AbstractProvider { @@ -73,4 +73,33 @@ public function userDetails($response, AccessToken $token) { throw new \Exception(sprintf('Method % not implemented', __METHOD__)); } + public function getBaseAuthorizationUrl() + { + // TODO: Implement getBaseAuthorizationUrl() method. + } + + public function getBaseAccessTokenUrl(array $params) + { + // TODO: Implement getBaseAccessTokenUrl() method. + } + + public function getResourceOwnerDetailsUrl(AccessToken $token) + { + // TODO: Implement getResourceOwnerDetailsUrl() method. + } + + protected function getDefaultScopes() + { + // TODO: Implement getDefaultScopes() method. + } + + protected function checkResponse(ResponseInterface $response, $data) + { + // TODO: Implement checkResponse() method. + } + + protected function createResourceOwner(array $response, AccessToken $token) + { + // TODO: Implement createResourceOwner() method. + } } From cf91e7918fb75f5ac99e768c7fd22902630de872 Mon Sep 17 00:00:00 2001 From: Jozef Pistej Date: Wed, 29 Sep 2021 18:50:17 +0200 Subject: [PATCH 2/3] relax composer.json and revert name --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 13ee670..36b5ae9 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "pistej/humanity-php-sdk", + "name": "shiftplanning/humanity-php-sdk", "description": "Humanity PHP SDK", "license": "MIT", "authors": [ @@ -18,9 +18,9 @@ "humanity" ], "require": { - "php": ">=7.2", - "league/oauth2-client": "~2.6.0", - "guzzlehttp/guzzle": "~7.3.0", + "php": "^7.0 || ^8.0", + "league/oauth2-client": "^2.6", + "guzzlehttp/guzzle": "^7.3", "ext-json": "*" } , From 61ae40dabbb33cee1501a9e98b80798dec4144d0 Mon Sep 17 00:00:00 2001 From: Jozef Pistej Date: Wed, 29 Sep 2021 18:59:41 +0200 Subject: [PATCH 3/3] relax composer.json and revert name --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 36b5ae9..9acba6c 100644 --- a/composer.json +++ b/composer.json @@ -18,9 +18,9 @@ "humanity" ], "require": { - "php": "^7.0 || ^8.0", + "php": "^7.2.5 || ^8.0", "league/oauth2-client": "^2.6", - "guzzlehttp/guzzle": "^7.3", + "guzzlehttp/guzzle": "^7.2", "ext-json": "*" } ,