Passed
Push — master ( 641929...562012 )
by Alexey
06:04 queued 12s
created

ClusterAppsScraper::__invoke()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.0073

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 29
ccs 16
cts 17
cp 0.9412
rs 9.1111
cc 6
nc 9
nop 3
crap 6.0073
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 17
    public function __invoke(RequestInterface $request, ResponseInterface $response, array &$options = []): array
37
    {
38 17
        $scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents());
39 17
        $scriptDataInfo = null;
40
41 17
        foreach ($scriptData as $scriptValue) {
42 17
            if (isset($scriptValue[0][1][0][0][0]) && \is_array($scriptValue[0][1][0][0][0])) {
43 17
                $scriptDataInfo = $scriptValue; // ds:3
44 17
                break;
45
            }
46
        }
47
48 17
        if ($scriptDataInfo === null) {
49
            return [[], null];
50
        }
51
52 17
        $query = Query::parse($request->getUri()->getQuery());
53 17
        $locale = $query[GPlayApps::REQ_PARAM_LOCALE] ?? GPlayApps::DEFAULT_LOCALE;
54 17
        $country = $query[GPlayApps::REQ_PARAM_COUNTRY] ?? GPlayApps::DEFAULT_COUNTRY;
55
56 17
        $apps = [];
57
58 17
        foreach ($scriptDataInfo[0][1][0][0][0] as $data) {
59 17
            $apps[] = AppsExtractor::extractApp($data, $locale, $country);
60
        }
61
62 17
        $nextToken = $scriptDataInfo[0][1][0][0][7][1] ?? null;
63
64 17
        return [$apps, $nextToken];
65
    }
66
}
67