MessageBuilder   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 151
Duplicated Lines 21.19 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 32
loc 151
rs 10
c 0
b 0
f 0
wmc 10
lcom 2
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A message() 10 10 2
A by() 0 9 1
A to() 0 9 1
A send() 22 22 3
A __get() 0 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace EntWeChat\Staff;
4
5
use EntWeChat\Core\Exceptions\InvalidArgumentException;
6
use EntWeChat\Core\Exceptions\RuntimeException;
7
use EntWeChat\Message\AbstractMessage;
8
use EntWeChat\Message\Raw as RawMessage;
9
use EntWeChat\Message\Text;
10
11
/**
12
 * Class MessageBuilder.
13
 */
14
class MessageBuilder
15
{
16
    /**
17
     * Message to send.
18
     *
19
     * @var \EntWeChat\Message\AbstractMessage;
20
     */
21
    protected $message;
22
23
    /**
24
     * Message receiver.
25
     *
26
     * @var array
27
     */
28
    protected $to;
29
30
    /**
31
     * Message sender.
32
     *
33
     * @var array
34
     */
35
    protected $by;
36
37
    /**
38
     * Staff instance.
39
     *
40
     * @var \EntWeChat\Staff\Staff
41
     */
42
    protected $staff;
43
44
    /**
45
     * User types.
46
     *
47
     * @var array
48
     */
49
    private $userTypes = [
0 ignored issues
show
Unused Code introduced by
The property $userTypes is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
50
        Staff::USER_TYPE_STAFF,
51
        Staff::USER_TYPE_USERID,
52
        Staff::USER_TYPE_OPENID,
53
    ];
54
55
    /**
56
     * MessageBuilder constructor.
57
     *
58
     * @param \EntWeChat\Staff\Staff $staff
59
     */
60
    public function __construct(Staff $staff)
61
    {
62
        $this->staff = $staff;
63
    }
64
65
    /**
66
     * Set message to send.
67
     *
68
     * @param string|AbstractMessage $message
69
     *
70
     * @throws InvalidArgumentException
71
     *
72
     * @return MessageBuilder
73
     */
74 View Code Duplication
    public function message($message)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        if (is_string($message)) {
77
            $message = new Text(['content' => $message]);
78
        }
79
80
        $this->message = $message;
81
82
        return $this;
83
    }
84
85
    /**
86
     * Set staff account to send message.
87
     *
88
     * @param string $type
89
     * @param string $id
90
     *
91
     * @return MessageBuilder
92
     */
93
    public function by($type, $id)
94
    {
95
        $this->account = [
0 ignored issues
show
Bug introduced by
The property account does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
96
            'type' => $type,
97
            'id'   => $id,
98
        ];
99
100
        return $this;
101
    }
102
103
    /**
104
     * Set target user open id.
105
     *
106
     * @param string $type
107
     * @param string $id
108
     *
109
     * @return MessageBuilder
110
     */
111
    public function to($type, $id)
112
    {
113
        $this->to = [
114
            'type' => $type,
115
            'id'   => $id,
116
        ];
117
118
        return $this;
119
    }
120
121
    /**
122
     * Send the message.
123
     *
124
     * @throws RuntimeException
125
     *
126
     * @return bool
127
     */
128 View Code Duplication
    public function send()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        if (empty($this->message)) {
131
            throw new RuntimeException('No message to send.');
132
        }
133
134
        $transformer = new Transformer();
135
136
        if ($this->message instanceof RawMessage) {
137
            $message = $this->message->get('content');
138
        } else {
139
            $content = $transformer->transform($this->message);
140
            $message = [
141
                'sender'   => $this->by,
142
                'receiver' => $this->to,
143
            ];
144
145
            $message = array_merge($message, $content);
146
        }
147
148
        return $this->staff->send($message);
149
    }
150
151
    /**
152
     * Return property.
153
     *
154
     * @param $property
155
     *
156
     * @return mixed
157
     */
158
    public function __get($property)
159
    {
160
        if (property_exists($this, $property)) {
161
            return $this->$property;
162
        }
163
    }
164
}
165