Passed
Push — master ( dcb648...5ad5c1 )
by Alexey
03:43 queued 16s
created

ClusterAppsScraper::__invoke()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.0042

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 29
ccs 17
cts 18
cp 0.9444
rs 9.3888
cc 5
nc 8
nop 3
crap 5.0042
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (c) Ne-Lexa
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 *
11
 * @see https://github.com/Ne-Lexa/google-play-scraper
12
 */
13
14
namespace Nelexa\GPlay\Scraper;
15
16
use GuzzleHttp\Psr7\Query;
17
use Nelexa\GPlay\GPlayApps;
18
use Nelexa\GPlay\HttpClient\ParseHandlerInterface;
19
use Nelexa\GPlay\Scraper\Extractor\AppsExtractor;
20
use Nelexa\GPlay\Util\ScraperUtil;
21
use Psr\Http\Message\RequestInterface;
22
use Psr\Http\Message\ResponseInterface;
23
24
/**
25
 * @internal
26
 */
27
class ClusterAppsScraper implements ParseHandlerInterface
28
{
29
    /**
30
     * @param RequestInterface  $request
31
     * @param ResponseInterface $response
32
     * @param array             $options
33
     *
34
     * @return array
35
     */
36 10
    public function __invoke(RequestInterface $request, ResponseInterface $response, array &$options = []): array
37
    {
38 10
        $scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents());
39 10
        $scriptDataInfo = null;
40
41 10
        foreach ($scriptData as $value) {
42 10
            if (isset($value[0][1][0][21][0])) {
43 9
                $scriptDataInfo = $value[0][1][0][21];
44 9
                break;
45
            }
46 10
            if (isset($value[0][1][0][22][0])) {
47 4
                $scriptDataInfo = $value[0][1][0][22];
48 4
                break;
49
            }
50
        }
51
52 10
        if ($scriptDataInfo === null) {
53
            return [[], null];
54
        }
55
56 10
        $query = Query::parse($request->getUri()->getQuery());
57 10
        $locale = $query[GPlayApps::REQ_PARAM_LOCALE] ?? GPlayApps::DEFAULT_LOCALE;
58 10
        $country = $query[GPlayApps::REQ_PARAM_COUNTRY] ?? GPlayApps::DEFAULT_COUNTRY;
59
60 10
        $apps = AppsExtractor::extractApps($scriptDataInfo[0], $locale, $country);
61
62 10
        $nextToken = $scriptDataInfo[1][3][1] ?? null;
63
64 10
        return [$apps, $nextToken];
65
    }
66
}
67