Completed
Push — main ( 439431...5a0a5a )
by Daniel
27s queued 13s
created

Factory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 69.57%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 89
ccs 16
cts 23
cp 0.6957
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A withQueryParam() 0 5 1
A make() 0 20 4
A withBaseUri() 0 5 1
A withApiKey() 0 5 1
A withHttpClient() 0 5 1
1
<?php
2
3
namespace InShore\Bookwhen;
4
5
use Exception;
6
use GuzzleHttp\Client as GuzzleClient;
7
use Http\Discovery\Psr18ClientDiscovery;
8
use InShore\Bookwhen\Transporters\HttpTransporter;
9
use InShore\Bookwhen\ValueObjects\ApiKey;
10
use InShore\Bookwhen\ValueObjects\Transporter\BaseUri;
11
use InShore\Bookwhen\ValueObjects\Transporter\Headers;
12
use InShore\Bookwhen\ValueObjects\Transporter\QueryParams;
13
use Psr\Http\Client\ClientInterface;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
use Symfony\Component\HttpClient\Psr18Client;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpClient\Psr18Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
final class Factory
19
{
20
    /**
21
     * The API key for the requests.
22
     */
23
    private ?string $apiKey = null;
24
25
    /**
26
     * The HTTP client for the requests.
27
     */
28
    private ?ClientInterface $httpClient = null;
29
30
    /**
31
     * The base URI for the requests.
32
     */
33
    private ?string $baseUri = null;
34
35
    /**
36
     * The query parameters for the requests.
37
     *
38
     * @var array<string, string|int>
39
     */
40
    private array $queryParams = [];
41
42
    /**
43
     * Sets the API key for the requests.
44
     */
45 20
    public function withApiKey(string $apiKey): self
46
    {
47 20
        $this->apiKey = trim($apiKey);
48
49 20
        return $this;
50
    }
51
52
    /**
53
     * Sets the base URI for the requests.
54
     * If no URI is provided the factory will use the default OpenAI API URI.
55
     */
56
    public function withBaseUri(string $baseUri): self
57
    {
58
        $this->baseUri = $baseUri;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Sets the HTTP client for the requests.
65
     * If no client is provided the factory will try to find one using PSR-18 HTTP Client Discovery.
66
     */
67 11
    public function withHttpClient(ClientInterface $client): self
68
    {
69 11
        $this->httpClient = $client;
70
71 11
        return $this;
72
    }
73
74
    /**
75
     * Adds a custom query parameter to the request url.
76
     */
77
    public function withQueryParam(string $name, string $value): self
78
    {
79
        $this->queryParams[$name] = $value;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Creates a new Open AI Client.
86
     */
87 20
    public function make(): Client
88
    {
89 20
        $headers = Headers::create();
90
91 20
        if (null !== $this->apiKey) {
92 20
            $headers = Headers::withAuthorization(ApiKey::from($this->apiKey));
93
        }
94
95 20
        $baseUri = BaseUri::from($this->baseUri ?: 'api.bookwhen.com/v2');
96
97 20
        $queryParams = QueryParams::create();
98 20
        foreach ($this->queryParams as $name => $value) {
99
            $queryParams = $queryParams->withParam($name, $value);
100
        }
101
102 20
        $client = $this->httpClient ??= Psr18ClientDiscovery::find();
103
104 20
        $transporter = new HttpTransporter($client, $baseUri, $headers, $queryParams);
105
106 20
        return new Client($transporter);
107
    }
108
}
109