Passed
Branch develop (61f351)
by Alexey
01:52
created

PlayStoreUiAppsScraper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 21 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Nelexa\GPlay\Scraper;
5
6
use Nelexa\GPlay\GPlayApps;
7
use Nelexa\GPlay\Http\ResponseHandlerInterface;
8
use Nelexa\GPlay\Scraper\Extractor\AppsExtractor;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use function GuzzleHttp\Psr7\parse_query;
12
13
class PlayStoreUiAppsScraper implements ResponseHandlerInterface
14
{
15
    /**
16
     * @param RequestInterface $request
17
     * @param ResponseInterface $response
18
     * @return array
19
     */
20
    public function __invoke(RequestInterface $request, ResponseInterface $response): array
21
    {
22
        $contents = substr($response->getBody()->getContents(), 5);
23
        $json = \GuzzleHttp\json_decode($contents, true);
24
        if (empty($json[0][2])) {
25
            return [[], null];
26
        }
27
        $json = \GuzzleHttp\json_decode($json[0][2], true);
28
        if (empty($json[0][0][0])) {
29
            return [[], null];
30
        }
31
32
        $locale = parse_query($request->getUri()->getQuery())[GPlayApps::REQ_PARAM_LOCALE] ?? GPlayApps::DEFAULT_LOCALE;
33
34
        $apps = [];
35
        foreach ($json[0][0][0] as $data) {
36
            $apps[] = AppsExtractor::extractApp($data, $locale);
37
        }
38
39
        $nextToken = $json[0][0][7][1] ?? null;
40
        return [$apps, $nextToken];
41
    }
42
}
43