PivotalTrackerChannel::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace NotificationChannels\PivotalTracker;
4
5
use GuzzleHttp\Client;
6
use Illuminate\Notifications\Notification;
7
use NotificationChannels\PivotalTracker\Exceptions\CouldNotSendNotification;
8
9
class PivotalTrackerChannel
10
{
11
    const API_ENDPOINT = 'https://www.pivotal-tracker.com/services/v5/';
12
13
    /** @var Client */
14
    protected $client;
15
16
    /** @param Client $client */
17
    public function __construct(Client $client)
18
    {
19
        $this->client = $client;
20
    }
21
22
    /**
23
     * Send the given notification.
24
     *
25
     * @param mixed $notifiable
26
     * @param \Illuminate\Notifications\Notification $notification
27
     *
28
     * @throws \NotificationChannels\PivotalTracker\Exceptions\CouldNotSendNotification
29
     */
30
    public function send($notifiable, Notification $notification)
31
    {
32
        if (! $routing = collect($notifiable->routeNotificationFor('PivotalTracker'))) {
33
            return;
34
        }
35
36
        $parameters = $notification->toPivotalTracker($notifiable)->toArray();
0 ignored issues
show
Bug introduced by
The method toPivotalTracker() 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...
37
38
        $response = $this->client->request('POST', $this->storiesEndpoint($routing->get('projectId')), [
39
            'headers' => [
40
                'X-TrackerToken' => $routing->get('token'),
41
                'Content-Type' => 'application/json',
42
            ],
43
            'json' => $parameters,
44
        ]);
45
46
        if ($response->getStatusCode() !== 200) {
47
            throw CouldNotSendNotification::serviceRespondedWithAnError($response);
48
        }
49
    }
50
51
    /**
52
     * Return the stories api endpoint for a given project.
53
     *
54
     * @param int $projectId the projet identifier
55
     *
56
     * @return string the stories endpoint
57
     */
58
    protected function storiesEndpoint($projectId)
59
    {
60
        return self::API_ENDPOINT."projects/{$projectId}/stories";
61
    }
62
}
63