KavenegarChannel::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace NotificationChannels\Kavenegar;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Arr;
7
use NotificationChannels\Kavenegar\Exceptions\CouldNotSendNotification;
8
use Illuminate\Notifications\Notification;
9
use NotificationChannels\Kavenegar\Exceptions\InvalidConfiguration;
10
11 View Code Duplication
class KavenegarChannel
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    private static $API_ENDPOINT = 'https://api.kavenegar.com/v1/%s/sms/send.json';
14
15
    /** @var Client */
16
    protected $client;
17
18
    /**
19
     * @param Client $client
20
     */
21
    public function __construct(Client $client)
22
    {
23
        $this->client = $client;
24
    }
25
26
    /**
27
     * Send the given notification.
28
     *
29
     * @param mixed $notifiable
30
     * @param \Illuminate\Notifications\Notification $notification
31
     *
32
     * @throws \NotificationChannels\Kavenegar\Exceptions\InvalidConfiguration
33
     * @throws \NotificationChannels\Kavenegar\Exceptions\CouldNotSendNotification
34
     */
35
    public function send($notifiable, Notification $notification)
36
    {
37
        if (! $routing = collect($notifiable->routeNotificationFor('Kavenegar'))) {
38
            return;
39
        }
40
41
        $key = config('services.kavenegar.api_key');
42
43
        if (is_null($key)) {
44
            throw InvalidConfiguration::configurationNotSet();
45
        }
46
47
        $kavenegarParameters = $notification->toKavenegar($notifiable)->toArray();
0 ignored issues
show
Bug introduced by
The method toKavenegar() 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...
48
49
        $response = $this->client->post(sprintf(self::$API_ENDPOINT, $key), [
50
            'form_params' => Arr::set($kavenegarParameters, 'receptor', $routing->get('mobile_number')),
51
        ]);
52
53
        if ($response->getStatusCode() !== 200) {
54
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
55
        }
56
    }
57
}
58