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

DeveloperInfoScraper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 7
eloc 36
c 0
b 0
f 0
dl 0
loc 73
ccs 30
cts 36
cp 0.8333
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getScriptDataInfo() 0 23 4
A __invoke() 0 28 3
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\Exception\GooglePlayException;
18
use Nelexa\GPlay\GPlayApps;
19
use Nelexa\GPlay\HttpClient\ParseHandlerInterface;
20
use Nelexa\GPlay\Model\Developer;
21
use Nelexa\GPlay\Model\GoogleImage;
22
use Nelexa\GPlay\Util\ScraperUtil;
23
use Psr\Http\Message\RequestInterface;
24
use Psr\Http\Message\ResponseInterface;
25
26
/**
27
 * @internal
28
 */
29
class DeveloperInfoScraper implements ParseHandlerInterface
30
{
31
    /**
32
     * @param RequestInterface  $request
33
     * @param ResponseInterface $response
34
     * @param array             $options
35
     *
36
     * @throws \Nelexa\GPlay\Exception\GooglePlayException
37
     *
38
     * @return \Nelexa\GPlay\Model\Developer
39
     */
40 2
    public function __invoke(RequestInterface $request, ResponseInterface $response, array &$options = []): Developer
41
    {
42 2
        $query = Query::parse($request->getUri()->getQuery());
43 2
        $developerId = $query[GPlayApps::REQ_PARAM_ID];
44 2
        $url = (string) $request->getUri()->withQuery(http_build_query([GPlayApps::REQ_PARAM_ID => $developerId]));
45
46 2
        $scriptDataInfo = $this->getScriptDataInfo($request, $response);
47
48 2
        $name = $scriptDataInfo[0][0][0];
49
50 2
        $cover = empty($scriptDataInfo[0][9][0][3][2])
51
            ? null
52 2
            : new GoogleImage($scriptDataInfo[0][9][0][3][2]);
53 2
        $icon = empty($scriptDataInfo[0][9][1][3][2])
54
            ? null
55 2
            : new GoogleImage($scriptDataInfo[0][9][1][3][2]);
56 2
        $developerSite = $scriptDataInfo[0][9][2][0][5][2] ?? null;
57 2
        $description = $scriptDataInfo[0][10][1][1] ?? '';
58
59 2
        return new Developer(
60 2
            Developer::newBuilder()
61 2
                ->setId($developerId)
62 2
                ->setUrl($url)
63 2
                ->setName($name)
64 2
                ->setDescription($description)
65 2
                ->setWebsite($developerSite)
66 2
                ->setIcon($icon)
67 2
                ->setCover($cover)
68
        );
69
    }
70
71
    /**
72
     * @param RequestInterface  $request
73
     * @param ResponseInterface $response
74
     *
75
     * @throws GooglePlayException
76
     *
77
     * @return array
78
     */
79 2
    private function getScriptDataInfo(RequestInterface $request, ResponseInterface $response): array
80
    {
81 2
        $scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents());
82
83 2
        $scriptDataInfo = null;
84
85 2
        foreach ($scriptData as $scriptValue) {
86 2
            if (isset($scriptValue[0][21])) {
87 2
                $scriptDataInfo = $scriptValue; // ds:5
88 2
                break;
89
            }
90
        }
91
92 2
        if ($scriptDataInfo === null) {
93
            throw (new GooglePlayException(
94
                sprintf(
95
                    'Error parse vendor page %s. Need update library.',
96
                    $request->getUri()
97
                )
98
            ))->setUrl($request->getUri()->__toString());
99
        }
100
101 2
        return $scriptDataInfo;
102
    }
103
}
104