GetGroupsRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A parseResponse() 0 19 5
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\Entity\Group;
16
use Slince\SmartQQ\EntityCollection;
17
use Slince\SmartQQ\Credential;
18
use Slince\SmartQQ\EntityFactory;
19
use Slince\SmartQQ\Exception\ResponseException;
20
use Slince\SmartQQ\Utils;
21
22
class GetGroupsRequest extends Request
23
{
24
    protected $uri = 'http://s.web2.qq.com/api/get_group_name_list_mask2';
25
26
    protected $referer = 'http://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2';
27
28
    protected $method = RequestInterface::REQUEST_METHOD_POST;
29
30
    public function __construct(Credential $credential)
31
    {
32
        $this->setParameter('r', \GuzzleHttp\json_encode([
33
            'vfwebqq' => $credential->getVfWebQQ(),
34
            'hash' => Utils::hash($credential->getUin(), $credential->getPtWebQQ()),
35
        ]));
36
    }
37
38
    /**
39
     * 解析响应数据.
40
     *
41
     * @param Response $response
42
     *
43
     * @return EntityCollection
44
     */
45
    public static function parseResponse(Response $response)
46
    {
47
        $jsonData = \GuzzleHttp\json_decode($response->getBody(), true);
48
        if ($jsonData && 0 == $jsonData['retcode']) {
49
            $markNames = (new Collection($jsonData['result']['gmarklist']))->combine('uin', 'markname')
50
                ->toArray();
51
            $groups = [];
52
            foreach ($jsonData['result']['gnamelist'] as $groupData) {
53
                $groupId = $groupData['gid'];
54
                $groupData['id'] = $groupData['gid'];
55
                $groupData['markName'] = isset($markNames[$groupId]) ? $markNames[$groupId] : '';
56
                $group = EntityFactory::createEntity(Group::class, $groupData);
57
                $groups[] = $group;
58
            }
59
60
            return new EntityCollection($groups);
61
        }
62
        throw new ResponseException($jsonData['retcode'], $response);
63
    }
64
}
65