Completed
Branch develop (45165d)
by Alexey
04:09
created

PermissionScraper::__invoke()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
rs 9.5555
c 0
b 0
f 0
cc 5
nc 1
nop 2
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