Passed
Push — master ( 8760fe...a207da )
by Hao
03:48
created

ActionCard::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 0
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace DingRobot\Message;
4
5
use DingRobot\Message\Traits\Btn;
6
7
/**
8
 * ActionCard
9
 *
10
 * @method ActionCard title($title)
11
 * @method ActionCard text($markdownText)
12
 * @method ActionCard singleURL($singleUrl)
13
 * @method ActionCard singleTitle($singleTitle)
14
 *
15
 * @package DingRobot\Message
16
 */
17
class ActionCard extends Base
18
{
19
    use Btn;
20
21
    /**
22
     * 隐藏发消息者头像
23
     * hide sender avatar
24
     */
25
    const AVATAR_HIDE = '1';
26
27
    /**
28
     * 显示发消息者头像
29
     * show sender avatar
30
     */
31
    const AVATAR_SHOW = '0';
32
33
    /**
34
     * 按钮横向排列
35
     */
36
    const BTN_ORIENTATION_HORIZONTAL = '1';
37
38
    /**
39
     * 按钮竖直排列
40
     */
41
    const BTN_ORIENTATION_VERTICAL = '0';
42
43 4
    public function __construct($title = '')
44
    {
45 4
        parent::__construct();
46 4
        $this->title($title);
47 4
        $this->bodyName = 'actionCard';
48 4
    }
49
50 2
    protected function getBody()
51
    {
52 2
        if($this->btns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->btns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53 2
            $this->body['btns'] = $this->btns;
54 2
            foreach ($this->body['btns'] as $btn) {
55 2
                $this->validateBtn($btn);
56
            }
57
        }
58 2
        return $this->body;
59
    }
60
61 4
    protected function bodyFields()
62
    {
63 4
        return ['title', 'text', 'singleURL', 'singleTitle'];
64
    }
65
66 4
    protected function validate()
67
    {
68 4
        $this->check('title');
69 4
        $this->check('text');
70
71 3
        if (isset($this->body['singleURL']) && isset($this->body['singleTitle'])) {
72
            // 整体跳转ActionCard类型 btns 失效
73 1
            $this->check('singleURL');
74 1
            $this->check('singleTitle');
75 1
            $this->btns([]);
76 3
        } elseif ($this->btns) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $this->btns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Unused Code introduced by
This elseif statement is empty, and could be removed.

This check looks for the bodies of elseif statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These elseif bodies can be removed. If you have an empty elseif but statements in the else branch, consider inverting the condition.

Loading history...
77
            // 独立跳转ActionCard类型 校验 btn 即可
78
        } else {
79 1
            throw new \InvalidArgumentException("must have btns or single");
80
        }
81 2
    }
82
83
    /**
84
     * hide sender avatar
85
     * 隐藏发送者头像
86
     *
87
     * @return $this
88
     */
89 2
    public function hideAvatar()
90
    {
91 2
        $this->body['hideAvatar'] = self::AVATAR_HIDE;
92
93 2
        return $this;
94
    }
95
96
    /**
97
     * show sender avatar
98
     * 显示发送者头像
99
     *
100
     * @return $this
101
     */
102 2
    public function showAvatar()
103
    {
104 2
        $this->body['hideAvatar'] = self::AVATAR_SHOW;
105
106 2
        return $this;
107
    }
108
109
    /**
110
     * 设置按钮横向排列
111
     * set button as horizontal
112
     *
113
     * @return $this
114
     */
115 2
    public function btnOrientationHorizontal()
116
    {
117 2
        $this->body['btnOrientation'] = self::BTN_ORIENTATION_HORIZONTAL;
118
119 2
        return $this;
120
    }
121
122
    /**
123
     * 设置按钮纵向排列
124
     * set button as vertical
125
     *
126
     * @return $this
127
     */
128 2
    public function btnOrientationVertical()
129
    {
130 2
        $this->body['btnOrientation'] = self::BTN_ORIENTATION_VERTICAL;
131
132 2
        return $this;
133
    }
134
}