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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
*.swp
*.swo
reports/*
.idea
26 changes: 18 additions & 8 deletions src/Anaf.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,32 @@ class Anaf
/**
* Creates a new Anaf Authorized Client with the given api key.
*/
public static function authorizedClient(string $apiKey): Client
public static function authorizedClient(string $apiKey, bool $isStage = false): Client
{
return self::factory()
$factory = self::factory()
->withApiKey($apiKey)
->withBaseUri('api.anaf.ro')
->make();
->withBaseUri('api.anaf.ro');

if (!$isStage) {
return $factory->make();
}

return $factory->staging()->make();
}

/**
* Creates a new Anaf Client for non-authorized requests.
*/
public static function client(): Client
public static function client(bool $isStage = false): Client
{
return self::factory()
->withBaseUri('webservicesp.anaf.ro')
->make();
$factory = self::factory()
->withBaseUri('webservicesp.anaf.ro');

if (!$isStage) {
return $factory->make();
}

return $factory->staging()->make();
}

/**
Expand Down
11 changes: 3 additions & 8 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Factory
*/
private ?string $baseUri = null;

private static bool $staging = false;
private bool $staging = false;

/**
* The query parameters for the requests.
Expand Down Expand Up @@ -58,16 +58,11 @@ public function withBaseUri(string $baseUri): self
*/
public function staging(): self
{
self::$staging = true;
$this->staging = true;

return $this;
}

public static function isStaging(): bool
{
return self::$staging;
}

/**
* Adds a custom query parameter to the request url.
*/
Expand Down Expand Up @@ -99,7 +94,7 @@ public function make(): Client

$client = new GuzzleClient;

$transporter = new HttpTransporter($client, $baseUri, $headers, $queryParams);
$transporter = new HttpTransporter($client, $baseUri, $headers, $queryParams, $this->staging);

return new Client($transporter);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct(
private readonly BaseUri $baseUri,
private readonly Headers $headers,
private readonly QueryParams $queryParams,
private readonly bool $isStage = false,
) {
// ..
}
Expand All @@ -41,7 +42,7 @@ public function __construct(
*/
public function requestObject(Payload $payload): array
{
$request = $payload->toRequest($this->baseUri, $this->headers, $this->queryParams);
$request = $payload->toRequest($this->baseUri, $this->headers, $this->queryParams, $this->isStage);

try {
$response = $this->client->sendRequest($request);
Expand Down Expand Up @@ -81,7 +82,7 @@ public function requestObject(Payload $payload): array

public function requestFile(Payload $payload): FileHandler
{
$request = $payload->toRequest($this->baseUri, $this->headers, $this->queryParams);
$request = $payload->toRequest($this->baseUri, $this->headers, $this->queryParams, $this->isStage);

try {
$response = $this->client->sendRequest($request);
Expand Down
8 changes: 4 additions & 4 deletions src/ValueObjects/Transporter/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public static function get(string $resource, array $parameters): self
/**
* Creates a new Psr 7 Request instance.
*/
public function toRequest(BaseUri $baseUri, Headers $headers, QueryParams $queryParams): RequestInterface
public function toRequest(BaseUri $baseUri, Headers $headers, QueryParams $queryParams, bool $isStage = false): RequestInterface
{
$psr17Factory = new Psr17Factory;

$uri = $this->buildUri($baseUri);
$uri = $this->buildUri($baseUri, $isStage);

$queryParams = $queryParams->toArray();

Expand Down Expand Up @@ -123,11 +123,11 @@ public function toRequest(BaseUri $baseUri, Headers $headers, QueryParams $query
return $request;
}

private function buildUri(BaseUri $baseUri): string
private function buildUri(BaseUri $baseUri, bool $isStage = false): string
{
$uri = $baseUri->toString().$this->uri->toString();

if (! Factory::isStaging()) {
if (! $isStage) {
return $uri;
}

Expand Down
64 changes: 63 additions & 1 deletion tests/Anaf.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,77 @@

use Anaf\Client;

it('may create a client on stage and a client on prod', function () {
$anafClientStage = Anaf::client(isStage: true);
$anafClientProd = Anaf::client();

expect($anafClientStage)->toBeInstanceOf(Client::class)
->and($anafClientProd)->toBeInstanceOf(Client::class);

checkStageValue($anafClientStage, true);
checkStageValue($anafClientProd, false);

});

it('may create an authorized client on stage and an authorized client on prod', function () {
$anafClientStage = Anaf::authorizedClient('dummy-api-key', isStage: true);
$anafClientProd = Anaf::authorizedClient('dummy-api-key');

expect($anafClientStage)->toBeInstanceOf(Client::class)
->and($anafClientProd)->toBeInstanceOf(Client::class);

checkStageValue($anafClientStage, true);
checkStageValue($anafClientProd, false);
});

it('may create an authorized on stage and a client on prod', function () {
$anafClientStage = Anaf::authorizedClient('dummy-api-key', isStage: true);
$anafClientProd = Anaf::client();

expect($anafClientStage)->toBeInstanceOf(Client::class)
->and($anafClientProd)->toBeInstanceOf(Client::class);

checkStageValue($anafClientStage, true);
checkStageValue($anafClientProd, false);
});

it('may create a client on stage and an authorized on prod', function () {
$anafClientStage = Anaf::client(isStage: true);
$anafClientProd = Anaf::authorizedClient('dummy-api-key');

expect($anafClientStage)->toBeInstanceOf(Client::class)
->and($anafClientProd)->toBeInstanceOf(Client::class);

checkStageValue($anafClientStage, true);
checkStageValue($anafClientProd, false);
});

it('may create a client on stage', function () {
$anafClient = Anaf::client(isStage: true);

expect($anafClient)->toBeInstanceOf(Client::class);
checkStageValue($anafClient, true);
});

it('may create an authorized client on stage', function () {
$anafClient = Anaf::authorizedClient('dummy-api-key', isStage: true);

expect($anafClient)->toBeInstanceOf(Client::class);
checkStageValue($anafClient, true);
});

it('may create a client', function () {
$anafClient = Anaf::client();

expect($anafClient)->toBeInstanceOf(Client::class);
checkStageValue($anafClient, false);
});

it('may create a authorized client', function () {
it('may create an authorized client', function () {
$anafClient = Anaf::authorizedClient('dummy-api-key');

expect($anafClient)->toBeInstanceOf(Client::class);
checkStageValue($anafClient, false);
});

it('may create a client via factory', function () {
Expand All @@ -21,3 +82,4 @@

expect($anafClient)->toBeInstanceOf(Client::class);
});

15 changes: 15 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,18 @@ function mockAuthorizedClient(string $method, string $resource, Response|Respons

return new Client($transporter);
}

function checkStageValue(Client $anafClient, bool $expectedValue): void
{
$transporterReflection = new ReflectionClass($anafClient);
$transporterProperty = $transporterReflection->getProperty('transporter');
$transporterProperty->setAccessible(true);
$transporter = $transporterProperty->getValue($anafClient);

$stagingReflection = new ReflectionClass($transporter);
$stagingProperty = $stagingReflection->getProperty('isStage');
$stagingProperty->setAccessible(true);
$staging = $stagingProperty->getValue($transporter);

expect($staging)->toBe($expectedValue);
}