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 ( b1c486...fc4ad3 )
by Carlos
02:23
created

YunxinGateway::buildSendCodeParams()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
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 YunxinGateway.
22
 *
23
 * @author her-cat <[email protected]>
24
 *
25
 * @see https://dev.yunxin.163.com/docs/product/%E7%9F%AD%E4%BF%A1/%E7%9F%AD%E4%BF%A1%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97
26
 */
27
class YunxinGateway extends Gateway
28
{
29
    use HasHttpRequest;
30
31
    const ENDPOINT_TEMPLATE = 'https://api.netease.im/%s/%s.action';
32
33
    const ENDPOINT_ACTION = 'sendCode';
34
35
    const SUCCESS_CODE = 200;
36
37
    /**
38
     * Send a short message.
39
     *
40
     * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
41
     * @param \Overtrue\EasySms\Contracts\MessageInterface $message
42
     * @param \Overtrue\EasySms\Support\Config $config
43
     *
44
     * @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...
45
     *
46
     * @throws GatewayErrorException
47
     */
48
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
49
    {
50
        $data = $message->getData($this);
51
52
        $action = isset($data['action']) ? $data['action'] : self::ENDPOINT_ACTION;
53
54
        $endpoint = $this->buildEndpoint('sms', $action);
55
56
        switch ($action) {
57
            case 'sendCode':
58
                $params = $this->buildSendCodeParams($to, $message, $config);
59
                break;
60
            case 'verifyCode':
61
                $params = $this->buildVerifyCodeParams($to, $message);
62
                break;
63
            default:
64
                throw new GatewayErrorException(sprintf('action: %s not supported', $action), 0);
65
        }
66
67
        $headers = $this->buildHeaders($config);
68
69
        try {
70
            $result = $this->post($endpoint, $params, $headers);
71
72
            if (!isset($result['code']) || self::SUCCESS_CODE !== $result['code']) {
73
                $code = isset($result['code']) ? $result['code'] : 0;
74
                $error = isset($result['msg']) ? $result['msg'] : json_encode($result, JSON_UNESCAPED_UNICODE);
75
                throw new GatewayErrorException($error, $code);
76
            }
77
        } catch (\Exception $e) {
78
            throw new GatewayErrorException($e->getMessage(), $e->getCode());
79
        }
80
81
        return $result;
82
    }
83
84
    /**
85
     * @param $resource
86
     * @param $function
87
     *
88
     * @return string
89
     */
90
    protected function buildEndpoint($resource, $function)
91
    {
92
        return sprintf(self::ENDPOINT_TEMPLATE, $resource, strtolower($function));
93
    }
94
95
    /**
96
     * Get the request headers.
97
     *
98
     * @param Config $config
99
     *
100
     * @return array
101
     */
102
    protected function buildHeaders(Config $config)
103
    {
104
        $headers = [
105
            'AppKey' => $config->get('app_key'),
106
            'Nonce' => md5(uniqid('easysms')),
107
            'CurTime' => (string) time(),
108
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
109
        ];
110
111
        $headers['CheckSum'] = sha1("{$config->get('app_secret')}{$headers['Nonce']}{$headers['CurTime']}");
112
113
        return $headers;
114
    }
115
116
    /**
117
     * @param PhoneNumberInterface $to
118
     * @param MessageInterface $message
119
     * @param Config $config
120
     *
121
     * @return array
122
     */
123
    public function buildSendCodeParams(PhoneNumberInterface $to, MessageInterface $message, Config $config)
124
    {
125
        $data = $message->getData($this);
126
        $template = $message->getTemplate($this);
127
128
        return [
129
            'mobile' => $to->getUniversalNumber(),
130
            'authCode' => array_key_exists('code', $data) ? $data['code'] : '',
131
            'deviceId' => array_key_exists('device_id', $data) ? $data['device_id'] : '',
132
            'templateid' => is_string($template) ? $template : '',
133
            'codeLen' => $config->get('code_length', 4),
134
            'needUp' => $config->get('need_up', false),
135
        ];
136
    }
137
138
    /**
139
     * @param PhoneNumberInterface $to
140
     * @param MessageInterface $message
141
     *
142
     * @return array
143
     *
144
     * @throws GatewayErrorException
145
     */
146
    public function buildVerifyCodeParams(PhoneNumberInterface $to, MessageInterface $message)
147
    {
148
        $data = $message->getData($this);
149
150
        if (!array_key_exists('code', $data)) {
151
            throw new GatewayErrorException('"code" cannot be empty', 0);
152
        }
153
154
        return [
155
            'mobile' => $to->getUniversalNumber(),
156
            'code' => $data['code'],
157
        ];
158
    }
159
}
160