Passed
Push — master ( f86ea9...d8c162 )
by Alexey
09:45
created

FindSimilarAppsUrlScraper::__invoke()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.0208

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 21
ccs 11
cts 12
cp 0.9167
rs 9.2222
cc 6
nc 6
nop 3
crap 6.0208
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\AppId;
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 FindSimilarAppsUrlScraper implements ParseHandlerInterface
27
{
28
    /** @var AppId */
29
    private $appId;
30
31
    /**
32
     * SimilarScraper constructor.
33
     *
34
     * @param AppId $appId
35
     */
36 2
    public function __construct(AppId $appId)
37
    {
38 2
        $this->appId = $appId;
39
    }
40
41
    /**
42
     * @param RequestInterface  $request
43
     * @param ResponseInterface $response
44
     * @param array             $options
45
     *
46
     * @return string|null
47
     */
48 2
    public function __invoke(RequestInterface $request, ResponseInterface $response, array &$options = []): ?string
49
    {
50 2
        $scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents());
51 2
        $url = null;
52
53 2
        foreach ($scriptData as $value) {
54
            if (
55 2
                isset($value[1][1][1][21][1][2][4][2])
56 2
                && \is_string($value[1][1][1][21][1][2][4][2])
57 2
                && strpos($value[1][1][1][21][1][2][4][2], 'cluster') !== false
58
            ) {
59 2
                $url = $value[1][1][1][21][1][2][4][2];
60 2
                break;
61
            }
62
        }
63
64 2
        if ($url !== null) {
65 2
            return GPlayApps::GOOGLE_PLAY_URL . $url;
66
        }
67
68
        return null;
69
    }
70
}
71