Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
"humanity"
],
"require": {
"php": ">=5.4.0",
"league/oauth2-client": "~0.11.0",
"guzzle/guzzle": "~3.7"
"php": "^7.2.5 || ^8.0",
"league/oauth2-client": "^2.6",
"guzzlehttp/guzzle": "^7.2",
"ext-json": "*"
}
,
"require-dev": {
"roave/security-advisories": "dev-latest"
},
"autoload": {
"psr-0": {
Expand Down
145 changes: 42 additions & 103 deletions src/Humanity/Humanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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()]);
}

/**
Expand Down Expand Up @@ -159,7 +157,6 @@ public function clearAccessToken() {
*
* @return AccessToken
* @throws \Exception
* @throws IDPException
*/
public function obtainAccessToken() {
$tokenStorage = $this->getStorage();
Expand Down Expand Up @@ -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];
Expand All @@ -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;
}

/**
Expand All @@ -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,
]);
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand Down
31 changes: 30 additions & 1 deletion src/Humanity/OAuth2/Client/Provider/Humanity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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.
}
}