Test Failed
Push — master ( 474729...cb562e )
by recca
01:52
created

Client   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 192
Duplicated Lines 4.17 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 96.36%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 17
c 4
b 0
f 0
lcom 1
cbo 6
dl 8
loc 192
ccs 53
cts 55
cp 0.9636
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A credit() 0 17 3
A setCredit() 0 6 1
A isValidResponse() 0 4 1
A doRequest() 0 12 1
B send() 0 25 2
B remapParams() 8 24 6

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 Recca0120\Every8d;
4
5
use Carbon\Carbon;
6
use DomainException;
7
use Http\Client\HttpClient;
8
use Http\Message\MessageFactory;
9
use Http\Discovery\HttpClientDiscovery;
10
use Http\Discovery\MessageFactoryDiscovery;
11
12
class Client
13
{
14
    /**
15
     * $apiEndpoint.
16
     *
17
     * @var string
18
     */
19
    public $apiEndpoint = 'http://api.every8d.com/API21/HTTP';
20
21
    /**
22
     * $credit.
23
     *
24
     * @var float
25
     */
26
    public $credit = null;
27
28
    /**
29
     * $userId.
30
     *
31
     * @var string
32
     */
33
    protected $userId;
34
35
    /**
36
     * $password.
37
     *
38
     * @var string
39
     */
40
    protected $password;
41
42
    /**
43
     * $httpClient.
44
     *
45
     * @var \Http\Client\HttpClient
46
     */
47
    protected $httpClient;
48
49
    /**
50
     * $messageFactory.
51
     *
52
     * @var \Http\Message\MessageFactory
53
     */
54
    protected $messageFactory;
55
56
    /**
57
     * __construct.
58
     *
59
     * @param string $userId
60
     * @param string $password
61
     * @param \Http\Client\HttpClient $httpClient
62
     * @param \Http\Message\MessageFactory $messageFactory
63
     */
64 5
    public function __construct($userId, $password, HttpClient $httpClient = null, MessageFactory $messageFactory = null)
65
    {
66 5
        $this->userId = $userId;
67 5
        $this->password = $password;
68 5
        $this->httpClient = $httpClient ?: HttpClientDiscovery::find();
69 5
        $this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find();
70 5
    }
71
72
    /**
73
     * credit.
74
     *
75
     * @return float
76
     */
77 3
    public function credit()
78
    {
79 3
        if (is_null($this->credit) === false) {
80 1
            return $this->credit;
81
        }
82
83 2
        $response = $this->doRequest('getCredit.ashx', [
84 2
            'UID' => $this->userId,
85 2
            'PWD' => $this->password,
86
        ]);
87
88 2
        if ($this->isValidResponse($response) === false) {
89 1
            throw new DomainException($response, 500);
90
        }
91
92 1
        return $this->setCredit($response)->credit;
93
    }
94
95
    /**
96
     * send.
97
     *
98
     * @param array $params
99
     *
100
     * @return string
101
     */
102 2
    public function send($params)
103
    {
104 2
        $response = $this->doRequest('sendSMS.ashx', array_filter(array_merge([
105 2
            'UID' => $this->userId,
106 2
            'PWD' => $this->password,
107
            'SB' => null,
108
            'MSG' => null,
109
            'DEST' => null,
110
            'ST' => null,
111 2
        ], $this->remapParams($params))));
112
113 2
        if ($this->isValidResponse($response) === false) {
114 1
            throw new DomainException($response, 500);
115
        }
116
117 1
        list($credit, $sended, $cost, $unsend, $batchId) = explode(',', $response);
118
119
        return [
120 1
            'credit' => $this->setCredit($credit)->credit,
121 1
            'sended' => (int) $sended,
122 1
            'cost' => (float) $cost,
123 1
            'unsend' => (int) $unsend,
124 1
            'batchId' => $batchId,
125
        ];
126
    }
127
128
    /**
129
     * setCredit.
130
     *
131
     * @param string $credit
132
     */
133 2
    protected function setCredit($credit)
134
    {
135 2
        $this->credit = (float) $credit;
136
137 2
        return $this;
138
    }
139
140
    /**
141
     * isValidResponse.
142
     *
143
     * @param string $response
144
     *
145
     * @return bool
146
     */
147 4
    protected function isValidResponse($response)
148
    {
149 4
        return substr($response, 0, 1) !== '-';
150
    }
151
152
    /**
153
     * doRequest.
154
     *
155
     * @param string $uri
156
     * @param array $params
157
     *
158
     * @return string
159
     */
160 4
    protected function doRequest($uri, $params)
161
    {
162 4
        $request = $this->messageFactory->createRequest(
163 4
            'POST',
164 4
            rtrim($this->apiEndpoint, '/').'/'.$uri,
165 4
            ['Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8'],
166 4
            http_build_query($params)
167
        );
168 4
        $response = $this->httpClient->sendRequest($request);
169
170 4
        return $response->getBody()->getContents();
171
    }
172
173
    /**
174
     * remapParams.
175
     *
176
     * @param array $params
177
     * @return array
178
     */
179 2
    protected function remapParams($params)
180
    {
181 2
        if (empty($params['subject']) === false) {
182
            $params['SB'] = $params['subject'];
183
            unset($params['subject']);
184
        }
185
186 2 View Code Duplication
        if (empty($params['to']) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
187 2
            $params['DEST'] = $params['to'];
188 2
            unset($params['to']);
189
        }
190
191 2 View Code Duplication
        if (empty($params['text']) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
192 2
            $params['MSG'] = $params['text'];
193 2
            unset($params['text']);
194
        }
195
196 2
        if (empty($params['sendTime']) === false) {
197 1
            $params['ST'] = empty($params['sendTime']) === false ? Carbon::parse($params['sendTime'])->format('YmdHis') : null;
198 1
            unset($params['sendTime']);
199
        }
200
201 2
        return $params;
202
    }
203
}
204