Completed
Push — master ( 30606e...358821 )
by Jonas
01:52
created

SteamSpyService::findAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2
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\JsonDecodeException;
20
use SteamScore\Api\Domain\Exceptions\RateLimitException;
21
22
final class SteamSpyService
23
{
24
    /**
25
     * @var string
26
     */
27
    const BASE_URI = 'https://steamspy.com/api.php';
28
29
    /**
30
     * @var string
31
     */
32
    const BUCKET = 'steamspy';
33
34
    /**
35
     * @var TokenBucket
36
     */
37
    private $bucket;
38
39
    /**
40
     * @var ClientInterface
41
     */
42
    private $client;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @param ClientInterface $client
48
     * @param TokenBucket     $bucket
49
     */
50 5
    public function __construct(ClientInterface $client, TokenBucket $bucket)
51
    {
52 5
        $this->bucket = $bucket;
53 5
        $this->client = $client;
54
    }
55
56
    /**
57
     * Finds all games.
58
     *
59
     * @return array
60
     */
61 3
    public function findAll(): array
62
    {
63 3
        if ($this->bucket->consume(1, $seconds) === false) {
64 1
            throw new RateLimitException((float) $seconds);
65
        }
66
67 2
        $response = $this->client->request('GET', self::BASE_URI, [
68
            'query' => [
69
                'request' => 'all',
70 2
            ],
71
        ]);
72
73 2
        return $this->parseResponse($response);
74
    }
75
76
    /**
77
     * Finds a game by its Steam AppId.
78
     *
79
     * @param int $appId
80
     *
81
     * @return array
82
     */
83 2
    public function findByAppId(int $appId): array
84
    {
85 2
        if ($this->bucket->consume(1, $seconds) === false) {
86 1
            throw new RateLimitException((float) $seconds);
87
        }
88
89 1
        $response = $this->client->request('GET', self::BASE_URI, [
90
            'query' => [
91 1
                'appid' => $appId,
92 1
                'request' => 'appdetails',
93
            ],
94
        ]);
95
96 1
        return $this->parseResponse($response);
97
    }
98
99 3
    private function parseResponse(ResponseInterface $response): array
100
    {
101 3
        $body = (string) $response->getBody();
102
103
        // Clear json_last_error()
104 3
        json_encode(null);
105
106 3
        $json = json_decode($body, true);
107
108 3
        if (JSON_ERROR_NONE !== json_last_error()) {
109 1
            throw new JsonDecodeException(sprintf('Unable to decode data to JSON in %s: %s', __CLASS__, json_last_error_msg()));
110
        }
111
112 2
        return $json;
113
    }
114
}
115