Completed
Push — master ( 45165d...5e44c4 )
by Alexey
04:08 queued 11s
created

FindDevAppsUrlScraper::__invoke()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.3073

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 24
ccs 10
cts 13
cp 0.7692
rs 9.5555
cc 5
nc 9
nop 2
crap 5.3073
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\Exception\GooglePlayException;
15
use Nelexa\GPlay\GPlayApps;
16
use Nelexa\GPlay\Util\ScraperUtil;
17
use Nelexa\HttpClient\ResponseHandlerInterface;
18
use Psr\Http\Message\RequestInterface;
19
use Psr\Http\Message\ResponseInterface;
20
21
/**
22
 * @internal
23
 */
24
class FindDevAppsUrlScraper implements ResponseHandlerInterface
25
{
26
    /**
27
     * @param RequestInterface  $request
28
     * @param ResponseInterface $response
29
     *
30
     * @throws GooglePlayException
31
     *
32
     * @return string|null
33
     */
34 1
    public function __invoke(RequestInterface $request, ResponseInterface $response): ?string
35
    {
36 1
        $scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents());
37
38 1
        $scriptDataApps = null;
39
40 1
        foreach ($scriptData as $key => $scriptValue) {
41 1
            if (isset($scriptValue[0][1][0][0][3][4][2])) { // ds:3
42 1
                $scriptDataApps = $scriptValue;
43 1
                break;
44
            }
45
        }
46
47 1
        if ($scriptDataApps === null) {
48
            throw (new GooglePlayException('Error fetch cluster page'))
49
                ->setUrl($request->getUri()->__toString())
50
            ;
51
        }
52
53 1
        if (isset($scriptDataApps[0][1][0][0][3][4][2])) {
54 1
            return GPlayApps::GOOGLE_PLAY_URL . $scriptDataApps[0][1][0][0][3][4][2];
55
        }
56
57
        return null;
58
    }
59
}
60