Skip to content

Commit

Permalink
Upload efactura
Browse files Browse the repository at this point in the history
  • Loading branch information
ciungulete committed Jan 20, 2024
1 parent 5d8e902 commit 8b05ce7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
Binary file modified art/social.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"require": {
"php": "^8.1.0",
"ext-iconv": "*",
"guzzlehttp/guzzle": "^7.5.0",
"php-http/discovery": "^1.19",
"ext-libxml": "*",
"ext-simplexml": "*",
"ext-libxml": "*"
"guzzlehttp/guzzle": "^7.5.0",
"php-http/discovery": "^1.19"
},
"require-dev": {
"laravel/pint": "^1.13.7",
Expand All @@ -25,8 +25,7 @@
"pestphp/pest-plugin-arch": "^2.5",
"pestphp/pest-plugin-type-coverage": "^2.7",
"phpstan/phpstan": "^1.10.54",
"rector/rector": "^0.18.13",
"spatie/ray": "^1.40",
"rector/rector": "^0.17.13",
"symfony/var-dumper": "^6.2.2"
},
"autoload": {
Expand Down
2 changes: 0 additions & 2 deletions src/Resources/Efactura.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Anaf\Contracts\FileContract;
use Anaf\Enums\Efactura\UploadStandard;
use Anaf\Enums\Transporter\ContentType;
use Anaf\Responses\Efactura\CreateMessagesResponse;
use Anaf\Responses\Efactura\CreateUploadResponse;
use Anaf\ValueObjects\Transporter\Payload;
Expand Down Expand Up @@ -35,7 +34,6 @@ public function upload(string $xml_path, string $tax_identification_number, Uplo
'standard' => $standard->value,
...($extern ? ['extern' => 'DA'] : []),
],
contentType: ContentType::ALL,
);

/** @var array<array-key, array{dateResponse: string, ExecutionStatus: string, index_incarcare: string}> $response */
Expand Down
5 changes: 3 additions & 2 deletions src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ public function requestObject(Payload $payload): array
throw new UnserializableResponse($jsonException);
}

$jsonResponse = json_encode($xml);
try {
$jsonResponse = json_encode($xml, JSON_THROW_ON_ERROR);

if ($jsonResponse === false) {
} catch (JsonException $jsonException) {
throw new UnserializableResponse($jsonException);
}

Expand Down
17 changes: 11 additions & 6 deletions src/ValueObjects/Transporter/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Payload
*/
private function __construct(
private readonly ContentType $contentType,
private readonly ContentType $acceptContentType,
private readonly Method $method,
private readonly ResourceUri $uri,
private readonly array $parameters = [],
Expand All @@ -40,23 +41,26 @@ private function __construct(
public static function create(string $resource, array $parameters): self
{
$contentType = ContentType::JSON;
$acceptContentType = ContentType::JSON;
$method = Method::POST;
$uri = ResourceUri::create($resource);

return new self($contentType, $method, $uri, $parameters);
return new self($contentType, $acceptContentType, $method, $uri, $parameters);
}

/**
* Creates a new Payload value object from the given parameters.
*
* @param array<array-key, string> $parameters
*/
public static function upload(string $resource, string $body, array $parameters = [], ContentType $contentType = ContentType::TEXT): self
public static function upload(string $resource, string $body, array $parameters = []): self
{
$method = Method::POST;
$uri = ResourceUri::create($resource);
$contentType = ContentType::TEXT;
$acceptContentType = ContentType::ALL;

return new self($contentType, $method, $uri, $parameters, $body);
return new self($contentType, $acceptContentType, $method, $uri, $parameters, $body);
}

/**
Expand All @@ -67,10 +71,11 @@ public static function upload(string $resource, string $body, array $parameters
public static function get(string $resource, array $parameters): self
{
$contentType = ContentType::JSON;
$acceptContentType = ContentType::JSON;
$method = Method::GET;
$uri = ResourceUri::get($resource);

return new self($contentType, $method, $uri, $parameters);
return new self($contentType, $acceptContentType, $method, $uri, $parameters);
}

/**
Expand All @@ -88,7 +93,7 @@ public function toRequest(BaseUri $baseUri, Headers $headers, QueryParams $query
$queryParams = [...$queryParams, ...$this->parameters];
}

if ($this->method === Method::POST && $this->contentType === ContentType::ALL) {
if ($this->method === Method::POST && in_array($this->contentType, [ContentType::ALL, ContentType::TEXT])) {
$queryParams = [...$queryParams, ...$this->parameters];
}

Expand All @@ -98,7 +103,7 @@ public function toRequest(BaseUri $baseUri, Headers $headers, QueryParams $query

$headers = $headers
->withContentType($this->contentType)
->acceptContentType($this->contentType);
->acceptContentType($this->acceptContentType);

$body = match ($this->contentType) {
ContentType::JSON => $this->method === Method::GET ? null : $psr17Factory->createStream(json_encode($this->parameters, JSON_THROW_ON_ERROR)),
Expand Down

0 comments on commit 8b05ce7

Please sign in to comment.