Completed
Push — master ( 503797...227dd4 )
by Morteza
03:09
created

ChabokChannel::send()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 10
cts 12
cp 0.8333
rs 9.568
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.074
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 2
    public function __construct(Client $client)
20
    {
21 2
        $this->client = $client;
22 2
        self::$API_ENDPOINT = 'https://' . config('services.chabok.app_id') . '.push.adpdigital.com/api';
23 2
    }
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 2
    public function send($notifiable, Notification $notification)
35
    {
36 2
        if (! $routing = collect($notifiable->routeNotificationFor('Chabok'))) {
37
            return;
38
        }
39
40 2
        $key = config('services.chabok.key');
41
42 2
        if (is_null($key)) {
43 1
            throw InvalidConfiguration::configurationNotSet();
44
        }
45
46 1
        $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...
47
48 1
        $response = $this->client->post(self::$API_ENDPOINT.'?access_token='.$key, [
49 1
            'form_params' => Arr::set($chabokParameters, 'user', $routing->get('uuid')),
50
        ]);
51
52 1
        if ($response->getStatusCode() !== 200) {
53 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
54
        }
55
    }
56
}
57