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 ( 161b0f...b3c2eb )
by Carlos
07:11 queued 05:23
created

YunzhixunGateway::buildEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 YunzhixunGateway.
22
 *
23
 * @author her-cat <[email protected]>
24
 *
25
 * @see http://docs.ucpaas.com/doku.php?id=%E7%9F%AD%E4%BF%A1:sendsms
26
 */
27
class YunzhixunGateway extends Gateway
28
{
29
    use HasHttpRequest;
30
31
    const SUCCESS_CODE = '000000';
32
33
    const FUNCTION_SEND_SMS = 'sendsms';
34
35
    const FUNCTION_BATCH_SEND_SMS = 'sendsms_batch';
36
37
    const ENDPOINT_TEMPLATE = 'https://open.ucpaas.com/ol/%s/%s';
38
39
    /**
40
     * Send a short message.
41
     *
42
     * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
43
     * @param \Overtrue\EasySms\Contracts\MessageInterface     $message
44
     * @param \Overtrue\EasySms\Support\Config                 $config
45
     *
46
     * @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...
47
     *
48
     * @throws GatewayErrorException
49
     */
50
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
51
    {
52
        $data = $message->getData($this);
53
54
        $function = isset($data['mobiles']) ? self::FUNCTION_BATCH_SEND_SMS : self::FUNCTION_SEND_SMS;
55
56
        $endpoint = $this->buildEndpoint('sms', $function);
57
58
        $params = $this->buildParams($to, $message, $config);
59
60
        return $this->execute($endpoint, $params);
61
    }
62
63
    /**
64
     * @param $resource
65
     * @param $function
66
     *
67
     * @return string
68
     */
69
    protected function buildEndpoint($resource, $function)
70
    {
71
        return sprintf(self::ENDPOINT_TEMPLATE, $resource, $function);
72
    }
73
74
    /**
75
     * @param PhoneNumberInterface $to
76
     * @param MessageInterface     $message
77
     * @param Config               $config
78
     *
79
     * @return array
80
     */
81
    protected function buildParams(PhoneNumberInterface $to, MessageInterface $message, Config $config)
82
    {
83
        $data = $message->getData($this);
84
85
        return [
86
            'sid' => $config->get('sid'),
87
            'token' => $config->get('token'),
88
            'appid' => $config->get('app_id'),
89
            'templateid' => $message->getTemplate($this),
90
            'uid' => isset($data['uid']) ? $data['uid'] : '',
91
            'param' => isset($data['params']) ? $data['params'] : '',
92
            'mobile' => isset($data['mobiles']) ? $data['mobiles'] : $to->getNumber(),
93
        ];
94
    }
95
96
    /**
97
     * @param $endpoint
98
     * @param $params
99
     *
100
     * @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...
101
     *
102
     * @throws GatewayErrorException
103
     */
104
    protected function execute($endpoint, $params)
105
    {
106
        try {
107
            $result = $this->postJson($endpoint, $params);
108
109 View Code Duplication
            if (!isset($result['code']) || self::SUCCESS_CODE !== $result['code']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
                $code = isset($result['code']) ? $result['code'] : 0;
111
                $error = isset($result['msg']) ? $result['msg'] : json_encode($result, JSON_UNESCAPED_UNICODE);
112
113
                throw new GatewayErrorException($error, $code);
114
            }
115
116
            return $result;
117
        } catch (\Exception $e) {
118
            throw new GatewayErrorException($e->getMessage(), $e->getCode());
119
        }
120
    }
121
}
122