Completed
Push — master ( f0f7cf...5f3b38 )
by Morteza
11:42
created

ChabokChannel   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 48
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A send() 0 24 4
1
<?php
2
3
namespace NotificationChannels\Chabok;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Arr;
7
use NotificationChannels\Chabok\Exceptions\CouldNotSendNotification;
8
use Illuminate\Notifications\Notification;
9
use NotificationChannels\Chabok\Exceptions\InvalidConfiguration;
10
11
class ChabokChannel
12
{
13
    private static $API_ENDPOINT;
14
15
    /** @var Client */
16
    protected $client;
17
18
    /** @param Client $client */
19 3
    public function __construct(Client $client)
20
    {
21 3
        $this->client = $client;
22 3
        self::$API_ENDPOINT = 'https://' . config('services.chabok.app_id') . '.push.adpdigital.com/api/push/toUsers';
23 3
    }
24
25
    /**
26
     * Send the given notification.
27
     *
28
     * @param mixed $notifiable
29
     * @param \Illuminate\Notifications\Notification $notification
30
     *
31
     * @throws \NotificationChannels\Chabok\Exceptions\InvalidConfiguration
32
     * @throws \NotificationChannels\Chabok\Exceptions\CouldNotSendNotification
33
     */
34 3
    public function send($notifiable, Notification $notification)
35
    {
36 3
        $routing = collect($notifiable->routeNotificationFor('Chabok'));
37
38 3
        if ($routing->isEmpty()) {
39
            return;
40
        }
41
42 3
        $key = config('services.chabok.key');
43
44 3
        if (is_null($key)) {
45 1
            throw InvalidConfiguration::configurationNotSet();
46
        }
47
48 2
        $chabokParameters = $notification->toChabok($notifiable)->toArray();
0 ignored issues
show
Bug introduced by
The method toChabok() 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...
49
50 2
        $response = $this->client->post(self::$API_ENDPOINT.'?access_token='.$key, [
51 2
            'form_params' => Arr::set($chabokParameters, 'user', $routing->get('uuid')),
52
        ]);
53
54 2
        if ($response->getStatusCode() !== 200) {
55 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
56
        }
57 1
    }
58
}
59