ClusterPagesFromListAppsScraper   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 96.15%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
eloc 30
c 1
b 1
f 0
dl 0
loc 55
ccs 25
cts 26
cp 0.9615
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 46 10
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 Nelexa\GPlay\GPlayApps;
17
use Nelexa\GPlay\HttpClient\ParseHandlerInterface;
18
use Nelexa\GPlay\Model\ClusterPage;
19
use Nelexa\GPlay\Util\ScraperUtil;
20
use Psr\Http\Message\RequestInterface;
21
use Psr\Http\Message\ResponseInterface;
22
23
/**
24
 * @internal
25
 */
26
class ClusterPagesFromListAppsScraper implements ParseHandlerInterface
27
{
28
    /**
29
     * @param RequestInterface  $request
30
     * @param ResponseInterface $response
31
     * @param array             $options
32
     *
33
     * @return array
34
     */
35 8
    public function __invoke(RequestInterface $request, ResponseInterface $response, array &$options = []): array
36
    {
37 8
        $contents = $response->getBody()->getContents();
38 8
        $scriptData = ScraperUtil::extractScriptData($contents);
39
40 8
        $scriptDataInfo = null;
41 8
        $token = null;
42
43 8
        foreach ($scriptData as $data) {
44 8
            if (isset($data[0][1][0][21][0])
45 8
                || isset($data[0][1][1][21][0])
46 8
                || isset($data[0][1][0][22][0])
47 8
                || isset($data[0][1][1][22][0])
48
            ) {
49 8
                $scriptDataInfo = $data[0][1];
50 8
                $token = $data[0][3][1] ?? null;
51 8
                break;
52
            }
53
        }
54
55 8
        if (!$scriptDataInfo) {
56
            return [
57
                'results' => [],
58
                'token' => null,
59
            ];
60
        }
61
62 8
        $results = [];
63
64 8
        foreach ($scriptDataInfo as $item) {
65 8
            if (isset($item[21][1][0], $item[21][1][2][4][2])) {
66 8
                $results[] = new ClusterPage(
67 8
                    trim($item[21][1][0]),
68 8
                    GPlayApps::GOOGLE_PLAY_URL . $item[21][1][2][4][2]
69
                );
70 8
            } elseif (isset($item[22][1][0], $item[22][1][2][4][2])) {
71 1
                $results[] = new ClusterPage(
72 1
                    trim($item[22][1][0]),
73 1
                    GPlayApps::GOOGLE_PLAY_URL . $item[22][1][2][4][2]
74
                );
75
            }
76
        }
77
78
        return [
79 8
            'results' => $results,
80
            'token' => $token,
81
        ];
82
    }
83
}
84