GetGroupDetailRequest::parseResponse()   B
last analyzed

Complexity

Conditions 9
Paths 5

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 5
nop 1
dl 0
loc 51
rs 7.5135
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 GuzzleHttp\Psr7\Response;
15
use Slince\SmartQQ\Credential;
16
use Slince\SmartQQ\Entity\GroupDetail;
17
use Slince\SmartQQ\Entity\GroupMember;
18
use Slince\SmartQQ\EntityCollection;
19
use Slince\SmartQQ\EntityFactory;
20
use Slince\SmartQQ\Exception\ResponseException;
21
use Slince\SmartQQ\Entity\Group;
22
23
class GetGroupDetailRequest extends Request
24
{
25
    protected $uri = 'http://s.web2.qq.com/api/get_group_info_ext2?gcode={groupcode}&vfwebqq={vfwebqq}&t=0.1';
26
27
    protected $referer = 'http://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1';
28
29
    public function __construct(Group $group, Credential $credential)
30
    {
31
        $this->setTokens([
32
            'groupcode' => $group->getCode(),
33
            'vfwebqq' => $credential->getVfWebQQ(),
34
        ]);
35
    }
36
37
    /**
38
     * 解析响应数据.
39
     *
40
     * @param Response $response
41
     *
42
     * @return GroupDetail
43
     */
44
    public static function parseResponse(Response $response)
45
    {
46
        $jsonData = \GuzzleHttp\json_decode($response->getBody(), true);
47
        if ($jsonData && 0 == $jsonData['retcode']) {
48
            //群成员的vip信息
49
            $vipInfos = (new Collection($jsonData['result']['vipinfo']))->combine('u', function ($entity) {
50
                return $entity;
51
            })->toArray();
52
            //群成员的名片信息, 如果全部成员都没有设置群名片则会不存在这个字段
53
            $cards = isset($jsonData['result']['cards']) ? (new Collection($jsonData['result']['cards']))
54
                ->combine('muin', 'card')
55
                ->toArray() : [];
56
            //群成员的简要信息
57
            $flags = (new Collection($jsonData['result']['ginfo']['members']))->combine('muin', 'mflag')
58
                ->toArray();
59
            //群基本详细信息
60
            $groupData = $jsonData['result']['ginfo'];
61
            $groupDetailData = [
62
                'gid' => $groupData['gid'],
63
                'name' => $groupData['name'],
64
                'code' => $groupData['code'],
65
                'owner' => $groupData['owner'],
66
                'level' => $groupData['level'],
67
                'createTime' => $groupData['createtime'],
68
                'flag' => $groupData['flag'],
69
                'memo' => $groupData['memo'],
70
                'members' => null,
71
            ];
72
            $members = [];
73
            foreach ($jsonData['result']['minfo'] as $memberData) {
74
                $uin = $memberData['uin'];
75
                $member = EntityFactory::createEntity(GroupMember::class, [
76
                    'flag' => isset($flags[$uin]) ? $flags[$uin] : null,
77
                    'nick' => $memberData['nick'],
78
                    'province' => $memberData['province'],
79
                    'gender' => $memberData['gender'],
80
                    'uin' => $uin,
81
                    'country' => $memberData['country'],
82
                    'city' => $memberData['city'],
83
                    'card' => isset($cards[$uin]) ? $cards[$uin] : null,
84
                    'isVip' => isset($vipInfos[$uin]) ? 1 == $vipInfos[$uin]['is_vip'] : false,
85
                    'vipLevel' => isset($vipInfos[$uin]) ? $vipInfos[$uin]['vip_level'] : 0,
86
                ]);
87
                $members[] = $member;
88
            }
89
            $groupDetailData['members'] = new EntityCollection($members);
90
91
            return EntityFactory::createEntity(GroupDetail::class, $groupDetailData);
92
        }
93
        throw new ResponseException($jsonData['retcode'], $response);
94
    }
95
}
96