Completed
Push — master ( e5cc01...3d4df1 )
by Taosikai
13:48
created

SendMessageRequest::parseResponse()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 9
c 2
b 1
f 0
nc 3
nop 1
dl 0
loc 14
rs 8.8571
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
     * @throws Code103ResponseException
38
     * @return bool
39
     */
40
    public static function parseResponse(Response $response)
41
    {
42
        $jsonData = \GuzzleHttp\json_decode($response->getBody(), true);
43
        if (
44
            (isset($jsonData['errCode']) && $jsonData['errCode'] === 0)
45
            ||(isset($jsonData['retcode']) && $jsonData['retcode'] === 0)
46
        ) {
47
            return true;
48
        }
49
        if ($jsonData['retcode'] === 103) {
50
            throw new Code103ResponseException($response);
51
        }
52
        return false;
53
    }
54
}
55