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.
Test Failed
Push — master ( 0595a3...567207 )
by Carlos
02:50 queued 31s
created

QiniuGateway::send()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 3
nc 4
nop 3
1
<?php
2
3
/*
4
 * This file is part of the overtrue/easy-sms.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Overtrue\EasySms\Gateways;
13
14
use Overtrue\EasySms\Contracts\MessageInterface;
15
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
16
use Overtrue\EasySms\Exceptions\GatewayErrorException;
17
use Overtrue\EasySms\Support\Config;
18
use Overtrue\EasySms\Traits\HasHttpRequest;
19
20
/**
21
 * Class QiniuGateway.
22
 *
23
 * @see https://developer.qiniu.com/sms/api/5897/sms-api-send-message
24
 */
25
class QiniuGateway extends Gateway
26
{
27
    use HasHttpRequest;
28
29
    const ENDPOINT_TEMPLATE = 'https://%s.qiniuapi.com/%s/%s';
30
31
    const ENDPOINT_VERSION = 'v1';
32
33
    /**
34
     * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
35
     * @param \Overtrue\EasySms\Contracts\MessageInterface     $message
36
     * @param \Overtrue\EasySms\Support\Config                 $config
37
     *
38
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be \Psr\Http\Message\ResponseInterface|array|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
39
     *
40
     * @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
41
     */
42
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
43
    {
44
        $endpoint = $this->buildEndpoint('sms', 'message/single');
45
46
        $data = $message->getData($this);
47
48
        $params = [
49
            'template_id' => $message->getTemplate($this),
50
            'mobile' => $to->getNumber(),
51
        ];
52
53
        if (!empty($data)) {
54
            $params['parameters'] = $data;
55
        }
56
57
        $headers = [
58
            'Content-Type' => 'application/json',
59
        ];
60
61
        $headers['Authorization'] = $this->generateSign($endpoint, 'POST', json_encode($params), $headers['Content-Type'], $config);
62
63
        $result = $this->postJson($endpoint, $params, $headers);
64
65
        if (isset($result['error'])) {
66
            throw new GatewayErrorException($result['message'], $result['error'], $result);
67
        }
68
69
        return $result;
70
    }
71
72
    /**
73
     * Build endpoint url.
74
     *
75
     * @param string $type
76
     * @param string $function
77
     *
78
     * @return string
79
     */
80
    protected function buildEndpoint($type, $function)
81
    {
82
        return sprintf(self::ENDPOINT_TEMPLATE, $type, self::ENDPOINT_VERSION, $function);
83
    }
84
85
    /**
86
     * Build endpoint url.
87
     *
88
     * @param string $url
89
     * @param string $method
90
     * @param string $body
91
     * @param string $contentType
92
     * @param Config $config
93
     *
94
     * @return string
95
     */
96
    protected function generateSign($url, $method, $body = null, $contentType = null, Config $config)
97
    {
98
        $urlItems = parse_url($url);
99
        $host = $urlItems['host'];
100
        if (isset($urlItems['port'])) {
101
            $port = $urlItems['port'];
102
        } else {
103
            $port = '';
104
        }
105
        $path = $urlItems['path'];
106
        if (isset($urlItems['query'])) {
107
            $query = $urlItems['query'];
108
        } else {
109
            $query = '';
110
        }
111
        //write request uri
112
        $toSignStr = $method.' '.$path;
113
        if (!empty($query)) {
114
            $toSignStr .= '?'.$query;
115
        }
116
        //write host and port
117
        $toSignStr .= "\nHost: ".$host;
118
        if (!empty($port)) {
119
            $toSignStr .= ':'.$port;
120
        }
121
        //write content type
122
        if (!empty($contentType)) {
123
            $toSignStr .= "\nContent-Type: ".$contentType;
124
        }
125
        $toSignStr .= "\n\n";
126
        //write body
127
        if (!empty($body)) {
128
            $toSignStr .= $body;
129
        }
130
131
        $hmac = hash_hmac('sha1', $toSignStr, $config->get('secret_key'), true);
132
133
        return 'Qiniu '.$config->get('access_key').':'.$this->base64UrlSafeEncode($hmac);
134
    }
135
136
    /**
137
     * @param string $data
138
     *
139
     * @return string
140
     */
141
    protected function base64UrlSafeEncode($data)
142
    {
143
        $find = array('+', '/');
144
        $replace = array('-', '_');
145
146
        return str_replace($find, $replace, base64_encode($data));
147
    }
148
}
149