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

TwitterDirectMessage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 96
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getContent() 0 4 1
A getReceiver() 0 18 3
A getApiEndpoint() 0 4 1
A getRequestBody() 0 16 1
1
<?php
2
3
namespace NotificationChannels\Twitter;
4
5
use Abraham\TwitterOAuth\TwitterOAuth;
6
use NotificationChannels\Twitter\Exceptions\CouldNotSendNotification;
7
8
class TwitterDirectMessage
9
{
10
    /** @var string */
11
    private $content;
12
13
    /** @var string */
14
    private $to;
15
16
    /** @var bool */
17
    public $isJsonRequest = true;
18
19
    /** @var string */
20
    private $apiEndpoint = 'direct_messages/events/new';
21
22
    /**
23
     * TwitterDirectMessage constructor.
24
     *
25
     * @param $to
26
     * @param $content
27
     */
28 6
    public function __construct($to, $content)
29
    {
30 6
        $this->to = $to;
31 6
        $this->content = $content;
32 6
    }
33
34
    /**
35
     * Get Twitter direct message content.
36
     *
37
     * @return  string
38
     */
39 3
    public function getContent()
40
    {
41 3
        return $this->content;
42
    }
43
44
    /**
45
     * Get Twitter direct message receiver.
46
     *
47
     * @param TwitterOAuth $twitter
48
     * @return  string
49
     * @throws CouldNotSendNotification
50
     */
51 4
    public function getReceiver(TwitterOAuth $twitter)
52
    {
53 4
        if (is_int($this->to)) {
54 3
            return $this->to;
55
        }
56
57 1
        $user = $twitter->get('users/show', [
58 1
            'screen_name' => $this->to,
59
            'include_user_entities' => false,
60
            'skip_status' => true,
61
        ]);
62
63 1
        if ($twitter->getLastHttpCode() === 404) {
64
            throw CouldNotSendNotification::userWasNotFound($twitter->getLastBody());
65
        }
66
67 1
        return $user->id;
68
    }
69
70
    /**
71
     * Return Twitter direct message api endpoint.
72
     *
73
     * @return  string
74
     */
75 1
    public function getApiEndpoint()
76
    {
77 1
        return $this->apiEndpoint;
78
    }
79
80
    /**
81
     * Build Twitter request body.
82
     *
83
     * @param TwitterOAuth $twitter
84
     * @return  array
85
     * @throws CouldNotSendNotification
86
     */
87 1
    public function getRequestBody(TwitterOAuth $twitter)
88
    {
89
        return [
90
            'event' => [
91 1
                'type' => 'message_create',
92
                'message_create' => [
93
                    'target' => [
94 1
                        'recipient_id' => $this->getReceiver($twitter),
95
                    ],
96
                    'message_data' => [
97 1
                        'text' => $this->getContent(),
98
                    ],
99
                ],
100
            ],
101
        ];
102
    }
103
}
104