Passed
Push — master ( 371c27...e17cde )
by Hao
04:51
created

Base   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 29
cts 30
cp 0.9667
rs 10
c 0
b 0
f 0
wmc 20

6 Methods

Rating   Name   Duplication   Size   Complexity  
A jsonSerialize() 0 6 2
A __construct() 0 2 1
A send() 0 7 2
A __callStatic() 0 3 1
B validate() 0 14 11
A __call() 0 9 3
1
<?php
2
3
namespace DingRobot\Message;
4
5
use DingRobot\Contract\RequesterContract;
6
use DingRobot\Message\Traits\At;
7
use DingRobot\Requester\CurlRequester;
8
9
/**
10
 * Base Class
11
 *
12
 * @package DingRobot\Message
13
 */
14
abstract class Base implements \JsonSerializable
15
{
16
    use At;
17
    /**
18
     * @var string
19
     */
20
    protected $bodyName;
21
    protected $at;
22
    protected $body;
23
24 11
    public function __construct()
25
    {
26 11
    }
27
28 8
    public function jsonSerialize()
29
    {
30
        return [
31 8
            'msgtype'       => $this->bodyName,
32 8
            $this->bodyName => $this->getBody(),
33 7
        ] + $this->getAt() ?: [];
34
    }
35
36
    abstract protected function getBody();
37
38
    abstract protected function bodyFields();
39
40 9
    protected function validate()
41
    {
42 9
        if (empty($this->bodyFields())) {
43 1
            return;
44
        }
45 8
        foreach ($this->bodyFields() as $field => $config) {
46 8
            $content  = isset($this->body[$field]) ? $this->body[$field] : null;
47 8
            $required = isset($config['required']) ? $config['required'] : null;
48 8
            $type     = isset($config['type']) ? $config['type'] : null;
49 8
            if ($required && !$content) {
50 1
                throw new \InvalidArgumentException("{$field} is required");
51
            }
52 8
            if ($type == 'string' && $content && !is_string($content)) {
53 1
                throw new \InvalidArgumentException("{$field} must be string");
54
            }
55
        }
56 7
    }
57
58
    /**
59
     * 发送
60
     * send
61
     *
62
     * @param string            $address 接口地址
63
     * @param RequesterContract $requester
64
     *
65
     * @return mixed
66
     */
67 9
    public function send($address = '', RequesterContract $requester = null)
68
    {
69 9
        $this->validate();
70 8
        if ($requester) {
71 8
            return $requester->request($address, json_encode($this->jsonSerialize()));
72
        } else {
73
            return (new CurlRequester())->request($address, json_encode($this->jsonSerialize()));
74
        }
75
    }
76
77 9
    public function __call($name, $args)
78
    {
79 9
        if (array_key_exists($name, $this->bodyFields())) {
80 8
            $this->body[$name] = isset($args[0]) ? $args[0] : '';
81
        } else {
82 1
            throw new \BadMethodCallException("field {$name} not found");
83
        }
84
85 8
        return $this;
86
    }
87
88 1
    public static function __callStatic($name, $args)
89
    {
90 1
        return (new static)->$name(...$args);
91
    }
92
}