HttpAdapterManager::getGlobalConfig()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 18
rs 9.4285
cc 2
eloc 12
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of Laravel HTTP adapter.
5
 *
6
 * (c) Hidde Beydals <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace HiddeCo\HttpAdapter;
13
14
use GrahamCampbell\Manager\AbstractManager;
15
use Illuminate\Contracts\Config\Repository;
16
17
class HttpAdapterManager extends AbstractManager
18
{
19
    /**
20
     * The factory instance.
21
     *
22
     * @var \HiddeCo\HttpAdapter\HttpAdapterFactory
23
     */
24
    protected $factory;
25
26
    /**
27
     * Create a new HTTP adapter manager instance.
28
     *
29
     * @param \Illuminate\Contracts\Config\Repository
30
     * @param \HiddeCo\HttpAdapter\HttpAdapterFactory
31
     */
32
    public function __construct(Repository $config, HttpAdapterFactory $factory)
33
    {
34
        $this->config = $config;
35
        $this->factory = $factory;
36
    }
37
38
    /**
39
     * Create the connection instance.
40
     *
41
     * @param array $config
42
     *
43
     * @return \Ivory\HttpAdapter\HttpAdapterInterface
44
     */
45
    protected function createConnection(array $config)
46
    {
47
        return $this->factory->make($config);
48
    }
49
50
    /**
51
     * Get the configuration name.
52
     *
53
     * @return string
54
     */
55
    protected function getConfigName()
56
    {
57
        return 'httpadapter';
58
    }
59
60
    /**
61
     * Get the configuration for a connection.
62
     *
63
     * @param string $name
64
     *
65
     * @throws \InvalidArgumentException
66
     *
67
     * @return string
68
     */
69
    public function getConnectionConfig($name)
70
    {
71
        $name = $name ?: $this->getDefaultConnection();
72
73
        $connections = $this->config->get($this->getConfigName().'.connections');
74
75
        if (!is_array($config = array_get($connections, $name)) && !$config) {
76
            throw new \InvalidArgumentException(sprintf('Adapter [%s] is not configured', $name));
77
        }
78
79
        $config = array_merge($config, $this->getGlobalConfig());
80
81
        $config['name'] = $name;
82
83
        return $config;
84
    }
85
86
    /**
87
     * Get the global configuration.
88
     *
89
     * @return array
90
     */
91
    public function getGlobalConfig()
92
    {
93
        $global = array_only($this->config->get($this->getConfigName().'.global'), ['eventable', 'config']);
94
95
        if (array_key_exists('config', $global)) {
96
            $global['config'] = array_only($global['config'], [
97
                'protocol_version',
98
                'keep_alive',
99
                'encoding_type',
100
                'boundary',
101
                'timeout',
102
                'user_agent',
103
                'base_uri',
104
            ]);
105
        }
106
107
        return $global;
108
    }
109
110
    /**
111
     * Get the factory instance.
112
     *
113
     * @return \HiddeCo\HttpAdapter\HttpAdapterFactory
114
     */
115
    public function getFactory()
116
    {
117
        return $this->factory;
118
    }
119
}
120