1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of SteamScore. |
6
|
|
|
* |
7
|
|
|
* (c) SteamScore <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public |
10
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this |
11
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace SteamScore\Api\Domain\Services; |
15
|
|
|
|
16
|
|
|
use bandwidthThrottle\tokenBucket\TokenBucket; |
17
|
|
|
use GuzzleHttp\ClientInterface; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
use SteamScore\Api\Domain\Exceptions\RateLimitException; |
20
|
|
|
|
21
|
|
|
final class SteamSpyService |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const BASE_URI = 'https://steamspy.com/api.php'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
const BUCKET = 'steamspy'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TokenBucket |
35
|
|
|
*/ |
36
|
|
|
private $bucket; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ClientInterface |
40
|
|
|
*/ |
41
|
|
|
private $client; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param ClientInterface $client |
47
|
|
|
* @param TokenBucket $bucket |
48
|
|
|
*/ |
49
|
|
|
public function __construct(ClientInterface $client, TokenBucket $bucket) |
50
|
|
|
{ |
51
|
|
|
$this->bucket = $bucket; |
52
|
|
|
$this->client = $client; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Finds all games. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function findAll(): array |
61
|
|
|
{ |
62
|
|
|
if ($this->bucket->consume(1, $seconds) === false) { |
63
|
|
|
throw new RateLimitException($seconds); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$response = $this->client->request('GET', self::BASE_URI, [ |
67
|
|
|
'query' => [ |
68
|
|
|
'request' => 'all', |
69
|
|
|
], |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
return $this->parseResponse($response); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Finds a game by its Steam AppId. |
77
|
|
|
* |
78
|
|
|
* @param int $appId |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function findByAppId(int $appId): array |
83
|
|
|
{ |
84
|
|
|
if ($this->bucket->consume(1, $seconds) === false) { |
85
|
|
|
throw new RateLimitException($seconds); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$response = $this->client->request('GET', self::BASE_URI, [ |
89
|
|
|
'query' => [ |
90
|
|
|
'appid' => $appId, |
91
|
|
|
'request' => 'appdetails', |
92
|
|
|
], |
93
|
|
|
]); |
94
|
|
|
|
95
|
|
|
return $this->parseResponse($response); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function parseResponse(ResponseInterface $response): array |
99
|
|
|
{ |
100
|
|
|
$body = (string) $response->getBody(); |
101
|
|
|
|
102
|
|
|
// Clear json_last_error() |
103
|
|
|
json_encode(null); |
104
|
|
|
|
105
|
|
|
$json = json_decode($body, true); |
106
|
|
|
|
107
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
108
|
|
|
// @todo: Custom exception |
109
|
|
|
throw new \Exception(sprintf('Unable to decode data to JSON in %s: %s', __CLASS__, json_last_error_msg())); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $json; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|