Completed
Pull Request — master (#16)
by Yuichi
09:45 queued 07:37
created

Config::hasKeysByUse()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

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