Passed
Pull Request — master (#4)
by
unknown
07:24
created

PlayStoreUiAppsScraper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
c 1
b 0
f 0
dl 0
loc 35
ccs 14
cts 16
cp 0.875
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 27 4
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\GPlayApps;
15
use Nelexa\GPlay\Scraper\Extractor\AppsExtractor;
16
use Nelexa\HttpClient\ResponseHandlerInterface;
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use function GuzzleHttp\Psr7\parse_query;
20
21
/**
22
 * @internal
23
 */
24
class PlayStoreUiAppsScraper implements ResponseHandlerInterface
25
{
26
    /**
27
     * @param RequestInterface  $request
28
     * @param ResponseInterface $response
29
     *
30
     * @return array
31
     */
32 29
    public function __invoke(RequestInterface $request, ResponseInterface $response): array
33
    {
34 29
        $contents = substr($response->getBody()->getContents(), 5);
35 29
        $json = \GuzzleHttp\json_decode($contents, true);
36
37 29
        if (empty($json[0][2])) {
38
            return [[], null];
39
        }
40 29
        $json = \GuzzleHttp\json_decode($json[0][2], true);
41
42 29
        if (empty($json[0][0][0])) {
43
            return [[], null];
44
        }
45
46 29
        $query = parse_query($request->getUri()->getQuery());
47 29
        $locale = $query[GPlayApps::REQ_PARAM_LOCALE] ?? GPlayApps::DEFAULT_LOCALE;
48 29
        $country = $query[GPlayApps::REQ_PARAM_COUNTRY] ?? GPlayApps::DEFAULT_COUNTRY;
49
50 29
        $apps = [];
51
52 29
        foreach ($json[0][0][0] as $data) {
53 29
            $apps[] = AppsExtractor::extractApp($data, $locale, $country);
54
        }
55
56 29
        $nextToken = $json[0][0][7][1] ?? null;
57
58 29
        return [$apps, $nextToken];
59
    }
60
}
61