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

Client::connectionTest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 1
crap 3
1
<?php
2
3
namespace CybozuHttp;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use GuzzleHttp\Subscriber\Log\Formatter;
7
use GuzzleHttp\Subscriber\Log\LogSubscriber;
8
use CybozuHttp\Subscriber\ErrorSubscriber;
9
use CybozuHttp\Exception\FailedAuthException;
10
use CybozuHttp\Exception\NotExistRequiredException;
11
12
/**
13
 * @author ochi51<[email protected]>
14
 */
15
class Client extends GuzzleClient
16
{
17
    /**
18
     * @var Config
19
     */
20
    protected $config;
21
22
    /**
23
     * Client constructor.
24
     * @param array $config
25
     */
26
    public function __construct($config = [])
27 6
    {
28
        $config = new Config($config);
29 6
        if (!$config->hasRequired()) {
30 6
            throw new NotExistRequiredException();
31 1
        }
32
33
        parent::__construct($config->toArray());
34 6
35 6
        $this->config = $config;
36 6
37 6
        $this->attachErrorSubscriber($config);
38
        $this->attachLogSubscriber($config);
39 6
    }
40 3
41 3
    /**
42
     * @param Config $config
43 6
     */
44
    protected function attachErrorSubscriber(Config $config)
45
    {
46 3
        $this->getEmitter()->attach(new ErrorSubscriber($config));
47
    }
48 3
49 3
    /**
50 3
     * @param Config $config
51 3
     */
52 3
    protected function attachLogSubscriber(Config $config)
53
    {
54
        if ($config->get('debug') && $config->get('logfile')) {
55
            $logSubscriber = new LogSubscriber(fopen($config->get('logfile'), 'a'), Formatter::DEBUG);
56
            $this->getEmitter()->attach($logSubscriber);
57 1
        }
58
    }
59 1
60
    /**
61
     * @return Config
62
     */
63
    public function getConfig()
64
    {
65 1
        return $this->config;
66
    }
67 1
68 1
    /**
69 1
     * @param array $config
70
     */
71 1
    public function changeAuthOptions(array $config)
72 1
    {
73 1
        $baseConfig = $this->config->toArray();
74 1
        unset($baseConfig['defaults']);
75 1
        $mergeConfig = $config + $baseConfig;
76
77 1
        $config = (new Config($mergeConfig));
78 1
        $options = $config->toArray()['defaults'];
79
        foreach ($options as $key => $option) {
80
            $this->setDefaultOption($key, $option);
81 1
        }
82 1
83
        $this->attachLogSubscriber($config);
84
85
        $this->config = $config;
86
    }
87
88 2
    /**
89
     * @param string $prefix
90 2
     * @throws FailedAuthException
91 1
     */
92 1
    public function connectionTest($prefix = '/')
93 1
    {
94
        $response = $this->get($prefix);
95 1
        $url = $response->getEffectiveUrl();
96
        if ($url && strpos($url, $this->getBaseUrl()) !== 0) {
97
            throw new FailedAuthException('Wronged auth information.');
98
        }
99
    }
100
}
101