Completed
Pull Request — master (#7)
by
unknown
02:17
created

TrelloChannel::addComments()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 11
cts 12
cp 0.9167
rs 9.2
cc 4
eloc 9
nc 4
nop 3
crap 4.0092
1
<?php
2
3
namespace NotificationChannels\Trello;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Support\Arr;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\Trello\Exceptions\CouldNotAddComment;
9
use NotificationChannels\Trello\Exceptions\InvalidConfiguration;
10
use NotificationChannels\Trello\Exceptions\CouldNotSendNotification;
11
12
class TrelloChannel
13
{
14
    const API_ENDPOINT = 'https://api.trello.com/1/cards/';
15
16
    /** @var Client */
17
    protected $client;
18
19
    /** @var key trello key */
20
    protected $key;
21
22
    /** @param Client $client */
23 5
    public function __construct(Client $client)
24
    {
25 5
        $this->client = $client;
26 5
        $this->key = config('services.trello.key');
27 5
    }
28
29
    /**
30
     * Send the given notification.
31
     *
32
     * @param mixed $notifiable
33
     * @param \Illuminate\Notifications\Notification $notification
34
     *
35
     * @throws \NotificationChannels\Trello\Exceptions\InvalidConfiguration
36
     * @throws \NotificationChannels\Trello\Exceptions\CouldNotSendNotification
37
     * @throws \NotificationChannels\Trello\Exceptions\CouldAddComment
38
     */
39 4
    public function send($notifiable, Notification $notification)
40
    {
41 4
        if (! $routing = collect($notifiable->routeNotificationFor('Trello'))) {
42
            return;
43
        }
44
45 4
        if (is_null($this->key)) {
46 1
            throw InvalidConfiguration::configurationNotSet();
47
        }
48
49 3
        $trelloParameters = $notification->toTrello($notifiable)->toArray();
0 ignored issues
show
Bug introduced by
The method toTrello() 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...
50 3
        $trelloCardComments = $notification->toTrello($notifiable)->getComments();
0 ignored issues
show
Bug introduced by
The method toTrello() 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...
51
52 3
        $response = $this->client->post(self::API_ENDPOINT.'?key='.$this->key.'&token='.$routing->get('token'), [
53 3
            'form_params' => Arr::set($trelloParameters, 'idList', $routing->get('idList')),
54 3
        ]);
55
56 3
        if ($response->getStatusCode() !== 200) {
57 1
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
58
        }
59
60 2
        if ($trelloCardComments) {
61 1
            $this->addComments($notifiable, $trelloCardComments, $response);
0 ignored issues
show
Documentation introduced by
$response is of type object<Psr\Http\Message\ResponseInterface>, but the function expects a object<NotificationChannels\Trello\Response>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62 1
        }
63 2
    }
64
65
    /**
66
     * Add comments to newly created card.
67
     * @param mixed $notifiable
68
     * @param array  $trelloCardComments array holding the comments to add
69
     * @param Response $response           response object from the trello api
70
     *
71
     * @throws \NotificationChannels\Trello\Exceptions\CouldAddComment
72
     */
73 2
    public function addComments($notifiable, array $trelloCardComments, $response)
74
    {
75 2
        if (! $routing = collect($notifiable->routeNotificationFor('Trello'))) {
76
            return;
77
        }
78
79 2
        $cardId = json_decode($response->getBody()->getContents())->id;
80 2
        foreach ($trelloCardComments as $comment) {
81 2
            $response = $this->client->post(self::API_ENDPOINT.$cardId.'/actions/comments?key='.$this->key.'&token='.$routing->get('token'), [
82 2
                'form_params' => ['text' => $comment],
83 2
            ]);
84 2
            if ($response->getStatusCode() !== 200) {
85 1
                throw CouldNotAddComment::serviceRespondedWithAnError($response);
86
            }
87 1
        }
88 1
    }
89
}
90