Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UML-2907 phpcbf run in service-api #2427

Merged
merged 7 commits into from
Nov 16, 2023
Merged
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
7 changes: 5 additions & 2 deletions service-api/app/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

declare(strict_types=1);

use Mezzio\Application;
use Mezzio\MiddlewareFactory;

// Delegate static file requests back to the PHP built-in webserver
if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
return false;
Expand All @@ -18,8 +21,8 @@
$container = require 'config/container.php';

/** @var \Mezzio\Application $app */
$app = $container->get(\Mezzio\Application::class);
$factory = $container->get(\Mezzio\MiddlewareFactory::class);
$app = $container->get(Application::class);
$factory = $container->get(MiddlewareFactory::class);

// Execute programmatic/declarative middleware pipeline and routing
// configuration statements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,10 @@
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\ResponseInterface;

/**
* Class ActorCodes
*
* @package App\DataAccess\ApiGateway
*/
class ActorCodes
{
private string $apiBaseUri;

private RequestSigner $awsSignature;

private HttpClient $httpClient;

private string $traceId;

/**
* ActorCodes Constructor
*
Expand All @@ -38,12 +27,9 @@ class ActorCodes
* @param string $apiUrl
* @param string $traceId An amazon trace id to pass to subsequent services
*/
public function __construct(HttpClient $httpClient, RequestSigner $awsSignature, string $apiUrl, string $traceId)
public function __construct(private HttpClient $httpClient, private RequestSigner $awsSignature, string $apiUrl, private string $traceId)
{
$this->httpClient = $httpClient;
$this->apiBaseUri = $apiUrl;
$this->awsSignature = $awsSignature;
$this->traceId = $traceId;
}

/**
Expand All @@ -60,7 +46,7 @@ public function validateCode(string $code, string $uid, string $dob): ActorCode
[
'lpa' => $uid,
'dob' => $dob,
'code' => $code
'code' => $code,
]
);

Expand All @@ -76,21 +62,16 @@ public function validateCode(string $code, string $uid, string $dob): ActorCode
*/
public function flagCodeAsUsed(string $code): void
{
$this->makePostRequest('v1/revoke', [ 'code' => $code ]);
$this->makePostRequest('v1/revoke', ['code' => $code]);
}

/**
* @param string $lpaId
* @param string $actorId
* @return ActorCode
*/
public function checkActorHasCode(string $lpaId, string $actorId): ActorCode
{
$response = $this->makePostRequest(
'v1/exists',
[
'lpa' => $lpaId,
'actor' => $actorId
'lpa' => $lpaId,
'actor' => $actorId,
]
);

Expand All @@ -108,7 +89,7 @@ public function checkActorHasCode(string $lpaId, string $actorId): ActorCode
*/
private function makePostRequest(string $url, array $body): ResponseInterface
{
$url = sprintf("%s/%s", $this->apiBaseUri, $url);
$url = sprintf('%s/%s', $this->apiBaseUri, $url);
$body = json_encode($body);

$request = new Request('POST', $url, $this->buildHeaders(), $body);
Expand All @@ -130,8 +111,8 @@ private function makePostRequest(string $url, array $body): ResponseInterface
private function buildHeaders(): array
{
$headerLines = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];

if (!empty($this->traceId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Service\Log\RequestTracing;
use GuzzleHttp\Client as HttpClient;
use Psr\Container\ContainerInterface;
use Exception;

class ActorCodesFactory
{
Expand All @@ -15,7 +16,7 @@ public function __invoke(ContainerInterface $container): ActorCodes
$config = $container->get('config');

if (!isset($config['codes_api']['endpoint'])) {
throw new \Exception('Actor codes API Gateway endpoint is not set');
throw new Exception('Actor codes API Gateway endpoint is not set');
}

return new ActorCodes(
Expand Down
43 changes: 15 additions & 28 deletions service-api/app/src/App/src/DataAccess/ApiGateway/Lpas.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,20 @@

/**
* Looks up LPAs in the Sirius API Gateway.
*
* Class Lpas
* @package App\DataAccess\ApiGateway
*/
class Lpas implements LpasInterface
{
private string $apiBaseUri;
private AwsSignatureV4 $awsSignature;
private HttpClient $httpClient;
private LoggerInterface $logger;
private DataSanitiserStrategy $sanitiser;
private string $traceId;

public function __construct(
HttpClient $httpClient,
AwsSignatureV4 $awsSignature,
private HttpClient $httpClient,
private AwsSignatureV4 $awsSignature,
string $apiUrl,
string $traceId,
DataSanitiserStrategy $sanitiser,
LoggerInterface $logger
private string $traceId,
private DataSanitiserStrategy $sanitiser,
private LoggerInterface $logger,
) {
$this->httpClient = $httpClient;
$this->apiBaseUri = $apiUrl;
$this->awsSignature = $awsSignature;
$this->traceId = $traceId;
$this->sanitiser = $sanitiser;
$this->logger = $logger;
}

/**
Expand All @@ -75,15 +62,15 @@ public function get(string $uid): ?Response\LpaInterface
*/
public function lookup(array $uids): array
{
$provider = AwsCredentialProvider::defaultProvider();
$provider = AwsCredentialProvider::defaultProvider();
$credentials = $provider()->wait();

// Builds an array of Requests to send
// The key for each request is the original uid.
$requests = array_combine(
$uids, // Use as array key
array_map(function ($v) use ($credentials) {
$url = $this->apiBaseUri . sprintf("/v1/use-an-lpa/lpas/%s", $v);
$url = $this->apiBaseUri . sprintf('/v1/use-an-lpa/lpas/%s', $v);

$request = new Request('GET', $url, $this->buildHeaders());

Expand All @@ -98,13 +85,13 @@ public function lookup(array $uids): array

$pool = new Pool($this->httpClient, $requests, [
'concurrency' => 50,
'options' => [
'options' => [
'http_errors' => false,
],
'fulfilled' => function ($response, $id) use (&$results) {
'fulfilled' => function ($response, $id) use (&$results) {
$results[$id] = $response;
},
'rejected' => function ($reason, $id) {
'rejected' => function ($reason, $id) {
// Log?
},
]);
Expand Down Expand Up @@ -132,8 +119,8 @@ public function lookup(array $uids): array
'Unexpected {status} response from gateway for request of LPA {lpaUid}',
[
'event_code' => EventCodes::UNEXPECTED_DATA_LPA_API_RESPONSE,
'status' => $statusCode,
'lpaUid' => $uid,
'status' => $statusCode,
'lpaUid' => $uid,
]
);
unset($results[$uid]);
Expand Down Expand Up @@ -165,14 +152,14 @@ public function requestLetter(int $caseId, ?int $actorId, ?string $additionalInf
$payloadContent['actor_uid'] = $actorId;
}

$provider = AwsCredentialProvider::defaultProvider();
$provider = AwsCredentialProvider::defaultProvider();
$credentials = $provider()->wait();

// request payload
$body = json_encode($payloadContent);

// construct request for API gateway
$url = $this->apiBaseUri . '/v1/use-an-lpa/lpas/requestCode';
$url = $this->apiBaseUri . '/v1/use-an-lpa/lpas/requestCode';
$request = new Request('POST', $url, $this->buildHeaders(), $body);
$request = $this->awsSignature->signRequest($request, $credentials);

Expand All @@ -195,7 +182,7 @@ public function requestLetter(int $caseId, ?int $actorId, ?string $additionalInf
private function buildHeaders(): array
{
$headerLines = [
'Content-Type' => 'application/json',
'Content-Type' => 'application/json',
];

if (!empty($this->traceId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GuzzleHttp\Client as HttpClient;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Exception;

class LpasFactory
{
Expand All @@ -18,7 +19,7 @@ public function __invoke(ContainerInterface $container)
$config = $container->get('config');

if (!isset($config['sirius_api']['endpoint'])) {
throw new \Exception('Sirius API Gateway endpoint is not set');
throw new Exception('Sirius API Gateway endpoint is not set');
}

return new Lpas(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@

class RequestSigner
{
private ?string $staticAuthToken;

private SignatureV4 $signer;

public function __construct(SignatureV4 $signer, ?string $staticAuthToken = null)
public function __construct(private SignatureV4 $signer, private ?string $staticAuthToken = null)
{
$this->signer = $signer;
$this->staticAuthToken = $staticAuthToken;
}

public function sign(RequestInterface $request): RequestInterface
Expand Down
29 changes: 6 additions & 23 deletions service-api/app/src/App/src/DataAccess/DynamoDb/ActorCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,8 @@ class ActorCodes implements ActorCodesInterface
{
use DynamoHydrateTrait;

/**
* @var DynamoDbClient
*/
private $client;

/**
* @var string
*/
private $actorLpaCodesTable;

/**
* ViewerCodeActivity constructor.
* @param DynamoDbClient $client
* @param string $actorLpaCodesTable
*/
public function __construct(DynamoDbClient $client, string $actorLpaCodesTable)
public function __construct(private DynamoDbClient $client, private string $actorLpaCodesTable)
{
$this->client = $client;
$this->actorLpaCodesTable = $actorLpaCodesTable;
}

/**
Expand All @@ -39,7 +22,7 @@ public function get(string $code): ?array
{
$result = $this->client->getItem([
'TableName' => $this->actorLpaCodesTable,
'Key' => [
'Key' => [
'ActorCode' => [
'S' => $code,
],
Expand All @@ -57,18 +40,18 @@ public function get(string $code): ?array
public function flagCodeAsUsed(string $code)
{
$this->client->updateItem([
'TableName' => $this->actorLpaCodesTable,
'Key' => [
'TableName' => $this->actorLpaCodesTable,
'Key' => [
'ActorCode' => [
'S' => $code,
],
],
'UpdateExpression' => 'set Active=:active',
'UpdateExpression' => 'set Active=:active',
'ExpressionAttributeValues' => [
':active' => [
'BOOL' => false,
],
]
],
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Aws\DynamoDb\DynamoDbClient;
use Psr\Container\ContainerInterface;
use Exception;

class ActorCodesFactory
{
Expand All @@ -14,7 +15,7 @@ public function __invoke(ContainerInterface $container)
$config = $container->get('config');

if (!isset($config['repositories']['dynamodb']['actor-codes-table'])) {
throw new \Exception('Actor Codes table configuration not present');
throw new Exception('Actor Codes table configuration not present');
}

return new ActorCodes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Aws\DynamoDb\DynamoDbClient;
use Psr\Container\ContainerInterface;
use Exception;

class ActorUsersFactory
{
Expand All @@ -14,7 +15,7 @@ public function __invoke(ContainerInterface $container)
$config = $container->get('config');

if (!isset($config['repositories']['dynamodb']['actor-users-table'])) {
throw new \Exception('Actor Users table configuration not present');
throw new Exception('Actor Users table configuration not present');
}

return new ActorUsers(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function delete(string $lpaActorToken): array
*/
public function activateRecord(string $lpaActorToken, string $actorId, string $activationCode): array
{
$current = new DateTimeImmutable('now', new DateTimeZone('Etc/UTC'));
$current = new DateTimeImmutable('now', new DateTimeZone('Etc/UTC'));
$activatedTime = $current->format(DateTimeInterface::ATOM);

$response = $this->client->updateItem(
Expand Down
Loading
Loading