Passed
Push — master ( 5e44c4...8c6c38 )
by Alexey
03:59 queued 11s
created

AppsExtractor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 29
c 1
b 0
f 0
dl 0
loc 62
ccs 32
cts 32
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extractDeveloper() 0 11 1
A extractApp() 0 21 1
A extractSummary() 0 5 2
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\Extractor;
13
14
use Nelexa\GPlay\GPlayApps;
15
use Nelexa\GPlay\Model\App;
16
use Nelexa\GPlay\Model\Developer;
17
use Nelexa\GPlay\Model\GoogleImage;
18
use Nelexa\GPlay\Util\ScraperUtil;
19
use function GuzzleHttp\Psr7\parse_query;
20
21
/**
22
 * @internal
23
 */
24
class AppsExtractor
25
{
26
    /**
27
     * @param array  $data
28
     * @param string $locale
29
     * @param string $country
30
     *
31
     * @return App
32
     */
33 28
    public static function extractApp(array $data, string $locale, string $country): App
34
    {
35 28
        $name = $data[2];
36 28
        $appId = $data[12][0];
37 28
        $icon = new GoogleImage($data[1][1][0][3][2]);
38 28
        $developer = self::extractDeveloper($data);
39 28
        $price = $data[7][0][3][2][1][0][2] ?? null;
40 28
        $summary = self::extractSummary($data);
41 28
        $score = $data[6][0][2][1][1] ?? 0.0;
42
43 28
        return App::newBuilder()
44 28
            ->setId($appId)
45 28
            ->setLocale($locale)
46 28
            ->setCountry($country)
47 28
            ->setName($name)
48 28
            ->setSummary($summary)
49 28
            ->setDeveloper($developer)
50 28
            ->setIcon($icon)
51 28
            ->setScore($score)
52 28
            ->setPriceText($price)
53 28
            ->build()
54
        ;
55
    }
56
57
    /**
58
     * @param array $data
59
     *
60
     * @return Developer
61
     */
62 28
    private static function extractDeveloper(array $data): Developer
63
    {
64 28
        $developerName = $data[4][0][0][0];
65 28
        $developerPage = GPlayApps::GOOGLE_PLAY_URL . $data[4][0][0][1][4][2];
66 28
        $developerId = parse_query(parse_url($developerPage, \PHP_URL_QUERY))[GPlayApps::REQ_PARAM_ID];
67
68 28
        return new Developer(
69 28
            Developer::newBuilder()
70 28
                ->setId($developerId)
71 28
                ->setUrl($developerPage)
72 28
                ->setName($developerName)
73
        );
74
    }
75
76
    /**
77
     * @param $scriptDataInfo
78
     *
79
     * @return string|null
80
     */
81 28
    private static function extractSummary(array $scriptDataInfo): ?string
82
    {
83 28
        return empty($scriptDataInfo[4][1][1][1][1]) ?
84 2
            null :
85 28
            ScraperUtil::html2text($scriptDataInfo[4][1][1][1][1]);
86
    }
87
}
88