Passed
Branch master (8dda47)
by Taosikai
28:32 queued 13:47
created

GetLnickRequest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A parseResponse() 0 11 4
1
<?php
2
/**
3
 * SmartQQ Library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\SmartQQ\Request;
7
8
use Cake\Collection\Collection;
9
use Slince\SmartQQ\Credential;
10
use Slince\SmartQQ\Entity\Friend;
11
use GuzzleHttp\Psr7\Response;
12
use Slince\SmartQQ\Exception\ResponseException;
13
14
class GetLnickRequest extends Request
15
{
16
    protected $uri = 'http://s.web2.qq.com/api/get_single_long_nick2?tuin={uin}&vfwebqq={vfwebqq}&t=0.1';
17
18
    protected $referer = 'http://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1';
19
20
    public function __construct(Friend $friend, Credential $credential)
21
    {
22
        $this->setTokens([
23
            'uin' => $friend->getUin(),
24
            'vfwebqq' => $credential->getVfWebQQ()
25
        ]);
26
    }
27
28
    /**
29
     * 解析响应数据
30
     * @param Response $response
31
     * @param Friend $friend
32
     * @return int
33
     */
34
    public static function parseResponse(Response $response, Friend $friend)
35
    {
36
        $jsonData = \GuzzleHttp\json_decode($response->getBody(), true);
37
        if ($jsonData && $jsonData['retcode'] == 0) {
38
            $info = (new Collection($jsonData['result']))->filter(function($info) use($friend){
39
                return $info['uin'] == $friend->getUin();
40
            })->first();
41
            return $info ? $info['lnick'] : null;
42
        }
43
        throw new ResponseException($jsonData['retcode'], $response);
44
    }
45
}
46