Passed
Pull Request — master (#1)
by
unknown
02:45
created

PermissionScraper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 26
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 19 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Nelexa\GPlay\Scraper;
5
6
use Nelexa\GPlay\Http\ResponseHandlerInterface;
7
use Nelexa\GPlay\Model\Permission;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
class PermissionScraper implements ResponseHandlerInterface
12
{
13
    /**
14
     * @param RequestInterface $request
15
     * @param ResponseInterface $response
16
     * @return Permission[]
17
     */
18
    public function __invoke(RequestInterface $request, ResponseInterface $response)
19
    {
20
        $contents = substr($response->getBody()->getContents(), 5);
21
        $json = \GuzzleHttp\json_decode($contents, true);
22
        $data = $json[0][2][0][65]['42656262'][1] ?? [];
23
24
        $parsePermissions = static function (array $items) use (&$parsePermissions) {
25
            return array_reduce($items, static function ($results, $item) use (&$parsePermissions) {
26
                if (is_array($item)) {
27
                    if (count($item) === 3 && is_string($item[0]) && is_string($item[1])) {
28
                        $results[] = new Permission($item[0], $item[1]);
29
                    } else {
30
                        $results = array_merge($results, $parsePermissions($item));
31
                    }
32
                }
33
                return $results;
34
            }, []);
35
        };
36
        return $parsePermissions($data);
37
    }
38
}
39