Passed
Push — master ( d3bea1...2962a0 )
by Alexey
03:34
created

PlayStoreUiAppsScraper::__invoke()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 27
ccs 15
cts 16
cp 0.9375
rs 9.7666
cc 4
nc 4
nop 2
crap 4.0039
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 15
    public function __invoke(RequestInterface $request, ResponseInterface $response): array
33
    {
34 15
        $contents = substr($response->getBody()->getContents(), 5);
35 15
        $json = \GuzzleHttp\json_decode($contents, true);
36
37 15
        if (empty($json[0][2])) {
38 1
            return [[], null];
39
        }
40 15
        $json = \GuzzleHttp\json_decode($json[0][2], true);
41
42 15
        if (empty($json[0][0][0])) {
43
            return [[], null];
44
        }
45
46 15
        $query = parse_query($request->getUri()->getQuery());
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Psr7\parse_query() has been deprecated: parse_query will be removed in guzzlehttp/psr7:2.0. Use Query::parse instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

46
        $query = /** @scrutinizer ignore-deprecated */ parse_query($request->getUri()->getQuery());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
47 15
        $locale = $query[GPlayApps::REQ_PARAM_LOCALE] ?? GPlayApps::DEFAULT_LOCALE;
48 15
        $country = $query[GPlayApps::REQ_PARAM_COUNTRY] ?? GPlayApps::DEFAULT_COUNTRY;
49
50 15
        $apps = [];
51
52 15
        foreach ($json[0][0][0] as $data) {
53 15
            $apps[] = AppsExtractor::extractApp($data, $locale, $country);
54
        }
55
56 15
        $nextToken = $json[0][0][7][1] ?? null;
57
58 15
        return [$apps, $nextToken];
59
    }
60
}
61