GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 6f7b29...eafe6c )
by Christoph
02:35
created

TwitterChannel::addImagesIfGiven()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
namespace NotificationChannels\Twitter;
4
5
use Abraham\TwitterOAuth\TwitterOAuth;
6
use Illuminate\Notifications\Notifiable;
7
use Illuminate\Notifications\Notification;
8
use NotificationChannels\Twitter\Exceptions\CouldNotSendNotification;
9
10
class TwitterChannel
11
{
12
    /** @var TwitterOAuth */
13
    protected $twitter;
14
15
    /** @param TwitterOAuth $twitter */
16 3
    public function __construct(TwitterOAuth $twitter)
17
    {
18 3
        $this->twitter = $twitter;
19 3
    }
20
21
    /**
22
     * Send the given notification.
23
     *
24
     * @param mixed $notifiable
25
     * @param \Illuminate\Notifications\Notification $notification
26
     * @throws CouldNotSendNotification
27
     */
28 3
    public function send($notifiable, Notification $notification)
29
    {
30 3
        $this->changeTwitterSettingsIfNeeded($notifiable);
31
32 3
        $twitterMessage = $notification->toTwitter($notifiable);
0 ignored issues
show
Bug introduced by
The method toTwitter() 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 3
        $twitterMessage = $this->addImagesIfGiven($twitterMessage);
34
35 3
        $this->twitter->post($twitterMessage->getApiEndpoint(), $twitterMessage->getRequestBody($this->twitter),
36 3
            $twitterMessage->isJsonRequest);
37
38 3
        if ($this->twitter->getLastHttpCode() !== 200) {
39 1
            throw CouldNotSendNotification::serviceRespondsNotSuccessful($this->twitter->getLastBody());
40
        }
41 2
    }
42
43
    /**
44
     * Use per user settings instead of default ones.
45
     *
46
     * @param Notifiable $notifiable
0 ignored issues
show
introduced by
The type Notifiable for parameter $notifiable is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?
Loading history...
47
     */
48 3
    private function changeTwitterSettingsIfNeeded($notifiable)
49
    {
50 3
        if ($twitterSettings = $notifiable->routeNotificationFor('twitter')) {
51
            $this->twitter = new TwitterOAuth($twitterSettings[0], $twitterSettings[1], $twitterSettings[2],
52
                $twitterSettings[3]);
53
        }
54 3
    }
55
56
    /**
57
     * If it is a status update message and images are provided, add them.
58
     *
59
     * @param $twitterMessage
60
     * @return mixed
61
     */
62 3
    private function addImagesIfGiven($twitterMessage)
63
    {
64 3
        if (is_a($twitterMessage, TwitterStatusUpdate::class) && $twitterMessage->getImages()) {
65 1
            $this->twitter->setTimeouts(10, 15);
66
67 1
            $twitterMessage->imageIds = collect($twitterMessage->getImages())->map(function (TwitterImage $image) {
68 1
                $media = $this->twitter->upload('media/upload', ['media' => $image->getPath()]);
69
70 1
                return $media->media_id_string;
71 1
            });
72
        }
73
74
        return $twitterMessage;
75
    }
76
}
77