Passed
Branch develop (61f351)
by Alexey
01:52
created

ReviewsExtractor::extractReviews()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 24
nc 3
nop 2
dl 0
loc 31
rs 9.536
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Nelexa\GPlay\Scraper\Extractor;
5
6
use Nelexa\GPlay\Model\GoogleImage;
7
use Nelexa\GPlay\Model\ReplyReview;
8
use Nelexa\GPlay\Model\Review;
9
use Nelexa\GPlay\Request\RequestApp;
10
use Nelexa\GPlay\Util\DateStringFormatter;
11
12
class ReviewsExtractor
13
{
14
    /**
15
     * @param RequestApp $requestApp
16
     * @param array $data
17
     * @return array
18
     */
19
    public static function extractReviews(RequestApp $requestApp, array $data): array
20
    {
21
        $reviews = [];
22
        foreach ($data as $reviewData) {
23
            $reviewId = $reviewData[0];
24
            $reviewUrl = $requestApp->getUrl() . '&reviewId=' . urlencode($reviewId);
25
            $userName = $reviewData[1][0];
26
            $avatar = new GoogleImage($reviewData[1][1][3][2]);
27
            $date = null;
28
            if (isset($reviewData[5][0])) {
29
                $date = DateStringFormatter::unixTimeToDateTime($reviewData[5][0]);
30
            }
31
            $score = $reviewData[2] ?? 0;
32
            $text = (string)($reviewData[4] ?? '');
33
            $likeCount = $reviewData[6];
34
35
            $reply = self::extractReplyReview($reviewData);
36
37
            $reviews[] = new Review(
38
                $reviewId,
39
                $reviewUrl,
40
                $userName,
41
                $text,
42
                $avatar,
43
                $date,
44
                $score,
45
                $likeCount,
46
                $reply
47
            );
48
        }
49
        return $reviews;
50
    }
51
52
    /**
53
     * @param array $reviewData
54
     * @return ReplyReview|null
55
     */
56
    private static function extractReplyReview(array $reviewData): ?ReplyReview
57
    {
58
        if (isset($reviewData[7][1])) {
59
            $replyText = $reviewData[7][1];
60
            $replyDate = DateStringFormatter::unixTimeToDateTime($reviewData[7][2][0]);
61
            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...
62
                return new ReplyReview(
63
                    $replyDate,
64
                    $replyText
65
                );
66
            }
67
        }
68
        return null;
69
    }
70
}
71