1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (c) Ne-Lexa |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @see https://github.com/Ne-Lexa/google-play-scraper |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Nelexa\GPlay\Scraper; |
15
|
|
|
|
16
|
|
|
use GuzzleHttp\Psr7\Query; |
17
|
|
|
use Nelexa\GPlay\Exception\GooglePlayException; |
18
|
|
|
use Nelexa\GPlay\HttpClient\ParseHandlerInterface; |
19
|
|
|
use Nelexa\GPlay\Model\AppId; |
20
|
|
|
use Nelexa\GPlay\Model\Review; |
21
|
|
|
use Nelexa\GPlay\Scraper\Extractor\ReviewsExtractor; |
22
|
|
|
use Nelexa\GPlay\Util\ScraperUtil; |
23
|
|
|
use Psr\Http\Message\RequestInterface; |
24
|
|
|
use Psr\Http\Message\ResponseInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @internal |
28
|
|
|
*/ |
29
|
|
|
class AppSpecificReviewScraper implements ParseHandlerInterface |
30
|
|
|
{ |
31
|
|
|
/** @var AppId */ |
32
|
|
|
private $requestApp; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* OneAppReviewScraper constructor. |
36
|
|
|
* |
37
|
|
|
* @param AppId $requestApp |
38
|
|
|
*/ |
39
|
|
|
public function __construct(AppId $requestApp) |
40
|
|
|
{ |
41
|
|
|
$this->requestApp = $requestApp; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param RequestInterface $request |
46
|
|
|
* @param ResponseInterface $response |
47
|
|
|
* @param array $options |
48
|
|
|
* |
49
|
|
|
* @throws \Nelexa\GPlay\Exception\GooglePlayException |
50
|
|
|
* |
51
|
|
|
* @return Review |
52
|
|
|
*/ |
53
|
|
|
public function __invoke(RequestInterface $request, ResponseInterface $response, array &$options = []): Review |
54
|
|
|
{ |
55
|
|
|
$reviewId = Query::parse($request->getUri()->getQuery())['reviewId']; |
56
|
|
|
$scriptData = ScraperUtil::extractScriptData($response->getBody()->getContents()); |
57
|
|
|
|
58
|
|
|
foreach ($scriptData as $key => $value) { |
59
|
|
|
if (isset($value[0][0][0]) && $value[0][0][0] === $reviewId) { |
60
|
|
|
return ReviewsExtractor::extractReview($this->requestApp, $value[0][0]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
throw new GooglePlayException( |
65
|
|
|
sprintf('%s application review %s does not exist.', $this->requestApp->getId(), $reviewId) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|