Factory::withQueryParam()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
namespace InShore\Bookwhen;
4
5
use Http\Discovery\Psr18ClientDiscovery;
6
use InShore\Bookwhen\Transporters\HttpTransporter;
7
use InShore\Bookwhen\ValueObjects\ApiKey;
8
use InShore\Bookwhen\ValueObjects\Transporter\BaseUri;
9
use InShore\Bookwhen\ValueObjects\Transporter\Headers;
10
use InShore\Bookwhen\ValueObjects\Transporter\QueryParams;
11
use Psr\Http\Client\ClientInterface;
12
use Symfony\Component\HttpClient\Psr18Client;
13
14
final class Factory
15
{
16
    /**
17
     * The API key for the requests.
18
     */
19
    private ?string $apiKey = null;
20
21
    /**
22
     * The HTTP client for the requests.
23
     */
24
    private ?ClientInterface $httpClient = null;
25
26
    /**
27
     * The base URI for the requests.
28
     */
29
    private ?string $baseUri = null;
30
31
    /**
32
     * The query parameters for the requests.
33
     *
34
     * @var array<string, string|int>
35
     */
36
    private array $queryParams = [];
37
38
    /**
39
     * Sets the API key for the requests.
40
     */
41
    public function withApiKey(string $apiKey): self
42
    {
43
        $this->apiKey = trim($apiKey);
44
45 20
        return $this;
46
    }
47 20
48
    /**
49 20
     * Sets the base URI for the requests.
50
     * If no URI is provided the factory will use the default OpenAI API URI.
51
     */
52
    public function withBaseUri(string $baseUri): self
53
    {
54
        $this->baseUri = $baseUri;
55
56
        return $this;
57
    }
58
59
    /**
60
     * Sets the HTTP client for the requests.
61
     * If no client is provided the factory will try to find one using PSR-18 HTTP Client Discovery.
62
     */
63
    public function withHttpClient(ClientInterface $client): self
64
    {
65
        $this->httpClient = $client;
66
67 11
        return $this;
68
    }
69 11
70
    /**
71 11
     * Adds a custom query parameter to the request url.
72
     */
73
    public function withQueryParam(string $name, string $value): self
74
    {
75
        $this->queryParams[$name] = $value;
76
77
        return $this;
78
    }
79
80
    /**
81
     * Creates a new Open AI Client.
82
     */
83
    public function make(): Client
84
    {
85
        $headers = Headers::create();
86
87 20
        if (null !== $this->apiKey) {
88
            $headers = Headers::withAuthorization(ApiKey::from($this->apiKey));
89 20
        }
90
91 20
        $baseUri = BaseUri::from($this->baseUri ?: 'api.bookwhen.com/v2');
92 20
93
        $queryParams = QueryParams::create();
94
        foreach ($this->queryParams as $name => $value) {
95 20
            $queryParams = $queryParams->withParam($name, $value);
96
        }
97 20
98 20
        $client = $this->httpClient ??= Psr18ClientDiscovery::find();
99
100
        $transporter = new HttpTransporter($client, $baseUri, $headers, $queryParams);
101
102 20
        return new Client($transporter);
103
    }
104
}
105