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

AppSpecificReviewScraper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 9
c 1
b 0
f 0
dl 0
loc 36
ccs 9
cts 11
cp 0.8182
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 13 4
A __construct() 0 3 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\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