Config   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 149
rs 10
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 5 1
A setTempDirPath() 0 5 1
A setClientConfig() 0 3 1
A get() 0 3 1
A setCredentials() 0 5 1
A getOrFail() 0 7 2
A setCredentialsConfig() 0 5 1
A disableRetries() 0 3 1
A getTempDirPath() 0 3 1
A getConnectorConfig() 0 27 6
A setApiEndpoint() 0 5 1
A setTransport() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AhmadMayahi\Vision;
6
7
use AhmadMayahi\Vision\Exceptions\ConfigException;
8
use Google\ApiCore\CredentialsWrapper;
9
use Google\ApiCore\Transport\TransportInterface;
10
use Google\Auth\FetchAuthTokenInterface;
11
12
class Config
13
{
14
    protected array $config = [];
15
16
    /**
17
     * The credentials to be used by the client to authorize API calls. This option
18
     * accepts either a path to a credentials file, or a decoded credentials file as a PHP array.
19
     * *Advanced usage*: In addition, this option can also accept a pre-constructed
20
     * {@see \Google\Auth\FetchAuthTokenInterface} object or
21
     * {@see \Google\ApiCore\CredentialsWrapper} object. Note that when one of these
22
     * objects are provided, any settings in $credentialsConfig will be ignored.
23
     *
24
     * @return $this
25
     */
26
    public function setCredentials(string|array|FetchAuthTokenInterface|CredentialsWrapper $val): static
27
    {
28
        $this->config['credentials'] = $val;
29
30
        return $this;
31
    }
32
33
    /**
34
     * Options used to configure credentials, including auth token caching, for the client.
35
     * For a full list of supporting configuration options:
36
     * @see \Google\ApiCore\CredentialsWrapper::build()
37
     *
38
     * @param array $val
39
     * @return $this
40
     */
41
    public function setCredentialsConfig(array $val): static
42
    {
43
        $this->config['credentialsConfig'] = $val;
44
45
        return $this;
46
    }
47
48
    public function setTempDirPath(string $path): static
49
    {
50
        $this->config['tempDirPath'] = $path;
51
52
        return $this;
53
    }
54
55
    public function getTempDirPath(): ?string
56
    {
57
        return $this->config['tempDirPath'] ?? null;
58
    }
59
60
    /**
61
     * The address of the API remote host. May optionally include the port, formatted
62
     * as "<uri>:<port>". Default 'vision.googleapis.com:443'.
63
     *
64
     * @param string $endpoint
65
     * @return $this
66
     * @see https://cloud.google.com/vision/docs/reference/rest
67
     */
68
    public function setApiEndpoint(string $endpoint): static
69
    {
70
        $this->config['apiEndpoint'] = $endpoint;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Determines whether retries defined by the client configuration should be disabled.
77
     * Default: `false`.
78
     *
79
     * @return $this
80
     */
81
    public function disableRetries(): static
82
    {
83
        return $this->set('disableRetries', true);
84
    }
85
86
    /**
87
     * Client method configuration, including retry settings. This option can be either
88
     * a path to a JSON file, or a PHP array containing the decoded JSON data.
89
     * By default, these settings points to the default client config file, which is
90
     * provided in the resources' folder.
91
     *
92
     * @param string|array $config
93
     * @return $this
94
     */
95
    public function setClientConfig(string|array $config): self
96
    {
97
        return $this->set('clientConfig', $config);
98
    }
99
100
    /**
101
     * The transport used for executing network requests. May be either the string
102
     * `rest` or `grpc`. Defaults to `grpc` if gRPC support is detected on the system.
103
     * *Advanced usage*: Additionally, it is possible to pass in an already
104
     * instantiated {@see \Google\ApiCore\Transport\TransportInterface} object. Note
105
     * that when this object is provided, any settings in $transportConfig, and any
106
     * $serviceAddress setting, will be ignored.
107
     */
108
    public function setTransport(string|TransportInterface $transport): static
109
    {
110
        return $this->set('transport', $transport);
111
    }
112
113
    public function getConnectorConfig(): array
114
    {
115
        $config = [
116
            'credentials' => $this->getOrFail('credentials'),
117
        ];
118
119
        if ($endPoint = $this->get('apiEndpoint')) {
120
            $config['apiEndpoint'] = $endPoint;
121
        }
122
123
        if ($credentialsConfig = $this->get('credentialsConfig')) {
124
            $config['credentialsConfig'] = $credentialsConfig;
125
        }
126
127
        if ($transport = $this->get('transport')) {
128
            $config['transport'] = $transport;
129
        }
130
131
        if ($clientConfig = $this->get('clientConfig')) {
132
            $config['clientConfig'] = $clientConfig;
133
        }
134
135
        if ($disableRetries = $this->get('disableRetries')) {
136
            $config['disableRetries'] = $disableRetries;
137
        }
138
139
        return $config;
140
    }
141
142
    public function get($key)
143
    {
144
        return $this->config[$key] ?? null;
145
    }
146
147
    protected function set($key, $val)
148
    {
149
        $this->config[$key] = $val;
150
151
        return $this;
152
    }
153
154
    public function getOrFail($key)
155
    {
156
        if (false === array_key_exists($key, $this->config)) {
157
            throw new ConfigException('Could not find the '.$key.' in config!');
158
        }
159
160
        return $this->config[$key];
161
    }
162
}
163