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

PlayStoreUiAppsScraper::__invoke()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0312

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 27
ccs 14
cts 16
cp 0.875
rs 9.7666
cc 4
nc 4
nop 2
crap 4.0312
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