Completed
Push — cache ( 3de9ec )
by Yuichi
08:59
created

Client::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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
    {
28
        $config = new Config($config);
29
        if (!$config->hasRequired()) {
30
            throw new NotExistRequiredException();
31
        }
32
33
        parent::__construct($config->toArray());
34
35
        $this->config = $config;
36
37
        $this->attachErrorSubscriber($config);
38
        $this->attachLogSubscriber($config);
39
    }
40
41
    /**
42
     * @param Config $config
43
     */
44
    protected function attachErrorSubscriber(Config $config)
45
    {
46
        $this->getEmitter()->attach(new ErrorSubscriber($config));
47
    }
48
49
    /**
50
     * @param Config $config
51
     */
52
    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
        }
58
    }
59
60
    /**
61
     * @return Config
62
     */
63
    public function getConfig()
64
    {
65
        return $this->config;
66
    }
67
68
    /**
69
     * @param array $config
70
     */
71
    public function changeAuthOptions(array $config)
72
    {
73
        $baseConfig = $this->config->toArray();
74
        unset($baseConfig['defaults']);
75
        $mergeConfig = $config + $baseConfig;
76
77
        $config = (new Config($mergeConfig));
78
        $options = $config->toArray()['defaults'];
79
        foreach ($options as $key => $option) {
80
            $this->setDefaultOption($key, $option);
81
        }
82
83
        $this->attachLogSubscriber($config);
84
85
        $this->config = $config;
86
    }
87
88
    /**
89
     * @param string $prefix
90
     * @throws FailedAuthException
91
     */
92
    public function connectionTest($prefix = '/')
93
    {
94
        $response = $this->get($prefix);
95
        $url = $response->getEffectiveUrl();
96
        if ($url && strpos($url, $this->getBaseUrl()) !== 0) {
97
            throw new FailedAuthException('Wronged auth information.');
98
        }
99
    }
100
}
101