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

AlidayuGateway   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
dl 0
loc 76
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5
ccs 28
cts 29
cp 0.9655
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B send() 0 32 3
A generateSign() 0 14 4
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 AlidayuGateway.
22
 *
23
 * @see http://open.taobao.com/doc2/apiDetail?apiId=25450#s2
24
 */
25
class AlidayuGateway extends Gateway
26
{
27
    use HasHttpRequest;
28
29
    const ENDPOINT_URL = 'https://eco.taobao.com/router/rest';
30
31
    const ENDPOINT_METHOD = 'alibaba.aliqin.fc.sms.num.send';
32
33
    const ENDPOINT_VERSION = '2.0';
34
35
    const ENDPOINT_FORMAT = 'json';
36
37
    /**
38
     * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
39
     * @param \Overtrue\EasySms\Contracts\MessageInterface     $message
40
     * @param \Overtrue\EasySms\Support\Config                 $config
41 1
     *
42
     * @return array
43 1
     *
44
     * @throws \Overtrue\EasySms\Exceptions\GatewayErrorException ;
45
     */
46
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
47
    {
48
        $params = [
49
            'method' => self::ENDPOINT_METHOD,
50
            'format' => self::ENDPOINT_FORMAT,
51
            'v' => self::ENDPOINT_VERSION,
52
            'sign_method' => 'md5',
53
            'timestamp' => date('Y-m-d H:i:s'),
54
            'sms_type' => 'normal',
55 1
            'sms_free_sign_name' => $config->get('sign_name'),
56
            'app_key' => $config->get('app_key'),
57
            'sms_template_code' => $message->getTemplate($this),
58 1
            'rec_num' => strval($to),
59 1
            'sms_param' => json_encode($message->getData($this)),
60 1
        ];
61 1
62 1
        $params['sign'] = $this->generateSign($params);
63 1
64 1
        $result = $this->post(self::ENDPOINT_URL, $params);
65 1
66 1
        if (!empty($result['error_response'])) {
67 1
            if (isset($result['error_response']['sub_msg'])) {
68 1
                $message = $result['error_response']['sub_msg'];
69 1
            } else {
70
                $message = $result['error_response']['msg'];
71 1
            }
72
73 1
            throw new GatewayErrorException($message, $result['error_response']['code'], $result);
74
        }
75 1
76 1
        return $result;
77 1
    }
78 1
79
    /**
80
     * Generate Sign.
81
     *
82 1
     * @param array $params
83
     *
84
     * @return string
85 1
     */
86
    protected function generateSign($params)
87
    {
88
        ksort($params);
89
        $stringToBeSigned = $this->config->get('app_secret');
90
        foreach ($params as $key => $value) {
91
            if (is_string($value) && '@' != substr($value, 0, 1)) {
92
                $stringToBeSigned .= "$key$value";
93
            }
94
        }
95 1
96
        $stringToBeSigned .= $this->config->get('app_secret');
97 1
98 1
        return strtoupper(md5($stringToBeSigned));
99 1
    }
100
}
101