Passed
Pull Request — develop (#146)
by
unknown
01:38
created

Factory::withHttpClient()   A

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
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 HTTP headers for the requests.
37
     *
38
     * @var array<string, string>
39
     */
40
    private array $headers = [];
0 ignored issues
show
introduced by
The private property $headers is not used, and could be removed.
Loading history...
41
42
    /**
43
     * The query parameters for the requests.
44
     *
45
     * @var array<string, string|int>
46
     */
47
    private array $queryParams = [];
48
49
    /**
50
     * Sets the API key for the requests.
51
     */
52 9
    public function withApiKey(string $apiKey): self
53
    {
54 9
        $this->apiKey = trim($apiKey);
55
56 9
        return $this;
57
    }
58
59
    /**
60
     * Sets the base URI for the requests.
61
     * If no URI is provided the factory will use the default OpenAI API URI.
62
     */
63
    public function withBaseUri(string $baseUri): self
64
    {
65
        $this->baseUri = $baseUri;
66
67
        return $this;
68
    }
69
70
    /**
71
     * Sets the HTTP client for the requests.
72
     * If no client is provided the factory will try to find one using PSR-18 HTTP Client Discovery.
73
     */
74
    public function withHttpClient(ClientInterface $client): self
75
    {
76
        $this->httpClient = $client;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Adds a custom query parameter to the request url.
83
     */
84
    public function withQueryParam(string $name, string $value): self
85
    {
86
        $this->queryParams[$name] = $value;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Creates a new Open AI Client.
93
     */
94 9
    public function make(): Client
95
    {
96 9
        $headers = Headers::create();
97
98 9
        if (null !== $this->apiKey) {
99 9
            $headers = Headers::withAuthorization(ApiKey::from($this->apiKey));
100
        }
101
102 9
        $baseUri = BaseUri::from($this->baseUri ?: 'api.bookwhen.com/v2');
103
104 9
        $queryParams = QueryParams::create();
105 9
        foreach ($this->queryParams as $name => $value) {
106
            $queryParams = $queryParams->withParam($name, $value);
107
        }
108
109 9
        $client = $this->httpClient ??= Psr18ClientDiscovery::find();
110
111 9
        $transporter = new HttpTransporter($client, $baseUri, $headers, $queryParams);
112
113 9
        return new Client($transporter);
114
    }
115
}
116