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