Passed
Branch feature/refactoring (13cbf0)
by Alexey
03:46
created

PlayStoreUiAppsScraper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 15
c 1
b 0
f 0
dl 0
loc 35
ccs 15
cts 16
cp 0.9375
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 28
    public function __invoke(RequestInterface $request, ResponseInterface $response): array
33
    {
34 28
        $contents = substr($response->getBody()->getContents(), 5);
35 28
        $json = \GuzzleHttp\json_decode($contents, true);
36
37 28
        if (empty($json[0][2])) {
38 1
            return [[], null];
39
        }
40 28
        $json = \GuzzleHttp\json_decode($json[0][2], true);
41
42 28
        if (empty($json[0][0][0])) {
43
            return [[], null];
44
        }
45
46 28
        $query = parse_query($request->getUri()->getQuery());
47 28
        $locale = $query[GPlayApps::REQ_PARAM_LOCALE] ?? GPlayApps::DEFAULT_LOCALE;
48 28
        $country = $query[GPlayApps::REQ_PARAM_COUNTRY] ?? GPlayApps::DEFAULT_COUNTRY;
49
50 28
        $apps = [];
51
52 28
        foreach ($json[0][0][0] as $data) {
53 28
            $apps[] = AppsExtractor::extractApp($data, $locale, $country);
54
        }
55
56 28
        $nextToken = $json[0][0][7][1] ?? null;
57
58 28
        return [$apps, $nextToken];
59
    }
60
}
61