Passed
Push — master ( 39e7c3...a20678 )
by Alexey
04:22
created

AppsExtractor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 96.88%

Importance

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

3 Methods

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