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 Setup Failed
Push — master ( 9d889d...27a869 )
by Carlos
02:09
created

AliyunGateway::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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 AliyunGateway.
22
 *
23
 * @author carson <[email protected]>
24
 *
25
 * @see https://help.aliyun.com/document_detail/55451.html
26
 */
27
class AliyunGateway extends Gateway
28
{
29
    use HasHttpRequest;
30
31
    const ENDPOINT_URL = 'http://dysmsapi.aliyuncs.com';
32
33
    const ENDPOINT_METHOD = 'SendSms';
34
35
    const ENDPOINT_VERSION = '2017-05-25';
36
37
    const ENDPOINT_FORMAT = 'JSON';
38
39
    const ENDPOINT_REGION_ID = 'cn-hangzhou';
40
41
    const ENDPOINT_SIGNATURE_METHOD = 'HMAC-SHA1';
42
43
    const ENDPOINT_SIGNATURE_VERSION = '1.0';
44
45
    /**
46
     * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
47
     * @param \Overtrue\EasySms\Contracts\MessageInterface     $message
48
     * @param \Overtrue\EasySms\Support\Config                 $config
49 1
     *
50
     * @return array
51 1
     *
52
     * @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
53
     */
54
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
55
    {
56
        $params = [
57
            'RegionId' => self::ENDPOINT_REGION_ID,
58
            'AccessKeyId' => $config->get('access_key_id'),
59
            'Format' => self::ENDPOINT_FORMAT,
60
            'SignatureMethod' => self::ENDPOINT_SIGNATURE_METHOD,
61
            'SignatureVersion' => self::ENDPOINT_SIGNATURE_VERSION,
62
            'SignatureNonce' => uniqid(),
63 1
            'Timestamp' => $this->getTimestamp(),
64
            'Action' => self::ENDPOINT_METHOD,
65
            'Version' => self::ENDPOINT_VERSION,
66 1
            'PhoneNumbers' => strval($to->getZeroPrefixedNumber()),
67 1
            'SignName' => $config->get('sign_name'),
68 1
            'TemplateCode' => $message->getTemplate($this),
69 1
            'TemplateParam' => json_encode($message->getData($this), JSON_FORCE_OBJECT),
70 1
        ];
71 1
72 1
        $params['Signature'] = $this->generateSign($params);
73 1
74 1
        $result = $this->get(self::ENDPOINT_URL, $params);
75 1
76 1
        if ('OK' != $result['Code']) {
77 1
            throw new GatewayErrorException($result['Message'], $result['Code'], $result);
78 1
        }
79 1
80
        return $result;
81 1
    }
82
83 1
    /**
84
     * Generate Sign.
85 1
     *
86 1
     * @param array $params
87
     *
88
     * @return string
89 1
     */
90
    protected function generateSign($params)
91
    {
92
        ksort($params);
93
        $accessKeySecret = $this->config->get('access_key_secret');
94
        $stringToSign = 'GET&%2F&'.urlencode(http_build_query($params, null, '&', PHP_QUERY_RFC3986));
95
96
        return base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret.'&', true));
97
    }
98
99 1
    /**
100
     * @return false|string
101 1
     */
102 1
    protected function getTimestamp()
103 1
    {
104
        $timezone = date_default_timezone_get();
105 1
        date_default_timezone_set('GMT');
106
        $timestamp = date('Y-m-d\TH:i:s\Z');
107
        date_default_timezone_set($timezone);
108
109
        return $timestamp;
110
    }
111
}
112