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

MeilianGateway   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 18
c 1
b 0
f 0
dl 0
loc 39
ccs 17
cts 17
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A send() 0 24 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