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.

RequestOptionsFactory::createForMessage()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
ccs 27
cts 27
cp 1
rs 8.9411
cc 2
eloc 23
nc 2
nop 4
crap 2
1
<?php
2
3
namespace Tgallice\FBMessenger;
4
5
use GuzzleHttp\RequestOptions;
6
use Tgallice\FBMessenger\Model\Message;
7
8
class RequestOptionsFactory
9
{
10
11
    /**
12
     * @param string $recipientOrPhone
13
     * @param string $typingIndicator
14
     *
15
     * @return array
16
     */
17 2
    public static function createForTyping($recipientOrPhone, $typingIndicator) {
18 2
        $options = [];
19
        $data = [
20 2
            'recipient'     => self::createRecipientField($recipientOrPhone),
21 2
            'sender_action' => $typingIndicator,
22 2
        ];
23 2
        $options[RequestOptions::JSON] = $data;
24
25 2
        return $options;
26
    }
27
28
    /**
29
     * @param string $recipientOrPhone
30
     * @param Message $message
31
     * @param string $notificationType
32
     *
33
     * @return array
34
     */
35 6
    public static function createForMessage($recipientOrPhone, Message $message, $notificationType = NotificationType::REGULAR, $messagingType = MessagingType::RESPONSE)
36
    {
37 6
        $options = [];
38
        $data = [
39 6
            'messaging_type' => $messagingType,
40 6
            'recipient' => self::createRecipientField($recipientOrPhone),
41 6
            'message' => $message,
42 6
            'notification_type' => $notificationType,
43 6
        ];
44
        
45 6
        if ($message->hasFileToUpload()) {
46
47
            // Create a multipart request
48 1
            $options[RequestOptions::MULTIPART] = [
49
                [
50 1
                  'name' => 'messaging_type',
51
                  'contents' => $messagingType
52 1
                ],
53
                [
54 1
                    'name' => 'recipient',
55 1
                    'contents' => json_encode($data['recipient']),
56 1
                ],
57
                [
58 1
                    'name' => 'message',
59 1
                    'contents' => json_encode($data['message']),
60 1
                ],
61
                [
62 1
                    'name' => 'notification_type',
63 1
                    'contents' => $data['notification_type'],
64 1
                ],
65
                [
66 1
                    'name' => 'filedata',
67 1
                    'contents' => $message->getFileStream(),
68 1
                ],
69
            ];
70
71
            // Update timeout if we upload a file
72 1
            $options['timeout'] = Client::DEFAULT_FILE_UPLOAD_TIMEOUT;
73
74 1
            return $options;
75
        }
76
77 5
        $options[RequestOptions::JSON] = $data;
78
79 5
        return $options;
80
    }
81
82 10
    public static function createRecipientField($recipientOrPhone)
83
    {
84 10
        $recipientFieldName = strpos($recipientOrPhone, '+') === 0 ? 'phone_number' : 'id';
85
86 10
        return [$recipientFieldName => $recipientOrPhone];
87
    }
88
}
89