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 ( 1c08e5...7e3181 )
by Carlos
05:08
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
c 0
b 0
f 0
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.6
cc 4
nc 4
nop 2
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\InvalidArgumentException;
17
use Overtrue\EasySms\Exceptions\GatewayErrorException;
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
     * 验证码渠道code.
37
     */
38
    const CHANNEL_VALIDATE_CODE = 'smsbj1';
39
40
    /**
41
     * 会员营销渠道code.
42
     */
43
    const CHANNEL_PROMOTION_CODE = 'smssh1';
44
45
    /**
46
     * @param PhoneNumberInterface $to
47
     * @param MessageInterface     $message
48
     * @param Config               $config
49
     *
50
     * @return array
51
     *
52
     * @throws GatewayErrorException
53
     * @throws InvalidArgumentException
54
     */
55 2
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
56
    {
57
        $params = [
58 2
            'account' => $config->get('account'),
59 2
            'password' => $config->get('password'),
60 2
            'phone' => $to->getNumber(),
61 2
            'msg' => $this->wrapChannelContent($message->getContent($this), $config),
62 2
        ];
63
64 2
        $result = $this->postJson($this->buildEndpoint($config), $params);
65
66 2
        if (!isset($result['code']) || '0' != $result['code']) {
67 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...
68
        }
69
70 2
        return $result;
71
    }
72
73
    /**
74
     * @param Config $config
75
     *
76
     * @return string
77
     *
78
     * @throws InvalidArgumentException
79
     */
80 3
    protected function buildEndpoint(Config $config)
81
    {
82 3
        $channel = $this->getChannel($config);
83
84 3
        return sprintf(self::ENDPOINT_URL_TEMPLATE, $channel);
85
    }
86
87
    /**
88
     * @param Config $config
89
     *
90
     * @return mixed
91
     *
92
     * @throws InvalidArgumentException
93
     */
94 9
    protected function getChannel(Config $config)
95
    {
96 9
        $channel = $config->get('channel', self::CHANNEL_VALIDATE_CODE);
97
98 9
        if (!in_array($channel, [self::CHANNEL_VALIDATE_CODE, self::CHANNEL_PROMOTION_CODE])) {
99 1
            throw new InvalidArgumentException('Invalid channel for ChuanglanGateway.');
100
        }
101
102 8
        return $channel;
103
    }
104
105
    /**
106
     * @param string $content
107
     * @param Config $config
108
     *
109
     * @return string|string
110
     *
111
     * @throws InvalidArgumentException
112
     */
113 6
    protected function wrapChannelContent($content, Config $config)
114
    {
115 6
        $channel = $this->getChannel($config);
116
117 6
        if (self::CHANNEL_PROMOTION_CODE == $channel) {
118 4
            $sign = (string) $config->get('sign', '');
119 4
            if (empty($sign)) {
120 1
                throw new InvalidArgumentException('Invalid sign for ChuanglanGateway when using promotion channel');
121
            }
122
123 3
            $unsubscribe = (string) $config->get('unsubscribe', '');
124 3
            if (empty($unsubscribe)) {
125 1
                throw new InvalidArgumentException('Invalid unsubscribe for ChuanglanGateway when using promotion channel');
126
            }
127
128 2
            $content = $sign.$content.$unsubscribe;
129 2
        }
130
131 4
        return $content;
132
    }
133
}
134