Completed
Push — master ( 45165d...5e44c4 )
by Alexey
04:08 queued 11s
created

ReviewsExtractor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 32
c 1
b 0
f 0
dl 0
loc 71
ccs 25
cts 25
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A extractReviews() 0 9 2
A extractReplyReview() 0 15 4
A extractReview() 0 23 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\Model\AppId;
15
use Nelexa\GPlay\Model\GoogleImage;
16
use Nelexa\GPlay\Model\ReplyReview;
17
use Nelexa\GPlay\Model\Review;
18
use Nelexa\GPlay\Util\DateStringFormatter;
19
20
/**
21
 * @internal
22
 */
23
class ReviewsExtractor
24
{
25
    /**
26
     * @param AppId $requestApp
27
     * @param array $data
28
     *
29
     * @return array
30
     */
31 20
    public static function extractReviews(AppId $requestApp, array $data): array
32
    {
33 20
        $reviews = [];
34
35 20
        foreach ($data as $reviewData) {
36 20
            $reviews[] = self::extractReview($requestApp, $reviewData);
37
        }
38
39 20
        return $reviews;
40
    }
41
42
    /**
43
     * @param AppId $requestApp
44
     * @param       $reviewData
45
     *
46
     * @return Review
47
     */
48 20
    public static function extractReview(AppId $requestApp, $reviewData): Review
49
    {
50 20
        $reviewId = $reviewData[0];
51 20
        $reviewUrl = $requestApp->getUrl() . '&reviewId=' . urlencode($reviewId);
52 20
        $userName = $reviewData[1][0];
53 20
        $avatar = (new GoogleImage($reviewData[1][1][3][2]))->setSize(64);
54 20
        $date = DateStringFormatter::unixTimeToDateTime($reviewData[5][0]);
55 20
        $score = $reviewData[2] ?? 0;
56 20
        $text = (string) ($reviewData[4] ?? '');
57 20
        $likeCount = $reviewData[6];
58
59 20
        $reply = self::extractReplyReview($reviewData);
60
61 20
        return new Review(
62 20
            $reviewId,
63
            $reviewUrl,
64
            $userName,
65
            $text,
66
            $avatar,
67
            $date,
68
            $score,
69
            $likeCount,
70
            $reply
71
        );
72
    }
73
74
    /**
75
     * @param array $reviewData
76
     *
77
     * @return ReplyReview|null
78
     */
79 20
    private static function extractReplyReview(array $reviewData): ?ReplyReview
80
    {
81 20
        if (isset($reviewData[7][1])) {
82 11
            $replyText = $reviewData[7][1];
83 11
            $replyDate = DateStringFormatter::unixTimeToDateTime($reviewData[7][2][0]);
84
85 11
            if ($replyText && $reviewData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $reviewData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
86 11
                return new ReplyReview(
87 11
                    $replyDate,
88
                    $replyText
89
                );
90
            }
91
        }
92
93 20
        return null;
94
    }
95
}
96