Transformer   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 229
Duplicated Lines 31.44 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 72
loc 229
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 2

12 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 16 16 4
A transformText() 0 9 1
A transformImage() 0 9 1
A transformMusic() 13 13 1
A transformVideo() 12 12 1
A transformVoice() 0 9 1
A transformFile() 0 9 1
A transformNews() 20 20 3
A transformMpNews() 0 22 3
A transformMaterial() 11 11 1
A transformTextCard() 0 12 1
A transformCard() 0 9 1

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\Broadcast;
4
5
use EntWeChat\Message\AbstractMessage;
6
use EntWeChat\Message\News;
7
use EntWeChat\Message\Text;
8
9
/**
10
 * Class Transformer.
11
 */
12
class Transformer
13
{
14
    /**
15
     * transform message to XML.
16
     *
17
     * @param array|string|AbstractMessage $message
18
     *
19
     * @return array
20
     */
21 View Code Duplication
    public function transform($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...
22
    {
23
        if (is_array($message)) {
24
            $class = News::class;
25
        } else {
26
            if (is_string($message)) {
27
                $message = new Text(['content' => $message]);
28
            }
29
30
            $class = get_class($message);
31
        }
32
33
        $handle = 'transform'.substr($class, strlen('EntWeChat\Message\\'));
34
35
        return method_exists($this, $handle) ? $this->$handle($message) : [];
36
    }
37
38
    /**
39
     * Transform text message.
40
     *
41
     * @return array
42
     */
43
    public function transformText(AbstractMessage $message)
44
    {
45
        return [
46
            'msgtype' => 'text',
47
            'text'    => [
48
                'content' => $message->get('content'),
49
            ],
50
        ];
51
    }
52
53
    /**
54
     * Transform image message.
55
     *
56
     * @return array
57
     */
58
    public function transformImage(AbstractMessage $message)
59
    {
60
        return [
61
            'msgtype' => 'image',
62
            'image'   => [
63
                'media_id' => $message->get('media_id'),
64
            ],
65
        ];
66
    }
67
68
    /**
69
     * Transform music message.
70
     *
71
     * @return array
72
     */
73 View Code Duplication
    public function transformMusic(AbstractMessage $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...
74
    {
75
        return [
76
            'msgtype' => 'music',
77
            'music'   => [
78
                'title'          => $message->get('title'),
79
                'description'    => $message->get('description'),
80
                'musicurl'       => $message->get('url'),
81
                'hqmusicurl'     => $message->get('hq_url'),
82
                'thumb_media_id' => $message->get('thumb_media_id'),
83
            ],
84
        ];
85
    }
86
87
    /**
88
     * Transform video message.
89
     *
90
     * @return array
91
     */
92 View Code Duplication
    public function transformVideo(AbstractMessage $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...
93
    {
94
        return [
95
            'msgtype' => 'video',
96
            'video'   => [
97
                'title'          => $message->get('title'),
98
                'media_id'       => $message->get('media_id'),
99
                'description'    => $message->get('description'),
100
                'thumb_media_id' => $message->get('thumb_media_id'),
101
            ],
102
        ];
103
    }
104
105
    /**
106
     * Transform voice message.
107
     *
108
     * @return array
109
     */
110
    public function transformVoice(AbstractMessage $message)
111
    {
112
        return [
113
            'msgtype' => 'voice',
114
            'voice'   => [
115
                'media_id' => $message->get('media_id'),
116
            ],
117
        ];
118
    }
119
120
    /**
121
     * Transform file message.
122
     *
123
     * @return array
124
     */
125
    public function transformFile(AbstractMessage $message)
126
    {
127
        return [
128
            'msgtype' => 'file',
129
            'file'    => [
130
                'media_id' => $message->get('media_id'),
131
            ],
132
        ];
133
    }
134
135
    /**
136
     * Transform articles message.
137
     *
138
     * @return array
139
     */
140 View Code Duplication
    public function transformNews($news)
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...
141
    {
142
        $articles = [];
143
144
        if (!is_array($news)) {
145
            $news = [$news];
146
        }
147
148
        foreach ($news as $item) {
149
            $articles[] = [
150
                'title'       => $item->get('title'),
151
                'description' => $item->get('description'),
152
                'url'         => $item->get('url'),
153
                'picurl'      => $item->get('pic_url'),
154
                'btntxt'      => $item->get('btntxt'),
155
            ];
156
        }
157
158
        return ['msgtype' => 'news', 'news' => ['articles' => $articles]];
159
    }
160
161
    /**
162
     * Transform articles message.
163
     *
164
     * @return array
165
     */
166
    public function transformMpNews($news)
167
    {
168
        $articles = [];
169
170
        if (!is_array($news)) {
171
            $news = [$news];
172
        }
173
174
        foreach ($news as $item) {
175
            $articles[] = [
176
                'title'              => $item->get('title'),
177
                'thumb_media_id'     => $item->get('thumb_media_id'),
178
                'author'             => $item->get('author'),
179
                'content_source_url' => $item->get('content_source_url'),
180
                'content'            => $item->get('content'),
181
                'digest'             => $item->get('digest'),
182
                'show_cover_pic'     => $item->get('show_cover_pic'),
183
            ];
184
        }
185
186
        return ['msgtype' => 'mpnews', 'news' => ['articles' => $articles]];
187
    }
188
189
    /**
190
     * Transform material message.
191
     *
192
     * @return array
193
     */
194 View Code Duplication
    public function transformMaterial(AbstractMessage $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...
195
    {
196
        $type = $message->getType();
197
198
        return [
199
            'msgtype' => $type,
200
            $type     => [
201
                'media_id' => $message->get('media_id'),
202
            ],
203
        ];
204
    }
205
206
    /**
207
     * Transform textcard message.
208
     *
209
     * @return array
210
     */
211
    public function transformTextCard(AbstractMessage $message)
212
    {
213
        return [
214
            'msgtype'  => 'textcard',
215
            'textcard' => [
216
                'title'       => $message->get('title'),
217
                'description' => $message->get('description'),
218
                'url'         => $message->get('url'),
219
                'btntxt'      => $message->get('btntxt'),
220
            ],
221
        ];
222
    }
223
224
    /**
225
     * Transform card message.
226
     *
227
     * @param $message
228
     *
229
     * @return array
230
     */
231
    public function transformCard($message)
232
    {
233
        return [
234
            'wxcard'  => [
235
                'card_id' => $message,
236
            ],
237
            'msgtype' => 'wxcard',
238
        ];
239
    }
240
}
241