-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from DarkSide666/master
Creating test user service; Add fraud prevention headers
- Loading branch information
Showing
12 changed files
with
294 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace HMRC\TestUser; | ||
|
||
use HMRC\Request\RequestHeader; | ||
use HMRC\Request\RequestHeaderValue; | ||
use HMRC\Request\RequestMethod; | ||
use HMRC\Request\RequestWithServerToken; | ||
|
||
abstract class AbstractRequest extends RequestWithServerToken | ||
{ | ||
/** @var PostBody */ | ||
protected $postBody; | ||
|
||
public function __construct(PostBody $postBody) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->postBody = $postBody; | ||
} | ||
|
||
protected function getMethod(): string | ||
{ | ||
return RequestMethod::POST; | ||
} | ||
|
||
protected function getHeaders(): array | ||
{ | ||
return array_merge(parent::getHeaders(), [ | ||
RequestHeader::CONTENT_TYPE => RequestHeaderValue::APPLICATION_JSON, | ||
]); | ||
} | ||
|
||
/** | ||
* @throws \GuzzleHttp\Exception\GuzzleException | ||
* @throws \HMRC\Exceptions\InvalidPostBodyException | ||
* @throws \HMRC\Exceptions\MissingAccessTokenException | ||
* | ||
* @return mixed|Response | ||
*/ | ||
public function fire() | ||
{ | ||
$this->postBody->validate(); | ||
|
||
return parent::fire(); | ||
} | ||
|
||
protected function getHTTPClientOptions(): array | ||
{ | ||
return array_merge([ | ||
'json' => $this->postBody->toArray(), | ||
], parent::getHTTPClientOptions()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace HMRC\TestUser; | ||
|
||
class CreateAgentRequest extends AbstractRequest | ||
{ | ||
protected function getApiPath(): string | ||
{ | ||
return '/create-test-user/agents'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace HMRC\TestUser; | ||
|
||
class CreateIndividualRequest extends AbstractRequest | ||
{ | ||
protected function getApiPath(): string | ||
{ | ||
return '/create-test-user/individuals'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace HMRC\TestUser; | ||
|
||
class CreateOrganisationRequest extends AbstractRequest | ||
{ | ||
protected function getApiPath(): string | ||
{ | ||
return '/create-test-user/organisations'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace HMRC\TestUser; | ||
|
||
use HMRC\Exceptions\InvalidPostBodyException; | ||
|
||
class PostBody implements \HMRC\Request\PostBody | ||
{ | ||
/** @var array */ | ||
private $serviceNames; | ||
|
||
/** | ||
* Validate the post body, it should throw an Exception if something is wrong. | ||
* | ||
* @throws InvalidPostBodyException | ||
*/ | ||
public function validate() | ||
{ | ||
$requiredFields = [ | ||
'serviceNames', | ||
]; | ||
|
||
$emptyFields = []; | ||
foreach ($requiredFields as $requiredField) { | ||
if (is_null($this->{$requiredField})) { | ||
$emptyFields[] = $requiredField; | ||
} | ||
} | ||
|
||
if (count($emptyFields) > 0) { | ||
$emptyFieldsString = implode(', ', $emptyFields); | ||
|
||
throw new InvalidPostBodyException("Missing post body fields ({$emptyFieldsString})."); | ||
} | ||
} | ||
|
||
/** | ||
* Return post body as an array to be used to call. | ||
* | ||
* @return array | ||
*/ | ||
public function toArray(): array | ||
{ | ||
return [ | ||
'serviceNames' => $this->serviceNames, | ||
]; | ||
} | ||
|
||
/** | ||
* @param array $serviceNames | ||
* | ||
* @return $this | ||
*/ | ||
public function setServiceNames(array $serviceNames): self | ||
{ | ||
$this->serviceNames = $serviceNames; | ||
|
||
return $this; | ||
} | ||
} |