Completed
Pull Request — master (#13)
by Abdullah
03:31
created

ZendeskChannel::updateTicket()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2.0185
1
<?php
2
3
namespace NotificationChannels\Zendesk;
4
5
use Illuminate\Support\Arr;
6
use Zendesk\API\HttpClient;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\Zendesk\Exceptions\CouldNotSendNotification;
9
10
class ZendeskChannel
11
{
12
    /** @var HttpClient */
13
    protected $client;
14
15
    /** @var array */
16
    protected $parameters;
17
18
    /** @param HttpClient $client */
19 2
    public function __construct(HttpClient $client)
20
    {
21 2
        $this->client = $client;
22 2
    }
23
24
    /**
25
     * Send the given notification.
26
     *
27
     * @param mixed $notifiable
28
     * @param \Illuminate\Notifications\Notification $notification
29
     *
30
     * @throws \NotificationChannels\Zendesk\Exceptions\InvalidConfiguration
31
     * @throws \NotificationChannels\Zendesk\Exceptions\CouldNotSendNotification
32
     */
33 2
    public function send($notifiable, Notification $notification)
34
    {
35 2
        $this->parameters = $notification->toZendesk($notifiable)->toArray();
36
37 2
        $id = $this->parameters['ticket'];
38
39
        if (!is_null($id)) {
40 2
            return $this->updateTicket($id, $notifiable);
41
        }
42
43
        return $this->createNewTicket($notifiable);
44 2
    }
45
46
    /**
47
     * Send update ticket request.
48
     *
49
     * @param mixed $notifiable
50
     */
51 2 View Code Duplication
    private function updateTicket($id, $notifiable)
1 ignored issue
show
Unused Code introduced by
The parameter $notifiable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method 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...
52
    {
53
        $this->prepareUpdateParameters();
54 2
55 1
        try {
56 1
            $this->client->tickets()->update($id, $this->parameters);
57
        } catch (\Exception $e) {
58
            throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
59
        }
60 1
    }
61 1
62
    /**
63 2
     * Send create ticket request.
64
     *
65
     * @param mixed $notifiable
66
     */
67 View Code Duplication
    private function createNewTicket($notifiable)
1 ignored issue
show
Duplication introduced by
This method 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...
68
    {
69
        $this->prepareCreateParameter($notifiable);
70
71
        try {
72
            $this->client->tickets()->create($this->parameters);
73
        } catch (\Exception $e) {
74
            throw CouldNotSendNotification::serviceRespondedWithAnError($e->getMessage());
75
        }
76
    }
77
78
    /**
79
     * Prepare the parameters before update request send.
80
     *
81
     * @param mixed $notifiable
0 ignored issues
show
Bug introduced by
There is no parameter named $notifiable. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
82
     */
83
    public function prepareUpdateParameters()
84
    {
85
        unset($this->parameters['subject'], $this->parameters['requester'], $this->parameters['description'], $this->parameters['ticket']);
86
    }
87
88
    /**
89
     * Prepare the parameters before create request send.
90
     *
91
     * @param mixed $notifiable
92
     */
93
    private function prepareCreateParameter($notifiable)
94
    {
95
        // Check if the requester data is not set
96
        if (! isset($this->parameters['requester']['name']) || $this->parameters['requester']['name'] === '') {
97
            $routing = collect($notifiable->routeNotificationFor('Zendesk'));
98
            if (! Arr::has($routing, ['name', 'email'])) {
99
                return;
100
            }
101
102
            $this->parameters['requester']['name'] = $routing['name'];
103
            $this->parameters['requester']['email'] = $routing['email'];
104
        }
105
106
        unset($this->parameters['ticket']);
107
    }
108
}
109