Issues (10)

src/ClientManager.php (2 issues)

Severity
1
<?php
2
3
namespace ElfSundae\BearyChat\Laravel;
4
5
use Closure;
6
use ElfSundae\BearyChat\Client;
7
use Illuminate\Support\Arr;
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 16
    public function getDefaultName()
52
    {
53 16
        return $this->defaultName ?: $this->getFirstClientName();
54
    }
55
56
    /**
57
     * Get the first client name.
58
     *
59
     * @return string
60
     */
61 16
    protected function getFirstClientName()
62
    {
63 16
        reset($this->clientsConfig);
64
65 16
        return key($this->clientsConfig);
66
    }
67
68
    /**
69
     * Set the default client name.
70
     *
71
     * @param  string  $name
72
     * @return $this
73
     */
74 4
    public function setDefaultName($name)
75
    {
76 4
        $this->defaultName = $name;
77
78 4
        return $this;
79
    }
80
81
    /**
82
     * Get the clients defaults.
83
     *
84
     * @return array
85
     */
86 4
    public function getClientsDefaults()
87
    {
88 4
        return $this->clientsDefaults;
89
    }
90
91
    /**
92
     * Set the clients defaults.
93
     *
94
     * @param  array  $defaults
95
     * @return $this
96
     */
97 8
    public function setClientsDefaults($defaults)
98
    {
99 8
        if (is_array($defaults)) {
0 ignored issues
show
The condition is_array($defaults) is always true.
Loading history...
100 8
            $this->clientsDefaults = $defaults;
101
        }
102
103 8
        return $this;
104
    }
105
106
    /**
107
     * Get the clients config.
108
     *
109
     * @return array
110
     */
111 4
    public function getClientsConfig()
112
    {
113 4
        return $this->clientsConfig;
114
    }
115
116
    /**
117
     * Set the clients config.
118
     *
119
     * @param  array  $config
120
     * @return $this
121
     */
122 16
    public function setClientsConfig($config)
123
    {
124 16
        if (is_array($config)) {
0 ignored issues
show
The condition is_array($config) is always true.
Loading history...
125 16
            $this->clientsConfig = $config;
126
        }
127
128 16
        return $this;
129
    }
130
131
    /**
132
     * Get a client instance.
133
     *
134
     * @param  string|null  $name
135
     * @return \ElfSundae\BearyChat\Client
136
     */
137 12
    public function client($name = null)
138
    {
139 12
        if (is_null($name)) {
140 12
            $name = $this->getDefaultName();
141
        }
142
143 12
        if (! isset($this->clients[$name])) {
144 12
            $this->clients[$name] = new Client(
145 12
                $this->getWebhookForClient($name),
146 12
                $this->getMessageDefaultsForClient($name),
147 12
                $this->getHttpClientForClient($name)
148
            );
149
        }
150
151 12
        return $this->clients[$name];
152
    }
153
154
    /**
155
     * Get the webhook for the given client.
156
     *
157
     * @param  string  $name
158
     * @return string
159
     */
160 12
    public function getWebhookForClient($name)
161
    {
162 12
        return Arr::get($this->clientsConfig[$name], 'webhook') ?:
163 12
            Arr::get($this->clientsDefaults, 'webhook');
164
    }
165
166
    /**
167
     * Get the message defaults for the given client.
168
     *
169
     * @param  string  $name
170
     * @return array
171
     */
172 12
    public function getMessageDefaultsForClient($name)
173
    {
174 12
        return array_merge(
175 12
            Arr::get($this->clientsDefaults, 'message_defaults', []),
176 12
            Arr::get($this->clientsConfig[$name], 'message_defaults', [])
177
        );
178
    }
179
180
    /**
181
     * Get the HTTP client for the given client.
182
     *
183
     * @param  string  $name
184
     * @return \GuzzleHttp\Client|null
185
     */
186 12
    protected function getHttpClientForClient($name)
187
    {
188 12
        if ($creator = $this->httpClientCreator) {
189 4
            return $creator($name);
190
        }
191 8
    }
192
193
    /**
194
     * Register a custom HTTP client creator Closure.
195
     *
196
     * @param  \Closure  $creator
197
     * @return $this
198
     */
199 4
    public function customHttpClient(Closure $creator)
200
    {
201 4
        $this->httpClientCreator = $creator;
202
203 4
        return $this;
204
    }
205
206
    /**
207
     * Dynamically call the default client instance.
208
     *
209
     * @param  string  $method
210
     * @param  array   $parameters
211
     * @return mixed
212
     */
213 4
    public function __call($method, $parameters)
214
    {
215 4
        return call_user_func_array([$this->client(), $method], $parameters);
216
    }
217
}
218