GetLnickRequest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 35
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 12 4
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 Cake\Collection\Collection;
14
use Slince\SmartQQ\Credential;
15
use Slince\SmartQQ\Entity\Friend;
16
use GuzzleHttp\Psr7\Response;
17
use Slince\SmartQQ\Exception\ResponseException;
18
19
class GetLnickRequest extends Request
20
{
21
    protected $uri = 'http://s.web2.qq.com/api/get_single_long_nick2?tuin={uin}&vfwebqq={vfwebqq}&t=0.1';
22
23
    protected $referer = 'http://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1';
24
25
    public function __construct(Friend $friend, Credential $credential)
26
    {
27
        $this->setTokens([
28
            'uin' => $friend->getUin(),
29
            'vfwebqq' => $credential->getVfWebQQ(),
30
        ]);
31
    }
32
33
    /**
34
     * 解析响应数据.
35
     *
36
     * @param Response $response
37
     * @param Friend   $friend
38
     *
39
     * @return int
40
     */
41
    public static function parseResponse(Response $response, Friend $friend)
42
    {
43
        $jsonData = \GuzzleHttp\json_decode($response->getBody(), true);
44
        if ($jsonData && 0 == $jsonData['retcode']) {
45
            $info = (new Collection($jsonData['result']))->filter(function ($info) use ($friend) {
46
                return $info['uin'] == $friend->getUin();
47
            })->first();
48
49
            return $info ? $info['lnick'] : null;
50
        }
51
        throw new ResponseException($jsonData['retcode'], $response);
52
    }
53
}
54