Completed
Push — master ( 084ccd...194b4a )
by Elf
04:09
created

ClientManager::getWebhookForClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 9.4285
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
     * Get the clients config.
113
     *
114
     * @return array
115
     */
116
    public function getClientsConfig()
117
    {
118
        return $this->clientsConfig;
119
    }
120
121
    /**
122
     * Set the clients config.
123
     *
124
     * @param  array  $config
125
     * @return $this
126
     */
127 5
    public function setClientsConfig($config)
128
    {
129 5
        if (is_array($config)) {
130 5
            $this->clientsConfig = $config;
131
        }
132
133 5
        return $this;
134
    }
135
136
    /**
137
     * Get a client instance.
138
     *
139
     * @param  string|null  $name
140
     * @return \ElfSundae\BearyChat\Client
141
     */
142 3
    public function client($name = null)
143
    {
144 3
        if (is_null($name)) {
145 2
            $name = $this->getDefaultName();
146
        }
147
148 3
        return $this->clients[$name] = $this->get($name);
149
    }
150
151
    /**
152
     * Attempt to get the client.
153
     *
154
     * @param  string  $name
155
     * @return \ElfSundae\BearyChat\Client
156
     */
157 3
    protected function get($name)
158
    {
159 3
        return isset($this->clients[$name]) ? $this->clients[$name] : $this->resolve($name);
160
    }
161
162
    /**
163
     * Resolve the given client.
164
     *
165
     * @param  string  $name
166
     * @return \ElfSundae\BearyChat\Client
167
     */
168 3
    protected function resolve($name)
169
    {
170 3
        return new Client(
171 3
            $this->getWebhookForClient($name),
172 3
            $this->getMessageDefaultsForClient($name),
173 3
            $this->getHttpClientForClient($name)
174
        );
175
    }
176
177
    /**
178
     * Get the webhook for the given client.
179
     *
180
     * @param  string  $name
181
     * @return string
182
     */
183 3
    public function getWebhookForClient($name)
184
    {
185 3
        return Arr::get($this->clientsConfig[$name], 'webhook') ?:
186 3
            Arr::get($this->clientsDefaults, 'webhook');
187
    }
188
189
    /**
190
     * Get the message defaults for the given client.
191
     *
192
     * @param  string  $name
193
     * @return array
194
     */
195 3
    public function getMessageDefaultsForClient($name)
196
    {
197 3
        return array_merge(
198 3
            Arr::get($this->clientsDefaults, 'message_defaults', []),
199 3
            Arr::get($this->clientsConfig[$name], 'message_defaults', [])
200
        );
201
    }
202
203
    /**
204
     * Get the HTTP client for the given client.
205
     *
206
     * @param  string  $name
207
     * @return \GuzzleHttp\Client|null
208
     */
209 3
    protected function getHttpClientForClient($name)
210
    {
211 3
        if ($creator = $this->httpClientCreator) {
212 1
            return $creator($name);
213
        }
214 2
    }
215
216
    /**
217
     * Register a custom HTTP client creator Closure.
218
     *
219
     * @param  \Closure  $creator
220
     * @return $this
221
     */
222 1
    public function customHttpClient(Closure $creator)
223
    {
224 1
        $this->httpClientCreator = $creator;
225
226 1
        return $this;
227
    }
228
229
    /**
230
     * Dynamically call the default client instance.
231
     *
232
     * @param  string  $method
233
     * @param  array   $parameters
234
     * @return mixed
235
     */
236 1
    public function __call($method, $parameters)
237
    {
238 1
        return call_user_func_array([$this->client(), $method], $parameters);
239
    }
240
}
241