Skip to content

Commit

Permalink
Refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TecsiAron committed Dec 16, 2024
1 parent 3456a62 commit ee32958
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 67 deletions.
7 changes: 4 additions & 3 deletions src/ANAFAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace EdituraEDU\ANAF;

use EdituraEDU\ANAF\Responses\ANAFAnswerListResponse;
use EdituraEDU\ANAF\Responses\InternalPagedAnswersResponse;
use EdituraEDU\ANAF\Responses\ANAFException;
use EdituraEDU\ANAF\Responses\ANAFVerifyResponse;
use EdituraEDU\ANAF\Responses\EntityResponse;
use EdituraEDU\ANAF\Responses\InternalPagedAnswersResponse;
use EdituraEDU\ANAF\Responses\PagedAnswerListResponse;
use EdituraEDU\ANAF\Responses\TVAResponse;
use EdituraEDU\ANAF\Responses\UBLUploadResponse;
Expand Down Expand Up @@ -526,7 +526,7 @@ public function ListAnswers(int $cif, int $days = 60, string|null $filter = null
/**
* Get answer list for a company (authenticated user must have access to the company!)
* Uses the paged API end point
* Answers will returned in a unified list
* Answers will be returned in a unified list
* @param int $startTime
* @param int $endTime
* @param int $cif
Expand All @@ -545,7 +545,7 @@ public function ListAnswersWithPagination(int $startTime, int $endTime, int $cif
if ($specificPage != null) {
$response = $this->GetAnswerPage($startTime, $endTime, $cif, $specificPage, $filter);
if ($response->IsSuccess()) {
return new PagedAnswerListResponse([$response],[]);
return new PagedAnswerListResponse([$response], []);
}
return PagedAnswerListResponse::CreateError($response->LastError);
}
Expand Down Expand Up @@ -696,6 +696,7 @@ public static function ValidateCIF(string|false $cif): bool
if ($cif === false) {
return false;
}
/** @noinspection PhpConditionAlreadyCheckedInspection */
if (!is_int($cif)) {
$cif = strtoupper($cif);
if (str_starts_with($cif, 'RO')) {
Expand Down
3 changes: 2 additions & 1 deletion src/ANAFEntity/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EdituraEDU\ANAF\ANAFEntity;

use stdClass;
use Throwable;

/**
* Represents an address based on the ANAF API response structure
Expand Down Expand Up @@ -43,7 +44,7 @@ public static function CreateFromParsed(stdClass $parsed): ?Address
}
}
return $result;
} catch (\Throwable $th) {
} catch (Throwable) {
return null;
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/ANAFEntity/Entity.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace EdituraEDU\ANAF\ANAFEntity;
use stdClass;

/**
* Represents the comprehensive response structure for a company/institution from the ANAF API
*/
Expand Down Expand Up @@ -38,10 +40,10 @@ class Entity

/**
* Used to convert the parsed data (json) from the ANAF API to an Entity object
* @param \stdClass $parsedData
* @param stdClass $parsedData
* @return Entity
*/
public static function CreateFromParsed(\stdClass $parsedData): Entity
public static function CreateFromParsed(stdClass $parsedData): Entity
{
$entity = new Entity();
$entity->date_generale = isset($parsedData->date_generale) ? GeneralInfo::CreateFromParsed($parsedData->date_generale) : null;
Expand Down Expand Up @@ -85,7 +87,7 @@ public function GetSector(): ?int
*/
public function IsRadiat(): bool
{
return str_starts_with(strtolower($this->date_generale->stare_inregistrare), "radi",);
return str_starts_with(strtolower($this->date_generale->stare_inregistrare), "radi");
}

}
3 changes: 2 additions & 1 deletion src/ANAFEntity/GeneralInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EdituraEDU\ANAF\ANAFEntity;

use stdClass;
use Throwable;

/**
* Represents the general information about a company based on the ANAF API response structure
Expand Down Expand Up @@ -45,7 +46,7 @@ public static function CreateFromParsed(stdClass $parsed): ?GeneralInfo
}
}
return $result;
} catch (\Throwable $th) {
} catch (Throwable $th) {
error_log($th->getMessage());
error_log($th->getTraceAsString());
return null;
Expand Down
6 changes: 4 additions & 2 deletions src/ANAFEntity/InactiveInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace EdituraEDU\ANAF\ANAFEntity;

use stdClass;

/**
* Represents the inactive information about a company based on the ANAF API response structure
*/
Expand All @@ -14,11 +16,11 @@ class InactiveInfo
public bool $statusInactivi = false;

/**
* Similar to @param \stdClass $parsedData
* Similar to @param stdClass $parsedData
* @return InactiveInfo
* @see Entity::CreateFromParsed
*/
public static function CreateFromParsed(\stdClass $parsedData): InactiveInfo
public static function CreateFromParsed(stdClass $parsedData): InactiveInfo
{
$inactiveInfo = new InactiveInfo();
$inactiveInfo->dataInactivare = $parsedData->dataInactivare ?? "";
Expand Down
6 changes: 4 additions & 2 deletions src/ANAFEntity/RTVAInfo.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace EdituraEDU\ANAF\ANAFEntity;
use stdClass;

/**
* Represents the RTVA (taxable on payment?) information about a company based on the ANAF API response structure
*/
Expand All @@ -14,11 +16,11 @@ class RTVAInfo
public bool $statusTvaIncasare = false;

/**
* Similar to @param \stdClass $parsedData
* Similar to @param stdClass $parsedData
* @return RTVAInfo
* @see Entity::CreateFromParsed
*/
public static function CreateFromParsed(\stdClass $parsedData): RTVAInfo
public static function CreateFromParsed(stdClass $parsedData): RTVAInfo
{
$rtvaInfo = new RTVAInfo();
$rtvaInfo->dataInceputTvaInc = $parsedData->dataInceputTvaInc ?? "";
Expand Down
6 changes: 4 additions & 2 deletions src/ANAFEntity/SplitTVAInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace EdituraEDU\ANAF\ANAFEntity;

use stdClass;

/**
* Represents the split TVA(broken down tax payment?) information about a company based on the ANAF API response structure
*/
Expand All @@ -12,11 +14,11 @@ class SplitTVAInfo
public bool $statusSplitTVA = false;

/**
* Similar to @param \stdClass $parsedData
* Similar to @param stdClass $parsedData
* @return SplitTVAInfo
* @see Entity::CreateFromParsed
*/
public static function CreateFromParsed(\stdClass $parsedData): SplitTVAInfo
public static function CreateFromParsed(stdClass $parsedData): SplitTVAInfo
{
$splitTVAInfo = new SplitTVAInfo();
$splitTVAInfo->dataInceputSplitTVA = $parsedData->dataInceputSplitTVA ?? "";
Expand Down
30 changes: 12 additions & 18 deletions src/Responses/InternalPagedAnswersResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,25 @@ class InternalPagedAnswersResponse extends ANAFAnswerListResponse
public function Parse(): void
{
$pattern = '/Pagina solicitata \d+ este mai mare decat numarul total de pagini \d+/';
try
{
try {
$parsed = $this->CommonParseJSON($this->rawResponse);
}
catch (\Throwable $th)
{
} catch (Throwable $th) {
$this->LastError = $th;
return;
}
if(str_starts_with(strtolower($parsed->titlu), "lista mesaje")
&& isset($parsed->eroare)
&& !isset($parsed->mesaje)
&& !isset($parsed->serial)
&& !isset($parsed->cui)
&& preg_match($pattern, $parsed->eroare))
{
if (str_starts_with(strtolower($parsed->titlu), "lista mesaje")
&& isset($parsed->eroare)
&& !isset($parsed->mesaje)
&& !isset($parsed->serial)
&& !isset($parsed->cui)
&& preg_match($pattern, $parsed->eroare)) {
$parsed->mesaje = [];
$parsed->serial = "";
$parsed->cui = "";
return;
}
parent::Parse();
if(isset($this->mesaje) && count($this->mesaje)>0)
{
if (isset($this->mesaje) && count($this->mesaje) > 0) {
$this->numar_inregistrari_in_pagina = $parsed->numar_inregistrari_in_pagina;
$this->numar_total_inregistrari_per_pagina = $parsed->numar_total_inregistrari_per_pagina;
$this->numar_total_inregistrari = $parsed->numar_total_inregistrari;
Expand All @@ -57,11 +52,10 @@ public function Parse(): void
* Will always return true if the response is not successful
* @return bool
*/
public function IsLastPage():bool
public function IsLastPage(): bool
{
if($this->IsSuccess())
{
return $this->index_pagina_curenta>=$this->numar_total_pagini;
if ($this->IsSuccess()) {
return $this->index_pagina_curenta >= $this->numar_total_pagini;
}
return true;
}
Expand Down
45 changes: 21 additions & 24 deletions src/Responses/PagedAnswerListResponse.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace EdituraEDU\ANAF\Responses;

use Exception;
use Throwable;

Expand All @@ -26,40 +28,35 @@ class PagedAnswerListResponse extends ANAFAnswerListResponse
* @var int $PageCount
*/
public int $PageCount;

/**
* @param InternalPagedAnswersResponse[] $responses
* @param InternalPagedAnswersResponse[] $errors
* @return void
*/
public function __construct(array $responses, array $errors)
{
$this->ErrorPages=$errors;
$this->PageCount=count($responses);
if($this->PageCount==0)
{
$this->Pages=[];
$this->mesaje=[];
$this->ErrorPages = $errors;
$this->PageCount = count($responses);
if ($this->PageCount == 0) {
$this->Pages = [];
$this->mesaje = [];
return;
}
$this->Pages = $responses;
$this->cui=$responses[0]->cui;
$this->serial=$responses[0]->serial;
$this->titlu=$responses[0]->titlu;
$this->mesaje=[];
if(count($errors)!=0)
{
if(count($errors)==1)
{
$this->LastError=$errors[0]->LastError;
}
else
{
$this->LastError=new ANAFException("Multiple page requests result in an error", ANAFException::COMPOUND_ERROR);
$this->cui = $responses[0]->cui;
$this->serial = $responses[0]->serial;
$this->titlu = $responses[0]->titlu;
$this->mesaje = [];
if (count($errors) != 0) {
if (count($errors) == 1) {
$this->LastError = $errors[0]->LastError;
} else {
$this->LastError = new ANAFException("Multiple page requests result in an error", ANAFException::COMPOUND_ERROR);
}
}
for($i=0; $i<$this->PageCount; $i++)
{
$this->mesaje=array_merge($this->mesaje,$responses[$i]->mesaje);
for ($i = 0; $i < $this->PageCount; $i++) {
$this->mesaje = array_merge($this->mesaje, $responses[$i]->mesaje);
}
}

Expand All @@ -70,8 +67,8 @@ public static function Create($rawResponse): PagedAnswerListResponse

public static function CreateError(Throwable $error): PagedAnswerListResponse
{
$response=new PagedAnswerListResponse([],[]);
$response->LastError=$error;
$response = new PagedAnswerListResponse([], []);
$response->LastError = $error;
return $response;

}
Expand Down
3 changes: 0 additions & 3 deletions tests/RequestTests/AccessTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace EdituraEDU\ANAF\Tests\RequestTests;

use EdituraEDU\ANAF\ANAFAPIClient;
use PHPUnit\Framework\Attributes\CoversFunction;

class AccessTokenTest extends RequestTestBase
{
public function testHasAccessToken(): void
Expand Down
8 changes: 1 addition & 7 deletions tests/RequestTests/AnswerListAnswerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

use DateInterval;
use DateTime;
use EdituraEDU\ANAF\ANAFAPIClient;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversFunction;
use PHPUnit\Framework\Attributes\Depends;
use PHPUnit\Framework\Attributes\DependsExternal;
use PHPUnit\Framework\Attributes\UsesFunction;
use Throwable;

class AnswerListAnswerTest extends RequestTestBase
Expand Down Expand Up @@ -87,7 +81,7 @@ public function testAnswersListWithPages()
if ($original->id == $paged->id) {
$found = true;
$this->assertEquals($original->detalii, $paged->detalii);
break 2;
break 1;
}
}
$this->assertTrue($found, "Message not found in original list");
Expand Down
1 change: 0 additions & 1 deletion tests/RequestTests/UploadUBLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace EdituraEDU\ANAF\Tests\RequestTests;

use PHPUnit\Framework\TestCase;
use Throwable;

class UploadUBLTest extends RequestTestBase
Expand Down

0 comments on commit ee32958

Please sign in to comment.