Passed
Push — master ( ac4415...6c0960 )
by Zing
04:55
created

MeilianGateway::send()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 15
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 25
ccs 16
cts 16
cp 1
crap 4
rs 9.7666
1
<?php
2
3
namespace Zing\LaravelSms\Gateways;
4
5
use Illuminate\Support\Arr;
6
use Overtrue\EasySms\Contracts\MessageInterface;
7
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
8
use Overtrue\EasySms\Gateways\Gateway;
9
use Overtrue\EasySms\Support\Config;
10
use Overtrue\EasySms\Traits\HasHttpRequest;
11
use Zing\LaravelSms\Exceptions\CouldNotSendNotification;
12
13
class MeilianGateway extends Gateway
14
{
15
    use HasHttpRequest;
16
17
    public const ENDPOINT_URL = 'http://m.5c.com.cn/api/send/index.php';
18
19
    /**
20
     * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $number
21
     * @param \Overtrue\EasySms\Contracts\MessageInterface $message
22
     * @param \Overtrue\EasySms\Support\Config $config
23
     *
24
     * @return string
25
     *
26
     * @throws \Zing\LaravelSms\Exceptions\CouldNotSendNotification
27
     */
28 4
    public function send(PhoneNumberInterface $number, MessageInterface $message, Config $config)
29
    {
30 4
        $endpoint = self::ENDPOINT_URL;
31
32 4
        $signature = $this->config->get('signature', '');
33
34 4
        $content = $message->getContent($this);
35 4
        $result = $this->post(
36 4
            $endpoint,
37
            [
38 4
                'username' => $this->config->get('username'),
39 4
                'password' => $this->config->get('password'),
40 4
                'apikey' => $this->config->get('api_key'),
41 4
                'mobile' => $number->getUniversalNumber(),
42 4
                'content' => strpos($content, '【') === 0 ? $content : $signature . $content,
43
            ]
44
        );
45 4
        if (! is_string($result)) {
0 ignored issues
show
introduced by
The condition is_string($result) is always false.
Loading history...
46 1
            throw new CouldNotSendNotification('meilian response does only seem to accept string.');
47
        }
48 3
        if (strpos($result, 'error') !== false) {
49 1
            throw new CouldNotSendNotification($result, 1, Arr::wrap($result));
50
        }
51
52 3
        return $result;
53
    }
54
}
55