Skip to content

Commit

Permalink
Added eligibility endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
rickogden committed Jun 13, 2019
1 parent 87bf8bc commit bcc61b3
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Client
protected const PATH_CREATE_CREDIT = '/v1/environments/{environment}/credits';
protected const PATH_CREATE_QUOTE = '/v1/quote';
protected const PATH_GET_CREDIT = '/v1/credits/{id}';
protected const PATH_GET_ELIGIBILITY = '/v1/environments/{environment}/eligibility/{msisdn}';
protected const PATH_GET_NETWORKS = '/v1/networks';
protected const PATH_GET_CREDIT_TYPES_FOR_NETWORK = '/v1/networks/{network}/credit-types';

Expand Down Expand Up @@ -120,10 +121,7 @@ public function getNetworks(?string $msisdn = null): NetworkCollection
// Make the API call
$options = (null !== $msisdn) ? ['query' => ['msisdn' => $msisdn]] : [];

try {
$data = $this->makeRequest('GET', self::PATH_GET_NETWORKS, $options, $logContext);
} catch (InvalidResponseException $e) {
}
$data = $this->makeRequest('GET', self::PATH_GET_NETWORKS, $options, $logContext);

// Log a valid response
$this->logger->info(self::LOG_RESPONSE_OK, $logContext);
Expand Down Expand Up @@ -379,4 +377,48 @@ public function getRefreshed(HateoasInterface $resource): HateoasInterface

return $resource::fromJsonArray($data);
}

/**
* @param string[] $networkIds
*
* @throws GuzzleException
* @throws InvalidResponseException
* @throws FailedResponseException
*
* @return Eligibility[]
*/
public function getEligibilityForNetworkIds(string $msisdn, array $networkIds): array
{
$networks = \implode(',', $networkIds);
$uri = \strtr(static::PATH_GET_ELIGIBILITY, ['{msisdn}' => $msisdn]);
$data = $this->makeRequest('GET', $uri, [
'query' => ['networks' => $networks],
]);

return \array_map(static function (array $item): Eligibility {
return Eligibility::fromJsonArray($item);
}, $data['_embedded']['eligibility']);
}

/**
* @param iterable<Network> $networks
*
* @throws GuzzleException
* @throws InvalidResponseException
* @throws FailedResponseException
*
* @return Eligibility[]
*/
public function getEligibilityForNetworks(string $msisdn, iterable $networks): array
{
$networks = (static function (Network ...$networks): array {
return $networks;
})(...$networks);

$networkIds = \array_map(static function (Network $network): string {
return $network->getId();
}, $networks);

return $this->getEligibilityForNetworkIds($msisdn, $networkIds);
}
}
39 changes: 39 additions & 0 deletions src/Eligibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace AirtimeRewards\ARConnect;

class Eligibility
{
/**
* @var string
*/
private $eligible;

/**
* @var Network
*/
private $network;

public static function fromJsonArray(array $data): self
{
return new self($data['eligibility'], Network::fromJsonArray($data['network']));
}

public function __construct(string $eligible, Network $network)
{
$this->eligible = $eligible;
$this->network = $network;
}

public function getEligible(): string
{
return $this->eligible;
}

public function getNetwork(): Network
{
return $this->network;
}
}
44 changes: 44 additions & 0 deletions src/Test/data/eligibility.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"_embedded": {
"eligibility": [
{
"eligibility": "UNKNOWN",
"network": {
"brand": "O2",
"id": "6efa2e06-3607-46c5-a722-6da971295edf",
"subscription_types": [
"POSTPAID",
"PREPAID"
],
"_links": {
"self": {
"href": "\/v1\/networks\/6efa2e06-3607-46c5-a722-6da971295edf"
},
"credit_types": {
"href": "\/v1\/networks\/6efa2e06-3607-46c5-a722-6da971295edf\/credit-types"
}
}
}
},
{
"eligibility": "ELIGIBLE",
"network": {
"brand": "EE",
"id": "c0cdf313-bc17-4696-9236-2c41e1847b38",
"subscription_types": [
"POSTPAID",
"PREPAID"
],
"_links": {
"self": {
"href": "\/v1\/networks\/c0cdf313-bc17-4696-9236-2c41e1847b38"
},
"credit_types": {
"href": "\/v1\/networks\/c0cdf313-bc17-4696-9236-2c41e1847b38\/credit-types"
}
}
}
}
]
}
}
17 changes: 17 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,23 @@ public function testGetCredit(): void
$this->assertSame('ref5', $credit2->getClientReference());
}

public function testGetEligibility(): void
{
$this->appendResponse(\file_get_contents(__DIR__.'/../src/Test/data/eligibility.json'));
$eligibility = $this->client->getEligibilityForNetworkIds('447990099876', ['network1', 'network2']);
$this->assertSame(
'/v1/environments/foo/eligibility/447990099876?networks=network1%2Cnetwork2',
(string) $this->getLastRequest()->getUri()
);

$this->assertCount(2, $eligibility);
$this->assertInstanceOf(Eligibility::class, $eligibility[0]);
$this->assertSame('UNKNOWN', $eligibility[0]->getEligible());
$this->assertSame('O2', $eligibility[0]->getNetwork()->getBrand());
$this->assertSame('ELIGIBLE', $eligibility[1]->getEligible());
$this->assertSame('EE', $eligibility[0]->getNetwork()->getBrand());
}

protected function appendResponse(string $data, int $responseCode = 200): void
{
$this->mock->append(new Response($responseCode, ['Content-Type' => 'application/json'], $data));
Expand Down

0 comments on commit bcc61b3

Please sign in to comment.