Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
18 / 18 |
OAuth2FlowAssess | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
18 / 18 |
__construct($client = null) | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
getClient() | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
setClient($client) | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
sendAuthenticationRequest($clientId, $clientSecret) | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
sendAuthorizedRequest(ResponseInterface $response) | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
<?php | |
namespace TriNetAssess; | |
use GuzzleHttp\ClientInterface; | |
use Psr\Http\Message\ResponseInterface; | |
class OAuth2FlowAssess | |
{ | |
protected $client; | |
//Start Here | |
/** | |
* OAuth2FlowAssess constructor. | |
* @param mixed $client | |
* @throws \TriNetAssess\AssessmentException | |
*/ | |
public function __construct($client = null) | |
{ | |
if (null !== $client) { | |
$this->setClient($client); | |
} | |
} | |
/** | |
* @return mixed | |
* @throws \TriNetAssess\AssessmentException | |
*/ | |
public function getClient() | |
{ | |
if(!($this->client instanceof ClientInterface)) { | |
throw new AssessmentException(); | |
} | |
return $this->client; | |
} | |
/** | |
* @param mixed $client | |
* @return OAuth2FlowAssess | |
* @throws \TriNetAssess\AssessmentException | |
*/ | |
public function setClient($client) | |
{ | |
if(!($client instanceof ClientInterface)) { | |
throw new AssessmentException(); | |
} | |
$this->client = $client; | |
return $this->client; | |
} | |
/** | |
* @param string $clientId | |
* @param string $clientSecret | |
* @return mixed | |
* @throws \TriNetAssess\AssessmentException | |
*/ | |
public function sendAuthenticationRequest($clientId, $clientSecret) | |
{ | |
return $this->getClient()->request('POST', 'http://example.org/oauth/token', [ | |
'form_params' => [ | |
'grant_type' => 'client_credentials', | |
'client_id' => $clientId, | |
'client_secret' => $clientSecret | |
] | |
]); | |
} | |
/** | |
* @param ResponseInterface $response | |
* @return mixed | |
* @throws \TriNetAssess\AssessmentException | |
*/ | |
public function sendAuthorizedRequest(ResponseInterface $response) | |
{ | |
$body = \GuzzleHttp\json_decode($response->getBody()); | |
$authorization = $body->token_type . ' ' . $body->access_token; | |
return $this->getClient()->request('GET', 'http://example.org/api/some_resource', [ | |
'headers' => [ | |
'Authorization' => $authorization | |
] | |
]); | |
} | |
/** | |
* TODO: remove before sending | |
* Recommendations: | |
* 1. Follow PSR standards: http://www.php-fig.org/psr/ | |
* 2. Support method chaining via fluent setters -> change setClient method to: return $this; | |
* 3. Follow DRY -> in sendAuthorizedRequest configuring the Authorization header should be a separate function | |
* called getAuthorization(ResponseInterface $response) that returns the Authorization string (2 additional UT) | |
* 4. Follow SOLID (Liskov substitution principle) -> type hinting of ClientInterface should be used instead of | |
* instanceof validation | |
*/ |