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(); |
|
|
|
|
50
|
3 |
|
$trelloCardComments = $notification->toTrello($notifiable)->getComments(); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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.