ExponentChannel   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 24 5
A sendExponent() 0 18 3
1
<?php
2
3
namespace NotificationChannels\Exponent;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\RequestException;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\Exponent\Exceptions\CouldNotSendNotification;
9
10
/**
11
 * Class ExponentChannel.
12
 */
13
class ExponentChannel
14
{
15
    const DEFAULT_API_URL = 'https://exp.host';
16
    const MAX_TOKEN_LENGTH = 100;
17
18
    protected $client;
19
20
    /**
21
     * ExponentChannel constructor.
22
     * @param Client $client
23
     */
24
    public function __construct(Client $client)
25
    {
26
        $this->client = $client;
27
    }
28
29
    /**
30
     * Send the given notification.
31
     *
32
     * @param mixed $notifiable
33
     * @param \Illuminate\Notifications\Notification $notification
34
     *
35
     * @throws \NotificationChannels\Exponent\Exceptions\CouldNotSendNotification
36
     */
37
    public function send($notifiable, Notification $notification)
38
    {
39
        if (! $notifiable->routeNotificationFor('exponent')) {
40
            return;
41
        }
42
        $tokens = (array) $notifiable->routeNotificationFor('exponent');
43
44
        if (empty($tokens)) {
45
            return;
46
        }
47
48
        /** @var ExponentMessage|null $exponentMessage */
49
        $exponentMessage = $notification->toExponent($notifiable);
0 ignored issues
show
Bug introduced by
The method toExponent() 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
        if (empty($exponentMessage)) {
52
            return;
53
        }
54
55
        $partialTokens = array_chunk($tokens, self::MAX_TOKEN_LENGTH, false);
56
        foreach ($partialTokens as $partialToken) {
57
            $exponentMessage->setTokens($partialToken);
58
            $this->sendExponent($exponentMessage);
59
        }
60
    }
61
62
    /**
63
     * @param ExponentMessage $exponentMessage
64
     */
65
    protected function sendExponent(ExponentMessage $exponentMessage)
66
    {
67
        $request = [];
68
        foreach ($exponentMessage->getTokens() as $token) {
69
            $request[] = $exponentMessage->toArray() + ['to' => $token];
70
        }
71
72
        try {
73
            $this->client->post('/--/api/v2/push/send', [
74
                'body' => json_encode($request),
75
                'headers' => [
76
                    'Content-Type' => 'application/json',
77
                ],
78
            ]);
79
        } catch (RequestException $e) {
80
            throw CouldNotSendNotification::serviceRespondedWithAnError($e);
81
        }
82
    }
83
}
84