Completed
Push — master ( 587a68...c3946b )
by Elf
02:51
created

ClientManager::getConfigForClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\BearyChat\Laravel;
4
5
use Closure;
6
use Illuminate\Support\Arr;
7
use ElfSundae\BearyChat\Client;
8
9
class ClientManager
10
{
11
    /**
12
     * The application instance.
13
     *
14
     * @var mixed
15
     */
16
    protected $app;
17
18
    /**
19
     * The default client name.
20
     *
21
     * @var string
22
     */
23
    protected $defaultName;
24
25
    /**
26
     * The defaults for all clients.
27
     *
28
     * @var array
29
     */
30
    protected $clientsDefaults = [];
31
32
    /**
33
     * The clients config.
34
     *
35
     * @var array
36
     */
37
    protected $clientsConfig = [];
38
39
    /**
40
     * The array of resolved BearyChat clients.
41
     *
42
     * @var array
43
     */
44
    protected $clients = [];
45
46
    /**
47
     * The registered custom HTTP client creator.
48
     *
49
     * @var \Closure
50
     */
51
    protected $httpClientCreator;
52
53
    /**
54
     * Create a new client manager instance.
55
     *
56
     * @param  mixed  $app
57
     */
58 5
    public function __construct($app)
59
    {
60 5
        $this->app = $app;
61 5
    }
62
63
    /**
64
     * Get the default client name.
65
     *
66
     * @return string
67
     */
68 3
    public function getDefaultName()
69
    {
70 3
        return $this->defaultName ?: Arr::first(array_keys($this->clientsConfig));
71
    }
72
73
    /**
74
     * Set the default client name.
75
     *
76
     * @param  string  $name
77
     * @return $this
78
     */
79 5
    public function setDefaultName($name)
80
    {
81 5
        $this->defaultName = $name;
82
83 5
        return $this;
84
    }
85
86
    /**
87
     * Get the clients defaults.
88
     *
89
     * @return array
90
     */
91 1
    public function getClientsDefaults()
92
    {
93 1
        return $this->clientsDefaults;
94
    }
95
96
    /**
97
     * Set the clients defaults.
98
     *
99
     * @param  array  $defaults
100
     * @return $this
101
     */
102 5
    public function setClientsDefaults($defaults)
103
    {
104 5
        if (is_array($defaults)) {
105 5
            $this->clientsDefaults = $defaults;
106
        }
107
108 5
        return $this;
109
    }
110
111
    /**
112
     * Set the clients config.
113
     *
114
     * @param  array  $config
115
     * @return $this
116
     */
117 5
    public function setClientsConfig($config)
118
    {
119 5
        if (is_array($config)) {
120 5
            $this->clientsConfig = $config;
121
        }
122
123 5
        return $this;
124
    }
125
126
    /**
127
     * Get a client instance.
128
     *
129
     * @param  string|null  $name
130
     * @return \ElfSundae\BearyChat\Client
131
     */
132 3
    public function client($name = null)
133
    {
134 3
        if (is_null($name)) {
135 2
            $name = $this->getDefaultName();
136
        }
137
138 3
        return $this->clients[$name] = $this->get($name);
139
    }
140
141
    /**
142
     * Attempt to get the client.
143
     *
144
     * @param  string  $name
145
     * @return \ElfSundae\BearyChat\Client
146
     */
147 3
    protected function get($name)
148
    {
149 3
        return isset($this->clients[$name]) ? $this->clients[$name] : $this->resolve($name);
150
    }
151
152
    /**
153
     * Resolve the given client.
154
     *
155
     * @param  string  $name
156
     * @return \ElfSundae\BearyChat\Client
157
     */
158 3
    protected function resolve($name)
159
    {
160 3
        $config = $this->getConfigForClient($name);
161
162 3
        return new Client(
163 3
            $config['webhook'],
164 3
            $config['message_defaults'],
165 3
            $this->getHttpClient($name)
166
        );
167
    }
168
169
    /**
170
     * Get client config for the given client name.
171
     *
172
     * @param  string  $name
173
     * @return array
174
     */
175 3
    protected function getConfigForClient($name)
176
    {
177 3
        $config = $this->clientsConfig[$name];
178
179 3
        if (empty($config['webhook'])) {
180 2
            $config['webhook'] = Arr::get($this->clientsDefaults, 'webhook');
181
        }
182
183 3
        $config['message_defaults'] = array_merge(
184 3
            Arr::get($this->clientsDefaults, 'message_defaults', []),
185 3
            Arr::get($config, 'message_defaults', [])
186
        );
187
188 3
        return $config;
189
    }
190
191
    /**
192
     * Get the HTTP client.
193
     *
194
     * @return \GuzzleHttp\Client|null
195
     */
196 3
    protected function getHttpClient($name)
197
    {
198 3
        if ($creator = $this->httpClientCreator) {
199 1
            return $creator($name);
200
        }
201 2
    }
202
203
    /**
204
     * Register a custom HTTP client creator Closure.
205
     *
206
     * @param  \Closure  $creator
207
     * @return $this
208
     */
209 1
    public function customHttpClient(Closure $creator)
210
    {
211 1
        $this->httpClientCreator = $creator;
212
213 1
        return $this;
214
    }
215
216
    /**
217
     * Dynamically call the default client instance.
218
     *
219
     * @param  string  $method
220
     * @param  array   $parameters
221
     * @return mixed
222
     */
223 1
    public function __call($method, $parameters)
224
    {
225 1
        return call_user_func_array([$this->client(), $method], $parameters);
226
    }
227
}
228