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

ReviewsScraper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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;
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