Passed
Push — master ( 0cbcec...cca867 )
by Alexey
04:00 queued 12s
created

AppsExtractor::extractApp()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 40
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 5.005

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 40
ccs 32
cts 34
cp 0.9412
rs 9.0808
cc 5
nc 6
nop 3
crap 5.005
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\Extractor;
15
16
use Nelexa\GPlay\Model\App;
17
use Nelexa\GPlay\Model\GoogleImage;
18
use Nelexa\GPlay\Util\ScraperUtil;
19
20
/**
21
 * @internal
22
 */
23
class AppsExtractor
24
{
25
    /**
26
     * @param array  $data
27
     * @param string $locale
28
     * @param string $country
29
     *
30
     * @return App
31
     */
32 22
    public static function extractApp(array $data, string $locale, string $country): App
33
    {
34 22
        $name = $data[3];
35 22
        $appId = $data[0][0];
36 22
        $icon = new GoogleImage($data[1][3][2]);
37 22
        $developerName = $data[14];
38 22
        $installsText = $data[15];
39 22
        $priceText = null;
40 22
        if (isset($data[8][1][0][0], $data[8][1][0][2]) && $data[8][1][0][0] > 0) {
41 16
            $priceText = $data[8][1][0][2];
42
        }
43 22
        $score = $data[4][1] ?? 0.0;
44 22
        $screenshots = array_values(array_map(static function (array $item): GoogleImage {
45 22
            return new GoogleImage($item[3][2]);
46 22
        }, array_filter($data[2], static function ($item): bool {
47 22
            return isset($item[3][2]);
48
        })));
49 22
        $description = ScraperUtil::html2text($data[13][1] ?? '');
50 22
        $cover = null;
51 22
        if (isset($data[22][3][2])) {
52 22
            $cover = new GoogleImage($data[22][3][2]);
53
        } elseif (isset($data[100][1][0][3][2])) {
54
            $cover = new GoogleImage($data[100][1][0][3][2]);
55
        }
56
//        $categoryName = $data[0][5];
57
58 22
        return App::newBuilder()
59 22
            ->setId($appId)
60 22
            ->setLocale($locale)
61 22
            ->setCountry($country)
62 22
            ->setName($name)
63 22
            ->setDeveloperName($developerName)
64 22
            ->setCover($cover)
65 22
            ->setIcon($icon)
66 22
            ->setInstallsText($installsText)
67 22
            ->setScore($score)
68 22
            ->setPriceText($priceText)
69 22
            ->setScreenshots($screenshots)
70 22
            ->setDescription($description)
71 22
            ->build()
72
        ;
73
    }
74
}
75