GetFriendsRequest::parseResponse()   B
last analyzed

Complexity

Conditions 9
Paths 26

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 26
nop 1
dl 0
loc 54
rs 7.448
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Category;
16
use Slince\SmartQQ\Entity\Friend;
17
use Slince\SmartQQ\EntityCollection;
18
use Slince\SmartQQ\EntityFactory;
19
use Slince\SmartQQ\Exception\ResponseException;
20
use GuzzleHttp\Psr7\Response;
21
use Slince\SmartQQ\Utils;
22
23
class GetFriendsRequest extends Request
24
{
25
    protected $uri = 'http://s.web2.qq.com/api/get_user_friends2';
26
27
    protected $referer = 'http://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1';
28
29
    protected $method = RequestInterface::REQUEST_METHOD_POST;
30
31
    public function __construct(Credential $credential)
32
    {
33
        $this->setParameter('r', \GuzzleHttp\json_encode([
34
            'vfwebqq' => $credential->getVfWebQQ(),
35
            'hash' => Utils::hash($credential->getUin(), $credential->getPtWebQQ()),
36
        ]));
37
    }
38
39
    /**
40
     * 解析响应数据.
41
     *
42
     * @param Response $response
43
     *
44
     * @return EntityCollection
45
     */
46
    public static function parseResponse(Response $response)
47
    {
48
        $jsonData = \GuzzleHttp\json_decode($response->getBody(), true);
49
        //有时候获取好友接口retcode=100003时也可以获取数据,但数据不完整故当做无效返回
50
        if ($jsonData && 0 == $jsonData['retcode']) {
51
            //好友基本信息
52
            $friendDatas = (new Collection($jsonData['result']['friends']))->combine('uin', function ($entity) {
53
                return $entity;
54
            })->toArray();
55
            //markNames
56
            $markNames = (new Collection($jsonData['result']['marknames']))->combine('uin', function ($entity) {
57
                return $entity;
58
            })->toArray();
59
            //分类
60
            $categories = (new Collection($jsonData['result']['categories']))->combine('index', function ($entity) {
61
                return $entity;
62
            })->toArray();
63
            //vip信息
64
            $vipInfos = (new Collection($jsonData['result']['vipinfo']))->combine('u', function ($entity) {
65
                return $entity;
66
            })->toArray();
67
            $friends = [];
68
            foreach ($jsonData['result']['info'] as $friendData) {
69
                $uin = $friendData['uin'];
70
                $friend = [
71
                    'uin' => $friendData['uin'],
72
                    'flag' => $friendData['flag'],
73
                    'face' => $friendData['face'],
74
                    'nick' => $friendData['nick'],
75
                    'markName' => isset($markNames[$uin]) ? $markNames[$uin]['markname'] : null,
76
                    'isVip' => isset($vipInfos[$uin]) ? 1 == $vipInfos[$uin]['is_vip'] : false,
77
                    'vipLevel' => isset($vipInfos[$uin]) ? $vipInfos[$uin]['vip_level'] : 0,
78
                ];
79
                $category = null;
80
                if (isset($friendDatas[$uin])) {
81
                    $categoryIndex = $friendDatas[$uin]['categories'];
82
                    if (0 == $categoryIndex) {
83
                        $category = Category::createMyFriendCategory();
84
                    } else {
85
                        $category = new Category(
86
                            $categories[$categoryIndex]['name'],
87
                            $categories[$categoryIndex]['index'],
88
                            $categories[$categoryIndex]['sort']
89
                        );
90
                    }
91
                }
92
                $friend['category'] = $category;
93
                $friends[] = EntityFactory::createEntity(Friend::class, $friend);
94
            }
95
96
            return new EntityCollection($friends);
97
        }
98
        throw new ResponseException($jsonData['retcode'], $response);
99
    }
100
}
101