Passed
Pull Request — master (#115)
by Daniel
41:55 queued 26:56
created

Factory::makeStreamHandler()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 16
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
    private ?Closure $streamHandler = null;
0 ignored issues
show
introduced by
The private property $streamHandler is not used, and could be removed.
Loading history...
51
52
    /**
53
     * Sets the API key for the requests.
54
     */
55
    public function withApiKey(string $apiKey): self
56
    {
57
        $this->apiKey = trim($apiKey);
58
59
        return $this;
60
    }
61
62
    /**
63
     * Sets the base URI for the requests.
64
     * If no URI is provided the factory will use the default OpenAI API URI.
65
     */
66
    public function withBaseUri(string $baseUri): self
67
    {
68
        $this->baseUri = $baseUri;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Adds a custom query parameter to the request url.
75
     */
76
    public function withQueryParam(string $name, string $value): self
77
    {
78
        $this->queryParams[$name] = $value;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Creates a new Open AI Client.
85
     */
86
    public function make(): Client
87
    {
88
        $headers = Headers::create();
89
90
        if ($this->apiKey !== null) {
91
            $headers = Headers::withAuthorization(ApiKey::from($this->apiKey));
92
        }
93
94
        $baseUri = BaseUri::from($this->baseUri ?: 'api.bookwhen.com/v2');
95
96
        $queryParams = QueryParams::create();
97
        foreach ($this->queryParams as $name => $value) {
98
            $queryParams = $queryParams->withParam($name, $value);
99
        }
100
101
        $client = $this->httpClient ??= Psr18ClientDiscovery::find();
102
103
        $transporter = new HttpTransporter($client, $baseUri, $headers, $queryParams);
104
105
        return new Client($transporter);
106
    }
107
}
108