Completed
Push — master ( 039bdb...90d49c )
by Yuichi
11s
created

Config::getBasicAuthOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
namespace CybozuHttp;
4
5
use CybozuHttp\Exception\NotExistRequiredException;
6
use CybozuHttp\Middleware\FinishMiddleware;
7
use GuzzleHttp\HandlerStack;
8
9
10
/**
11
 * @author ochi51 <[email protected]>
12
 */
13
class Config
14
{
15
16
    /**
17
     * @var array $config
18
     */
19
    private $config = [];
20
21
    /**
22
     * @var array $default
23
     */
24
    private $default = [
25
        'domain' => "cybozu.com",
26
        'use_api_token' => false,
27
        'use_basic' => false,
28
        'use_client_cert' => false,
29
        'base_uri' => null,
30
        'debug' => false
31
    ];
32
33
    /**
34
     * @var array $required
35
     */
36
    private $required = [
37
        'handler',
38
        'domain',
39
        'subdomain',
40
        'use_api_token',
41
        'use_basic',
42
        'use_client_cert',
43
        'base_uri',
44
        'debug'
45
    ];
46
47 12
    public function __construct(array $config)
48
    {
49 12
        $this->config = $config + $this->default;
50
51 12
        $this->config['base_uri'] = $this->getBaseUri();
52 12
        $this->config['handler'] = $handler =  HandlerStack::create();
53
54 12
        $this->configureAuth();
55 12
        $this->configureBasicAuth();
56 12
        $this->configureCert();
57
58 12
        $handler->before('http_errors', new FinishMiddleware(), 'cybozu_http.finish');
59 12
    }
60
61 12
    private function configureAuth()
62
    {
63 12
        if ($this->get('use_api_token')) {
64 2
            $this->config['headers']['X-Cybozu-API-Token'] = $this->get('token');
65 2
        } else {
66 12
            $this->config['headers']['X-Cybozu-Authorization'] =
67 12
                base64_encode($this->get('login') . ':' . $this->get('password'));
68
        }
69 12
    }
70
71 12
    private function configureBasicAuth()
72
    {
73 12
        if ($this->get('use_basic')) {
74 6
            $this->config['auth'] = $this->getBasicAuthOptions();
75 6
        }
76 12
    }
77
78 12
    private function configureCert()
79
    {
80 12
        if ($this->get('use_client_cert')) {
81 4
            $this->config['verify'] = true;
82 4
            $this->config['cert'] = $this->getCertOptions();
83 4
        } else {
84 9
            $this->config['verify'] = false;
85
        }
86 12
    }
87
88
    /**
89
     * @return array
90
     */
91 6
    private function getBasicAuthOptions()
92
    {
93 6
        if ($this->hasRequiredOnBasicAuth()) {
94
            return [
95 6
                $this->get('basic_login'),
96 6
                $this->get('basic_password')
97 6
            ];
98
        }
99 1
        throw new NotExistRequiredException("kintone.empty_basic_password");
100
    }
101
102
    /**
103
     * @return array
104
     */
105 4
    private function getCertOptions()
106
    {
107 4
        if ($this->hasRequiredOnCert()) {
108
            return [
109 4
                $this->get('cert_file'),
110 4
                $this->get('cert_password')
111 4
            ];
112
        }
113 1
        throw new NotExistRequiredException("kintone.empty_cert");
114
    }
115
116
    /**
117
     * @return array
118
     */
119 5
    public function toGuzzleConfig()
120
    {
121
        $config = [
122 5
            'handler' => $this->get('handler'),
123 5
            'base_uri' => $this->get('base_uri'),
124 5
            'headers' => $this->get('headers'),
125 5
            'debug' => $this->get('debug') ? fopen($this->get('logfile'), 'a') : false
126 5
        ];
127 5
        if ($this->get('auth')) {
128 4
            $config['auth'] = $this->get('auth');
129 4
        }
130 5
        $config['verify'] = $this->get('verify');
131 5
        if ($this->get('cert')) {
132 1
            $config['cert'] = $this->get('cert');
133 1
        }
134
135 5
        return $config;
136
    }
137
138
    /**
139
     * @param $key
140
     * @return string|bool
141
     */
142 12
    public function get($key)
143
    {
144 12
        if (isset($this->config[$key])) {
145 12
            return $this->config[$key];
146
        }
147
148 8
        return false;
149
    }
150
151
    /**
152
     * @return array
153
     */
154 1
    public function getConfig()
155
    {
156 1
        return $this->config;
157
    }
158
159
    /**
160
     * @return bool
161
     */
162 5
    public function hasRequired()
163
    {
164 5
        foreach ($this->required as $r) {
165 5
            if (!array_key_exists($r, $this->config)) {
166 1
                return false;
167
            }
168 5
        }
169
170 5
        return $this->hasRequiredOnAuth()
171 5
                && $this->hasRequiredOnBasicAuth()
172 5
                && $this->hasRequiredOnCert();
173
    }
174
175
    /**
176
     * @return bool
177
     */
178 5
    private function hasRequiredOnAuth()
179
    {
180 5
        if ($this->get('use_api_token')) {
181 1
            return !empty($this->get('token'));
182
        }
183
184 5
        return $this->get('login') && $this->get('password');
185
    }
186
187
    /**
188
     * @return bool
189
     */
190 8
    private function hasRequiredOnBasicAuth()
191
    {
192 8
        return $this->hasKeysByUse('use_basic', ['basic_login', 'basic_password']);
193
    }
194
195
    /**
196
     * @return bool
197
     */
198 9
    private function hasRequiredOnCert()
199
    {
200 9
        return $this->hasKeysByUse('use_client_cert', ['cert_file', 'cert_password']);
201
    }
202
203
    /**
204
     * @param string $use
205
     * @param string[] $keys
206
     * @return bool
207
     */
208 10
    private function hasKeysByUse($use, array $keys)
209
    {
210 10
        if (!$this->get($use)) {
211 5
            return true;
212
        }
213
214 8
        foreach ($keys as $key) {
215 8
            if (is_null($this->get($key)) || $this->get($key) === false) {
216 2
                return false;
217
            }
218 8
        }
219
220 8
        return true;
221
    }
222
223
    /**
224
     * @return string
225
     */
226 12
    public function getBaseUri()
227
    {
228 12
        $subdomain = $this->get('subdomain');
229 12
        $uri = "https://" . $subdomain;
230
231 12
        if (strpos($subdomain, '.') === false) {
232 12
            if ($this->get('use_client_cert')) {
233 4
                $uri .= ".s";
234 4
            }
235
236 12
            $uri .= "." . $this->get('domain');
237 12
        }
238
239 12
        return $uri;
240
    }
241
}