Client   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 86%

Importance

Changes 0
Metric Value
wmc 22
lcom 2
cbo 3
dl 0
loc 189
ccs 43
cts 50
cp 0.86
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getWebhook() 0 4 1
A setWebhook() 0 6 1
A webhook() 0 4 1
A getMessageDefaults() 0 10 3
A setMessageDefaults() 0 6 1
A messageDefaults() 0 4 1
A createMessage() 0 4 1
B getJsonPayload() 0 12 7
A __call() 0 6 1
A sendMessage() 0 14 2
A getHttpClient() 0 8 2
1
<?php
2
3
namespace ElfSundae\BearyChat;
4
5
use JsonSerializable;
6
use GuzzleHttp\Client as HttpClient;
7
8
class Client
9
{
10
    /**
11
     * The BearyChat incoming webhook.
12
     *
13
     * @var string
14
     */
15
    protected $webhook;
16
17
    /**
18
     * The default fields for messages.
19
     *
20
     * @var array
21
     */
22
    protected $messageDefaults = [];
23
24
    /**
25
     * The Guzzle http client.
26
     *
27
     * @var \GuzzleHttp\Client
28
     */
29
    protected $httpClient;
30
31
    /**
32
     * Create a new Client.
33
     *
34
     * @param  string  $webhook
35
     * @param  array  $messageDefaults  See `\ElfSundae\BearyChat\MessageDefaults`
36
     * @param  \GuzzleHttp\Client  $httpClient
37
     */
38 9
    public function __construct($webhook = null, $messageDefaults = [], $httpClient = null)
39
    {
40 9
        $this->setWebhook($webhook);
41 9
        $this->setMessageDefaults($messageDefaults);
42 9
        $this->httpClient = $httpClient;
43 9
    }
44
45
    /**
46
     * Get the webhook.
47
     *
48
     * @return string
49
     */
50 2
    public function getWebhook()
51
    {
52 2
        return $this->webhook;
53
    }
54
55
    /**
56
     * Set the webhook.
57
     *
58
     * @param  string  $webhook
59
     * @return $this
60
     */
61 9
    public function setWebhook($webhook)
62
    {
63 9
        $this->webhook = $webhook;
64
65 9
        return $this;
66
    }
67
68
    /**
69
     * Change the webhook URL.
70
     *
71
     * @param  string  $webhook
72
     * @return $this
73
     */
74 1
    public function webhook($webhook)
75
    {
76 1
        return $this->setWebhook($webhook);
77
    }
78
79
    /**
80
     * Retrieve message defaults.
81
     *
82
     * @param  string|null  $key
83
     * @return mixed
84
     */
85 4
    public function getMessageDefaults($key = null)
86
    {
87 4
        if (is_null($key)) {
88 4
            return $this->messageDefaults;
89
        }
90
91 1
        if (isset($this->messageDefaults[$key])) {
92 1
            return $this->messageDefaults[$key];
93
        }
94 1
    }
95
96
    /**
97
     * Set the message defaults.
98
     *
99
     * @param  array  $defaults
100
     * @return $this
101
     */
102 9
    public function setMessageDefaults($defaults)
103
    {
104 9
        $this->messageDefaults = (array) $defaults;
105
106 9
        return $this;
107
    }
108
109
    /**
110
     * Set the message defaults.
111
     *
112
     * @param  array  $defaults
113
     * @return $this
114
     */
115
    public function messageDefaults($defaults)
116
    {
117
        return $this->setMessageDefaults($defaults);
118
    }
119
120
    /**
121
     * Create a new Message instance.
122
     *
123
     * @return \ElfSundae\BearyChat\Message
124
     */
125 2
    public function createMessage()
126
    {
127 2
        return new Message($this);
128
    }
129
130
    /**
131
     * Send message to the BearyChat.
132
     *
133
     * @param  mixed $message  A JSON string, or any arrayable object.
134
     * @return bool
135
     */
136 2
    public function sendMessage($message)
137
    {
138 2
        if ($payload = $this->getJsonPayload($message)) {
139 1
            $response = $this->getHttpClient()
140 1
                ->post($this->getWebhook(), [
141 1
                    'headers' => ['Content-Type' => 'application/json'],
142 1
                    'body' => $payload,
143
                ]);
144
145
            return 200 === $response->getStatusCode();
146
        }
147
148 1
        return false;
149
    }
150
151
    /**
152
     * Get the JSON payload from an object.
153
     *
154
     * @param  mixed  $message
155
     * @return string
156
     */
157 2
    protected function getJsonPayload($message)
158
    {
159 2
        if (is_array($message) || $message instanceof JsonSerializable) {
160
            return json_encode($message);
161 2
        } elseif (method_exists($message, 'toJson')) {
162
            return $message->toJson();
163 2
        } elseif (method_exists($message, 'toArray')) {
164
            return json_encode($message->toArray());
165 2
        } elseif (is_string($message) && is_array(json_decode($message, true))) {
166 1
            return $message;
167
        }
168 1
    }
169
170
    /**
171
     * Get the http client.
172
     * @return \GuzzleHttp\Client
173
     */
174 2
    public function getHttpClient()
175
    {
176 2
        if (! $this->httpClient instanceof HttpClient) {
177
            $this->httpClient = new HttpClient;
178
        }
179
180 2
        return $this->httpClient;
181
    }
182
183
    /**
184
     * Any unhandled methods will be sent to a new Message instance.
185
     *
186
     * @param  string  $name
187
     * @param  array  $args
188
     * @return mixed
189
     */
190 1
    public function __call($name, $args)
191
    {
192 1
        $message = $this->createMessage();
193
194 1
        return call_user_func_array([$message, $name], $args);
195
    }
196
}
197