From a15168c0651a669fc386381d0a722641849bfe98 Mon Sep 17 00:00:00 2001 From: Andrei Dumitrescu Date: Mon, 19 Jan 2026 20:11:40 +0200 Subject: [PATCH] added internal stage option --- .gitignore | 1 + src/Anaf.php | 26 +++++++--- src/Factory.php | 11 ++-- src/Transporters/HttpTransporter.php | 5 +- src/ValueObjects/Transporter/Payload.php | 8 +-- tests/Anaf.php | 64 +++++++++++++++++++++++- tests/Pest.php | 15 ++++++ 7 files changed, 107 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 24a3f07..49352e0 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ *.swp *.swo reports/* +.idea diff --git a/src/Anaf.php b/src/Anaf.php index 5ebc46c..c423175 100644 --- a/src/Anaf.php +++ b/src/Anaf.php @@ -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(); } /** diff --git a/src/Factory.php b/src/Factory.php index 65019e3..1687425 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -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. @@ -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. */ @@ -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); } diff --git a/src/Transporters/HttpTransporter.php b/src/Transporters/HttpTransporter.php index 9f2ee14..d068e52 100644 --- a/src/Transporters/HttpTransporter.php +++ b/src/Transporters/HttpTransporter.php @@ -30,6 +30,7 @@ public function __construct( private readonly BaseUri $baseUri, private readonly Headers $headers, private readonly QueryParams $queryParams, + private readonly bool $isStage = false, ) { // .. } @@ -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); @@ -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); diff --git a/src/ValueObjects/Transporter/Payload.php b/src/ValueObjects/Transporter/Payload.php index d86830e..6960066 100644 --- a/src/ValueObjects/Transporter/Payload.php +++ b/src/ValueObjects/Transporter/Payload.php @@ -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(); @@ -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; } diff --git a/tests/Anaf.php b/tests/Anaf.php index c9a0d33..33e8d19 100644 --- a/tests/Anaf.php +++ b/tests/Anaf.php @@ -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 () { @@ -21,3 +82,4 @@ expect($anafClient)->toBeInstanceOf(Client::class); }); + diff --git a/tests/Pest.php b/tests/Pest.php index 3c6eb4b..a537e29 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -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); +}