Passed
Push — develop ( 0943c0...98f868 )
by Daniel
01:59 queued 23s
created

Factory::withApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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