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

ReviewsScraper   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 13
c 1
b 0
f 0
dl 0
loc 38
ccs 12
cts 14
cp 0.8571
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 17 3
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;
13
14
use Nelexa\GPlay\Model\AppId;
15
use Nelexa\GPlay\Scraper\Extractor\ReviewsExtractor;
16
use Nelexa\HttpClient\ResponseHandlerInterface;
17
use Psr\Http\Message\RequestInterface;
18
use Psr\Http\Message\ResponseInterface;
19
20
/**
21
 * @internal
22
 */
23
class ReviewsScraper implements ResponseHandlerInterface
24
{
25
    /** @var AppId */
26
    private $requestApp;
27
28
    /**
29
     * ReviewsScraper constructor.
30
     *
31
     * @param AppId $requestApp
32
     */
33 1
    public function __construct(AppId $requestApp)
34
    {
35 1
        $this->requestApp = $requestApp;
36 1
    }
37
38
    /**
39
     * @param RequestInterface  $request
40
     * @param ResponseInterface $response
41
     *
42
     * @return array
43
     */
44 1
    public function __invoke(RequestInterface $request, ResponseInterface $response)
45
    {
46 1
        $contents = substr($response->getBody()->getContents(), 5);
47 1
        $json = \GuzzleHttp\json_decode($contents, true);
48
49 1
        if (!isset($json[0][2])) {
50
            return [[], null];
51
        }
52 1
        $json = \GuzzleHttp\json_decode($json[0][2], true);
53
54 1
        if (empty($json[0])) {
55
            return [[], null];
56
        }
57 1
        $reviews = ReviewsExtractor::extractReviews($this->requestApp, $json[0]);
58 1
        $nextToken = $json[1][1] ?? null;
59
60 1
        return [$reviews, $nextToken];
61
    }
62
}
63