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.
Completed
Push — master ( d7e8fd...ae0f75 )
by Carlos
02:18
created

AliyunrestGateway::generateSign()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.7666
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 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 AliyunrestGateway.
22
 */
23
class AliyunrestGateway extends Gateway
24
{
25
    use HasHttpRequest;
26
27
    const ENDPOINT_URL = 'http://gw.api.taobao.com/router/rest';
28
29
    const ENDPOINT_VERSION = '2.0';
30
31
    const ENDPOINT_FORMAT = 'json';
32
33
    const ENDPOINT_METHOD = 'alibaba.aliqin.fc.sms.num.send';
34
35
    const ENDPOINT_SIGNATURE_METHOD = 'md5';
36
37
    const ENDPOINT_PARTNER_ID = 'EasySms';
38
39
    /**
40
     * @param PhoneNumberInterface $to
41
     * @param MessageInterface     $message
42
     * @param Config               $config
43
     *
44
     * @return array|void
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 1
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
47
    {
48
        $urlParams = [
49 1
            'app_key' => $config->get('app_key'),
50 1
            'v' => self::ENDPOINT_VERSION,
51 1
            'format' => self::ENDPOINT_FORMAT,
52 1
            'sign_method' => self::ENDPOINT_SIGNATURE_METHOD,
53 1
            'method' => self::ENDPOINT_METHOD,
54 1
            'timestamp' => date('Y-m-d H:i:s'),
55 1
            'partner_id' => self::ENDPOINT_PARTNER_ID,
56 1
        ];
57
58
        $params = [
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
59 1
            'extend' => '',
60 1
            'sms_type' => 'normal',
61 1
            'sms_free_sign_name' => $config->get('sign_name'),
62 1
            'sms_param' => json_encode($message->getData()),
63 1
            'rec_num' => !\is_null($to->getIDDCode()) ? strval($to->getZeroPrefixedNumber()) : $to->getNumber(),
64 1
            'sms_template_code' => $message->getTemplate($this),
65 1
        ];
66 1
        $urlParams['sign'] = $this->generateSign(array_merge($params, $urlParams));
67
68 1
        $result = $this->post($this->getEndpointUrl($urlParams), $params);
69
70 1
        if (isset($result['error_response']) && $result['error_response']['code'] != 0) {
71 1
            throw new GatewayErrorException($result['error_response']['msg'], $result['error_response']['code'], $result);
72
        }
73
74 1
        return $result;
75
    }
76
77
    /**
78
     * @param array $params
79
     *
80
     * @return string
81
     */
82 1
    protected function getEndpointUrl($params)
83
    {
84 1
        return self::ENDPOINT_URL.'?'.http_build_query($params);
85
    }
86
87
    /**
88
     * @param array $params
89
     *
90
     * @return string
91
     */
92 1
    protected function generateSign($params)
93
    {
94 1
        ksort($params);
95
96 1
        $stringToBeSigned = $this->config->get('app_secret_key');
97 1
        foreach ($params as $k => $v) {
98 1
            if (!is_array($v) && '@' != substr($v, 0, 1)) {
99 1
                $stringToBeSigned .= "$k$v";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $k instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $v instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
100 1
            }
101 1
        }
102 1
        unset($k, $v);
103 1
        $stringToBeSigned .= $this->config->get('app_secret_key');
104
105 1
        return strtoupper(md5($stringToBeSigned));
106
    }
107
}
108