Passed
Push — master ( 1828fb...5ac899 )
by Zing
06:56
created

MeilianGateway::send()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 24
ccs 16
cts 16
cp 1
rs 9.7666
cc 4
nc 3
nop 3
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\LaravelSms\Gateways;
6
7
use Illuminate\Support\Arr;
8
use Overtrue\EasySms\Contracts\MessageInterface;
9
use Overtrue\EasySms\Contracts\PhoneNumberInterface;
10
use Overtrue\EasySms\Gateways\Gateway;
11
use Overtrue\EasySms\Support\Config;
12
use Overtrue\EasySms\Traits\HasHttpRequest;
13
use Zing\LaravelSms\Exceptions\CouldNotSendNotification;
14
15
class MeilianGateway extends Gateway
16
{
17 1
    use HasHttpRequest;
18
19
    public const ENDPOINT_URL = 'http://m.5c.com.cn/api/send/index.php';
20
21
    /**
22
     * @param \Overtrue\EasySms\Contracts\PhoneNumberInterface $to
23
     * @param \Overtrue\EasySms\Contracts\MessageInterface $message
24
     * @param \Overtrue\EasySms\Support\Config $config
25
     *
26
     * @throws \Zing\LaravelSms\Exceptions\CouldNotSendNotification
27
     *
28
     * @return array
29
     */
30 4
    public function send(PhoneNumberInterface $to, MessageInterface $message, Config $config)
31
    {
32 4
        $endpoint = self::ENDPOINT_URL;
33 4
        $signature = $this->config->get('signature', '');
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' => $to->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
49 3
        if (strpos($result, 'error') !== false) {
50 1
            throw new CouldNotSendNotification($result, 1, Arr::wrap($result));
51
        }
52
53 3
        return ['success' => true, 'msg' => 'ok', 'result' => $result];
54
    }
55
}
56