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 ( 85040b...daa0b4 )
by Carlos
02:05
created

ModuyunGateway::send()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
cc 4
nc 8
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 ModuyunGateway.
22
 *
23
 * @see https://www.moduyun.com/doc/index.html#10002
24
 */
25
class ModuyunGateway extends Gateway
26
{
27
    use HasHttpRequest;
28
29
    const ENDPOINT_URL = 'https://live.moduyun.com/sms/v2/sendsinglesms';
30
31
    /**
32
     * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
33
     * @param \Overtrue\EasySms\Contracts\MessageInterface     $message
34
     * @param \Overtrue\EasySms\Support\Config                 $config
35
     *
36
     * @return array
37
     *
38
     * @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
39
     */
40
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
41
    {
42
        $urlParams = [
43
            'accesskey' => $config->get('accesskey'),
44
            'random' => rand(100000, 999999),
45
        ];
46
47
        $params = [
48
            'tel' => [
49
                'mobile' => $to->getNumber(),
50
                'nationcode' => $to->getIDDCode() ?: '86',
51
            ],
52
            'signId' => $config->get('signId', ''),
53
            'templateId' => $message->getTemplate($this),
54
            'time' => time(),
55
            'type' => $config->get('type', 0),
56
            'params' => array_values($message->getData($this)),
57
            'ext' => '',
58
            'extend' => '',
59
        ];
60
        $params['sig'] = $this->generateSign($params, $urlParams['random']);
61
62
        $result = $this->request('post', $this->getEndpointUrl($urlParams), [
63
            'headers' => [
64
                'Content-Type' => 'application/json',
65
                'Accept' => 'application/json'
66
            ],
67
            'json' => $params,
68
        ]);
69
70
        $result = is_string($result) ? json_decode($result, true) : $result;
71
        if (0 != $result['result']) {
72
            throw new GatewayErrorException($result['errmsg'], $result['result'], $result);
73
        }
74
75
        return $result;
76
    }
77
78
    /**
79
     * @param array $params
80
     *
81
     * @return string
82
     */
83
    protected function getEndpointUrl($params)
84
    {
85
        return self::ENDPOINT_URL . '?' . http_build_query($params);
86
    }
87
88
    /**
89
     * Generate Sign.
90
     *
91
     * @param array  $params
92
     * @param string $random
93
     *
94
     * @return string
95
     */
96
    protected function generateSign($params, $random)
97
    {
98
        return hash('sha256', sprintf(
99
            'secretkey=%s&random=%d&time=%d&mobile=%s',
100
            $this->config->get('secretkey'),
101
            $random,
102
            $params['time'],
103
            $params['tel']['mobile']
104
        ));
105
    }
106
}
107