Completed
Push — master ( a6ed1e...b9942a )
by Elf
02:29
created

ClientManager::getWebhookForClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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