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 ( 6f220c...66a8d7 )
by Carlos
08:37 queued 04:11
created

ChuanglanGateway::wrapChannelContent()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 3
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\Exceptions\InvalidArgumentException;
18
use Overtrue\EasySms\Support\Config;
19
use Overtrue\EasySms\Traits\HasHttpRequest;
20
21
/**
22
 * Class ChuanglanGateway.
23
 *
24
 * @see https://zz.253.com/v5.html#/api_doc
25
 */
26
class ChuanglanGateway extends Gateway
27
{
28
    use HasHttpRequest;
29
30
    /**
31
     * URL模板
32
     */
33
    const ENDPOINT_URL_TEMPLATE = 'https://%s.253.com/msg/send/json';
34
35
    /**
36
     * 国际短信
37
     */
38
    const INT_URL = 'http://intapi.253.com/send/json';
39
40
    /**
41
     * 验证码渠道code.
42
     */
43
    const CHANNEL_VALIDATE_CODE = 'smsbj1';
44
45
    /**
46
     * 会员营销渠道code.
47
     */
48
    const CHANNEL_PROMOTION_CODE = 'smssh1';
49
50
    /**
51
     * @param PhoneNumberInterface $to
52
     * @param MessageInterface     $message
53
     * @param Config               $config
54
     *
55
     * @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...
56
     *
57
     * @throws GatewayErrorException
58
     * @throws InvalidArgumentException
59
     */
60 2
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
61
    {
62 2
        $IDDCode = !empty($to->getIDDCode()) ? $to->getIDDCode() : 86;
63
64
        $params = [
65 2
            'account' => $config->get('account'),
66 2
            'password' => $config->get('password'),
67 2
            'phone' => $to->getNumber(),
68 2
            'msg' => $this->wrapChannelContent($message->getContent($this), $config, $IDDCode),
69 2
        ];
70
71 2
        if (86 != $IDDCode) {
72
            $params['mobile'] = $to->getIDDCode().$to->getNumber();
73
        }
74
75 2
        $result = $this->postJson($this->buildEndpoint($config, $IDDCode), $params);
76
77 2
        if (!isset($result['code']) || '0' != $result['code']) {
78 2
            throw new GatewayErrorException(json_encode($result, JSON_UNESCAPED_UNICODE), isset($result['code']) ? $result['code'] : 0, $result);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 140 characters; contains 145 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
79
        }
80
81 2
        return $result;
82
    }
83
84
    /**
85
     * @param Config $config
86
     * @param int    $IDDCode
87
     *
88
     * @return string
89
     *
90
     * @throws InvalidArgumentException
91
     */
92 3
    protected function buildEndpoint(Config $config, $IDDCode = 86)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $IDDCode is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
93
    {
94 3
        $channel = $this->getChannel($config, $IDDCode);
95
96 3
        return sprintf(self::ENDPOINT_URL_TEMPLATE, $channel);
97
    }
98
99
    /**
100
     * @param Config $config
101
     * @param int    $IDDCode
102
     *
103
     * @return mixed
104
     *
105
     * @throws InvalidArgumentException
106
     */
107 9
    protected function getChannel(Config $config, $IDDCode)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $IDDCode is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
108
    {
109 9
        if (86 != $IDDCode) {
110
            return self::INT_URL;
111
        }
112 9
        $channel = $config->get('channel', self::CHANNEL_VALIDATE_CODE);
113
114 9
        if (!in_array($channel, [self::CHANNEL_VALIDATE_CODE, self::CHANNEL_PROMOTION_CODE])) {
115 1
            throw new InvalidArgumentException('Invalid channel for ChuanglanGateway.');
116
        }
117
118 8
        return $channel;
119
    }
120
121
    /**
122
     * @param string $content
123
     * @param Config $config
124
     * @param int    $IDDCode
125
     *
126
     * @return string|string
127
     *
128
     * @throws InvalidArgumentException
129
     */
130 6
    protected function wrapChannelContent($content, Config $config, $IDDCode)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $IDDCode is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
131
    {
132 6
        $channel = $this->getChannel($config, $IDDCode);
133
134 6
        if (self::CHANNEL_PROMOTION_CODE == $channel) {
135 4
            $sign = (string) $config->get('sign', '');
136 4
            if (empty($sign)) {
137 1
                throw new InvalidArgumentException('Invalid sign for ChuanglanGateway when using promotion channel');
138
            }
139
140 3
            $unsubscribe = (string) $config->get('unsubscribe', '');
141 3
            if (empty($unsubscribe)) {
142 1
                throw new InvalidArgumentException('Invalid unsubscribe for ChuanglanGateway when using promotion channel');
143
            }
144
145 2
            $content = $sign.$content.$unsubscribe;
146 2
        }
147
148 4
        return $content;
149
    }
150
}
151