GlobeLabsSmsChannel   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B send() 0 32 7
A generateExceptionMessage() 0 15 3
1
<?php
2
3
namespace Coreproc\GlobeLabsSms;
4
5
use Coreproc\GlobeLabsSms\Events\GlobeLabsSmsSent;
6
use Coreproc\GlobeLabsSms\Exceptions\CouldNotSendNotification;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Exception\ClientException;
9
use GuzzleHttp\Exception\ConnectException;
10
use GuzzleHttp\Exception\GuzzleException;
11
use GuzzleHttp\Exception\ServerException;
12
use Illuminate\Notifications\Notification;
13
use Lang;
14
15
class GlobeLabsSmsChannel
16
{
17
    /**
18
     * @var Client
19
     */
20
    protected $client;
21
22
    public function __construct(Client $client)
23
    {
24
        $this->client = $client;
25
    }
26
27
    /**
28
     * Send the given notification.
29
     *
30
     * @param mixed $notifiable
31
     * @param \Illuminate\Notifications\Notification $notification
32
     *
33
     * @throws CouldNotSendNotification
34
     */
35
    public function send($notifiable, Notification $notification)
36
    {
37
        if (empty($notifiable->routeNotificationFor('globeLabsSms'))) {
38
            throw new CouldNotSendNotification('Missing method in your notifiable: routeNotificationForGlobeLabs().');
39
        }
40
41
        $contactInfo = $notifiable->routeNotificationFor('globeLabsSms');
42
43
        // The contact info should include an address (mobile number). We are making access_token optional. The API
44
        // response will catch that anyway.
45
        if (empty($contactInfo['address'])) {
46
            throw new CouldNotSendNotification('Missing address variable from your routeNotificationForGlobeLabs().');
47
        }
48
49
        $message = $notification->toGlobeLabsSms($notifiable);
0 ignored issues
show
Bug introduced by
The method toGlobeLabsSms() does not seem to exist on object<Illuminate\Notifications\Notification>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
51
        try {
52
            $response = $this->client->request('POST', $message->getApiSendUrl(), [
53
                'body' => $message->toJson(),
54
            ]);
55
56
            event(new GlobeLabsSmsSent($response));
57
        } catch (ConnectException $exception) {
58
            throw $this->generateExceptionMessage($exception, 'connect_exception');
59
        } catch (ClientException $exception) {
60
            throw $this->generateExceptionMessage($exception, 'client_exception');
61
        } catch (ServerException $exception) {
62
            throw $this->generateExceptionMessage($exception, 'server_exception');
63
        } catch (GuzzleException $exception) {
64
            throw $this->generateExceptionMessage($exception, 'guzzle_exception');
65
        }
66
    }
67
68
    private function generateExceptionMessage($exception, $langString)
69
    {
70
        $message = $exception->getMessage();
71
72
        if (! empty($exception->getResponse())) {
73
            $response = json_decode($exception->getResponse()->getBody()->getContents());
74
            $message = $response->error ?? $exception->getMessage();
75
        }
76
77
        if (Lang::has('globe_labs_sms::errors.' . $langString)) {
78
            $message = __('globe_labs_sms::errors.' . $langString);
79
        }
80
81
        return new CouldNotSendNotification($message);
82
    }
83
}
84