Passed
Branch feature/refactoring (13cbf0)
by Alexey
03:46
created

PermissionScraper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 26
c 1
b 0
f 0
dl 0
loc 49
ccs 25
cts 26
cp 0.9615
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 41 5
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author   Ne-Lexa
7
 * @license  MIT
8
 *
9
 * @see      https://github.com/Ne-Lexa/google-play-scraper
10
 */
11
12
namespace Nelexa\GPlay\Scraper;
13
14
use Nelexa\GPlay\Model\GoogleImage;
15
use Nelexa\GPlay\Model\Permission;
16
use Nelexa\HttpClient\ResponseHandlerInterface;
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\ResponseInterface;
19
20
/**
21
 * @internal
22
 */
23
class PermissionScraper implements ResponseHandlerInterface
24
{
25
    /**
26
     * @param RequestInterface  $request
27
     * @param ResponseInterface $response
28
     *
29
     * @return Permission[]
30
     */
31 1
    public function __invoke(RequestInterface $request, ResponseInterface $response)
32
    {
33 1
        $contents = substr($response->getBody()->getContents(), 5);
34 1
        $json = \GuzzleHttp\json_decode($contents, true);
35 1
        $data = \GuzzleHttp\json_decode($json[0][2], true);
36
37
        $permissionMapFn = static function (array $v): string {
38 1
            return (string) $v[1];
39 1
        };
40
41 1
        $permissions = [];
42
43 1
        foreach (\array_slice($data, 0, 2) as $items) {
44 1
            if ($items === null) {
45
                continue;
46
            }
47
48 1
            foreach ($items as $values) {
49 1
                $permissionName = $values[0];
50 1
                $permissions[$permissionName] = [
51 1
                    'name' => $permissionName,
52 1
                    'icon' => new GoogleImage($values[1][3][2]),
53 1
                    'permissions' => array_map($permissionMapFn, $values[2]),
54
                ];
55
            }
56
        }
57
58 1
        if (isset($data[2])) {
59 1
            end($permissions);
60 1
            $lastKey = key($permissions);
61 1
            $permissions[$lastKey]['permissions'] = array_merge(
62 1
                array_map($permissionMapFn, $data[2]),
63 1
                $permissions[$lastKey]['permissions']
64
            );
65
        }
66
67 1
        return array_map(
68
            static function (array $data) {
69 1
                return new Permission($data['name'], $data['icon'], $data['permissions']);
70 1
            },
71 1
            $permissions
72
        );
73
    }
74
}
75