Completed
Pull Request — master (#2)
by Morteza
08:21
created

ChabokChannel::send()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0072

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 13
cp 0.9231
rs 9.536
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4.0072
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
        $additionalConfiguration = config('services.chabok.additional') ?? [];
44 3
45 1
        if (is_null($key)) {
46
            throw InvalidConfiguration::configurationNotSet();
47
        }
48 2
49
        $chabokParameters = $notification->toChabok($notifiable)->toArray();
50 2
        $params = array_merge(
51 2
            ['form_params' => Arr::set($chabokParameters, 'user', $routing->get('uuid'))],
52
            $additionalConfiguration,
53
        );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
54 2
        $response = $this->client->post(self::$API_ENDPOINT.'?access_token='.$key, $params);
55 1
56
        if ($response->getStatusCode() !== 200) {
57 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
58
        }
59
    }
60
}
61