SendMessageRequest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A makeMessageParameter() 0 10 1
B parseResponse() 0 15 7
1
<?php
2
/*
3
 * This file is part of the slince/smartqq package.
4
 *
5
 * (c) Slince <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Slince\SmartQQ\Request;
12
13
use GuzzleHttp\Psr7\Response;
14
use Slince\SmartQQ\Credential;
15
use Slince\SmartQQ\Exception\Code103ResponseException;
16
use Slince\SmartQQ\Message\Request\Message;
17
18
class SendMessageRequest extends Request
19
{
20
    protected $method = RequestInterface::REQUEST_METHOD_POST;
21
22
    protected $referer = 'http://d1.web2.qq.com/cfproxy.html?v=20151105001&callback=1';
23
24
    public function makeMessageParameter(Message $message, Credential $credential)
25
    {
26
        return [
27
            'content' => (string) $message->getContent(),
28
            'face' => $message->getFace(),
29
            'clientid' => $credential->getClientId(),
30
            'msg_id' => $message->getMsgId(),
31
            'psessionid' => $credential->getPSessionId(),
32
        ];
33
    }
34
35
    /**
36
     * @param Response $response
37
     *
38
     * @throws Code103ResponseException
39
     *
40
     * @return bool
41
     */
42
    public static function parseResponse(Response $response)
43
    {
44
        $jsonData = \GuzzleHttp\json_decode($response->getBody(), true);
45
        if (
46
            (isset($jsonData['errCode']) && 0 === $jsonData['errCode'])
47
            || (isset($jsonData['retcode']) && 0 === $jsonData['retcode'])
48
        ) {
49
            return true;
50
        }
51
        if (isset($jsonData['retcode']) && 103 === $jsonData['retcode']) {
52
            throw new Code103ResponseException($response);
53
        }
54
55
        return false;
56
    }
57
}
58