Passed
Branch feature/refactoring (13cbf0)
by Alexey
03:46
created

AppSpecificReviewScraper::__invoke()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
ccs 6
cts 8
cp 0.75
rs 10
cc 4
nc 3
nop 2
crap 4.25
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\Exception\GooglePlayException;
15
use Nelexa\GPlay\Model\AppId;
16
use Nelexa\GPlay\Model\Review;
17
use Nelexa\GPlay\Scraper\Extractor\ReviewsExtractor;
18
use Nelexa\GPlay\Util\ScraperUtil;
19
use Nelexa\HttpClient\ResponseHandlerInterface;
20
use Psr\Http\Message\RequestInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use function GuzzleHttp\Psr7\parse_query;
23
24
/**
25
 * @internal
26
 */
27
class AppSpecificReviewScraper implements ResponseHandlerInterface
28
{
29
    /** @var AppId */
30
    private $requestApp;
31
32
    /**
33
     * OneAppReviewScraper constructor.
34
     *
35
     * @param AppId $requestApp
36
     */
37 1
    public function __construct(AppId $requestApp)
38
    {
39 1
        $this->requestApp = $requestApp;
40 1
    }
41
42
    /**
43
     * @param RequestInterface  $request
44
     * @param ResponseInterface $response
45
     *
46
     * @throws GooglePlayException
47
     *
48
     * @return Review
49
     */
50 1
    public function __invoke(RequestInterface $request, ResponseInterface $response): Review
51
    {
52 1
        $reviewId = parse_query($request->getUri()->getQuery())['reviewId'];
53 1
        $scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents());
54
55 1
        foreach ($scriptData as $key => $value) {
56 1
            if (isset($value[0][0][0]) && $value[0][0][0] === $reviewId) {
57 1
                return ReviewsExtractor::extractReview($this->requestApp, $value[0][0]);
58
            }
59
        }
60
61
        throw new GooglePlayException(
62
            sprintf('%s application review %s does not exist.', $this->requestApp->getId(), $reviewId)
63
        );
64
    }
65
}
66