Passed
Branch develop (61f351)
by Alexey
01:52
created

PlayStoreUiRequest::getAppsRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 4
dl 0
loc 20
rs 9.7666
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Nelexa\GPlay\Request;
5
6
use GuzzleHttp\Psr7\Request;
7
use Nelexa\GPlay\Enum\SortEnum;
8
use Nelexa\GPlay\GPlayApps;
9
use Psr\Http\Message\RequestInterface;
10
use function GuzzleHttp\Psr7\stream_for;
11
12
class PlayStoreUiRequest
13
{
14
    public const LIMIT_REVIEW_ON_PAGE = 199;
15
    public const LIMIT_APPS_ON_PAGE = 100;
16
17
    private const RPC_ID_REVIEWS = 'UsvDTd';
18
    private const RPC_ID_APPS = 'qnKhOb';
19
20
    /**
21
     * @param RequestApp $requestApp
22
     * @param int $count
23
     * @param SortEnum $sort
24
     * @param string|null $token
25
     * @return RequestInterface
26
     */
27
    public static function getReviewsRequest(RequestApp $requestApp, int $count, SortEnum $sort, ?string $token = null): RequestInterface
28
    {
29
        $limit = min(self::LIMIT_REVIEW_ON_PAGE, max(1, $count));
30
        $queryParams = [
31
            'rpcids' => self::RPC_ID_REVIEWS,
32
            GPlayApps::REQ_PARAM_LOCALE => $requestApp->getLocale(),
33
            GPlayApps::REQ_PARAM_COUNTRY => $requestApp->getCountry(),
34
            'authuser' => null,
35
            'soc-app' => 121,
36
            'soc-platform' => 1,
37
            'soc-device' => 1,
38
        ];
39
        $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams);
40
        $formParams = [
41
            'f.req' => '[[["' . self::RPC_ID_REVIEWS . '","[null,null,[2,' . $sort->value() . ',[' . $limit . ',null,' . ($token === null ? 'null' : '\\"' . $token . '\\"') . ']],[\\"' . $requestApp->getId() . '\\",7]]",null,"generic"]]]',
42
        ];
43
        $headers = [
44
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
45
        ];
46
        $body = stream_for(http_build_query($formParams));
47
        return new Request('POST', $url, $headers, $body, '2.0');
48
    }
49
50
    /**
51
     * @param string $locale
52
     * @param string $country
53
     * @param int $count
54
     * @param string $token
55
     * @return RequestInterface
56
     */
57
    public static function getAppsRequest(string $locale, string $country, int $count, string $token): RequestInterface
58
    {
59
        $limit = min(self::LIMIT_APPS_ON_PAGE, max(1, $count));
60
        $queryParams = [
61
            'rpcids' => self::RPC_ID_APPS,
62
            GPlayApps::REQ_PARAM_LOCALE => $locale,
63
            GPlayApps::REQ_PARAM_COUNTRY => $country,
64
            'soc-app' => 121,
65
            'soc-platform' => 1,
66
            'soc-device' => 1,
67
        ];
68
        $url = GPlayApps::GOOGLE_PLAY_URL . '/_/PlayStoreUi/data/batchexecute?' . http_build_query($queryParams);
69
        $formParams = [
70
            'f.req' => '[[["' . self::RPC_ID_APPS . '","[[null,[[10,[10,' . $limit . ']],true,null,[1]],null,\\"' . $token . '\\"]]",null,"generic"]]]',
71
        ];
72
        $headers = [
73
            'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8',
74
        ];
75
        $body = stream_for(http_build_query($formParams));
76
        return new Request('POST', $url, $headers, $body, '2.0');
77
    }
78
}
79