SteamSpyService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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