Passed
Push — master ( bb114b...6dc021 )
by Alexey
03:23 queued 10s
created

DeveloperInfoScraper::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 3.0059

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 28
ccs 21
cts 23
cp 0.913
rs 9.568
cc 3
nc 4
nop 2
crap 3.0059
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\Model\Developer;
17
use Nelexa\GPlay\Model\GoogleImage;
18
use Nelexa\GPlay\Util\ScraperUtil;
19
use Nelexa\HttpClient\ResponseHandlerInterface;
20
use Psr\Http\Message\RequestInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use function GuzzleHttp\Psr7\parse_query;
23
24
/**
25
 * @internal
26
 */
27
class DeveloperInfoScraper implements ResponseHandlerInterface
28
{
29
    /**
30
     * @param RequestInterface  $request
31
     * @param ResponseInterface $response
32
     *
33
     * @throws GooglePlayException
34
     *
35
     * @return mixed
36
     */
37 2
    public function __invoke(RequestInterface $request, ResponseInterface $response)
38
    {
39 2
        $query = parse_query($request->getUri()->getQuery());
0 ignored issues
show
Deprecated Code introduced by
The function GuzzleHttp\Psr7\parse_query() has been deprecated: parse_query will be removed in guzzlehttp/psr7:2.0. Use Query::parse instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

39
        $query = /** @scrutinizer ignore-deprecated */ parse_query($request->getUri()->getQuery());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
40 2
        $developerId = $query[GPlayApps::REQ_PARAM_ID];
41 2
        $url = (string) $request->getUri()->withQuery(http_build_query([GPlayApps::REQ_PARAM_ID => $developerId]));
42
43 2
        $scriptDataInfo = $this->getScriptDataInfo($request, $response);
44
45 2
        $name = $scriptDataInfo[0][0][0];
46
47 2
        $cover = empty($scriptDataInfo[0][9][0][3][2]) ?
48
            null :
49 2
            new GoogleImage($scriptDataInfo[0][9][0][3][2]);
50 2
        $icon = empty($scriptDataInfo[0][9][1][3][2]) ?
51
            null :
52 2
            new GoogleImage($scriptDataInfo[0][9][1][3][2]);
53 2
        $developerSite = $scriptDataInfo[0][9][2][0][5][2] ?? null;
54 2
        $description = $scriptDataInfo[0][10][1][1] ?? '';
55
56 2
        return new Developer(
57 2
            Developer::newBuilder()
58 2
                ->setId($developerId)
59 2
                ->setUrl($url)
60 2
                ->setName($name)
61 2
                ->setDescription($description)
62 2
                ->setWebsite($developerSite)
63 2
                ->setIcon($icon)
64 2
                ->setCover($cover)
65
        );
66
    }
67
68
    /**
69
     * @param RequestInterface  $request
70
     * @param ResponseInterface $response
71
     *
72
     * @throws GooglePlayException
73
     *
74
     * @return array
75
     */
76 2
    private function getScriptDataInfo(RequestInterface $request, ResponseInterface $response): array
77
    {
78 2
        $scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents());
79
80 2
        $scriptDataInfo = null;
81
82 2
        foreach ($scriptData as $key => $scriptValue) {
83 2
            if (isset($scriptValue[0][21])) {
84 2
                $scriptDataInfo = $scriptValue; // ds:5
85
                break;
86
            }
87
        }
88
89 2
        if ($scriptDataInfo === null) {
90
            throw (new GooglePlayException(
91
                sprintf(
92
                    'Error parse vendor page %s. Need update library.',
93
                    $request->getUri()
94
                )
95
            ))->setUrl($request->getUri()->__toString());
96
        }
97
98 2
        return $scriptDataInfo;
99
    }
100
}
101