Completed
Pull Request — master (#3)
by Abdullah
02:40 queued 25s
created

ZendeskChannel::send()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5.0113

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 13
cp 0.9231
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 2
crap 5.0113
1
<?php
2
3
namespace NotificationChannels\Zendesk;
4
5
use Zendesk\API\Client;
6
use Illuminate\Support\Arr;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\Zendesk\Exceptions\CouldNotSendNotification;
9
10
class ZendeskChannel
11
{
12
    /** @var Client */
13
    protected $client;
14
15
    /** @param Client $client */
16 3
    public function __construct(Client $client)
17
    {
18 3
        $this->client = $client;
19 3
    }
20
21
    /**
22
     * Send the given notification.
23
     *
24
     * @param mixed $notifiable
25
     * @param \Illuminate\Notifications\Notification $notification
26
     *
27
     * @throws \NotificationChannels\Zendesk\Exceptions\InvalidConfiguration
28
     * @throws \NotificationChannels\Zendesk\Exceptions\CouldNotSendNotification
29
     */
30 3
    public function send($notifiable, Notification $notification)
31
    {
32 3
        $zendeskParameters = $notification->toZendesk($notifiable)->toArray();
1 ignored issue
show
Bug introduced by
The method toZendesk() 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...
33
34 3
        if (!isset($zendeskParameters['requester']['name']) or $zendeskParameters['requester']['name'] ==='') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
35 1
            $routing = collect($notifiable->routeNotificationFor('Zendesk'));
36 1
            if (! Arr::has($routing, ['name', 'email'])) {
37
                return;
38
            }
39
40 1
            $zendeskParameters['requester']['name'] = $routing['name'];
41 1
            $zendeskParameters['requester']['email'] = $routing['email'];
42 1
        }
43
44 3
        $response = $this->client->tickets()->create($zendeskParameters);
45
46 3
        if ($response->getStatusCode() !== 200) {
47 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
48
        }
49 2
    }
50
}
51