-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathFactory.php
More file actions
101 lines (78 loc) · 2.16 KB
/
Copy pathFactory.php
File metadata and controls
101 lines (78 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace Anaf;
use Anaf\Transporters\HttpTransporter;
use Anaf\ValueObjects\ApiKey;
use Anaf\ValueObjects\Transporter\BaseUri;
use Anaf\ValueObjects\Transporter\Headers;
use Anaf\ValueObjects\Transporter\QueryParams;
use GuzzleHttp\Client as GuzzleClient;
class Factory
{
/**
* The Bear token for the requests.
*/
private ?string $apiKey = null;
/**
* The base URI for the requests.
*/
private ?string $baseUri = null;
private bool $staging = false;
/**
* The query parameters for the requests.
*
* @var array<string, string|int>
*/
private array $queryParams = [];
/**
* Sets the Token for the requests.
*/
public function withApiKey(string $token): self
{
$this->apiKey = $token;
return $this;
}
/**
* Sets the base URI for the requests.
* If no URI is provided the factory will use the default ANAF API URI.
*/
public function withBaseUri(string $baseUri): self
{
$this->baseUri = $baseUri;
return $this;
}
/**
* Sets the staging mode for the requests.
*/
public function staging(): self
{
$this->staging = true;
return $this;
}
/**
* Adds a custom query parameter to the request url.
*/
public function withQueryParam(string $name, string $value): self
{
$this->queryParams[$name] = $value;
return $this;
}
/**
* Creates a new ANAF Client.
*/
public function make(): Client
{
$headers = Headers::create();
if ($this->apiKey !== null) {
$headers = Headers::withAuthorization(ApiKey::from($this->apiKey));
}
$baseUri = BaseUri::from($this->baseUri ?: 'webservicesp.anaf.ro');
$queryParams = QueryParams::create();
foreach ($this->queryParams as $name => $value) {
$queryParams = $queryParams->withParam($name, $value);
}
$client = new GuzzleClient;
$transporter = new HttpTransporter($client, $baseUri, $headers, $queryParams, $this->staging);
return new Client($transporter);
}
}