GlobalApi::games()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 24
ccs 0
cts 12
cp 0
rs 9.4888
c 0
b 0
f 0
cc 5
nc 6
nop 0
crap 30
1
<?php
2
3
namespace Gameap\Services;
4
5
use Config;
6
use Gameap\Exceptions\Services\ResponseException;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Exception\ClientException;
9
use GuzzleHttp\RequestOptions;
10
use Symfony\Component\HttpFoundation\Response;
11
12
class GlobalApi
13
{
14
    private const CONFIG_GLOBAL_API_NAME = 'app.global_api';
15
16
    /**
17
     * @return array
18
     * @throws ResponseException|\GuzzleHttp\Exception\GuzzleException
19
     * @throws \JsonException
20
     */
21
    public static function games()
22
    {
23
        try {
24
            $client = new Client(['headers' => [
25
                'Accept' => 'application/json'
26
            ]]);
27
            $res    = $client->get(config(self::CONFIG_GLOBAL_API_NAME) . '/games');
28
            $status = $res->getStatusCode();
29
        } catch (ClientException $e) {
30
            throw new ResponseException($e->getMessage());
31
        }
32
33
        if ($status !== Response::HTTP_OK) {
34
            throw new ResponseException('Unexpected HTTP status code: ' . $status);
35
        }
36
37
        $json    = $res->getBody()->getContents();
38
        $results = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
39
40
        if (empty($results['success']) || $results['success'] !== true) {
41
            throw new ResponseException('API Error');
42
        }
43
44
        return $results['data'];
45
    }
46
47
    /**
48
     * @param $summary
49
     * @param $description
50
     * @param $environment
51
     * @return bool
52
     * @throws ResponseException
53
     */
54
    public static function sendBug($summary, $description, $environment = '')
55
    {
56
        $version = Config::get('constants.AP_VERSION') . ' [' . Config::get('constants.AP_DATE') . ']';
57
58
        $environment .= 'PHP version: ' . PHP_VERSION . "\n";
59
        $environment .= php_uname() . "\n";
60
61
        try {
62
            $client = new Client(['headers' => ['Accept' => 'application/json']]);
63
64
            $res = $client->post(
65
                config('app.global_api') . '/bugs',
66
                [
67
                    RequestOptions::JSON => compact('version', 'summary', 'description', 'environment'),
68
                ]
69
            );
70
71
            $status = $res->getStatusCode();
72
        } catch (ClientException $e) {
73
            throw new ResponseException($e->getMessage());
74
        }
75
76
        if ($status !== Response::HTTP_CREATED) {
77
            throw new ResponseException('Unexpected HTTP status code: ' . $status);
78
        }
79
80
        return true;
81
    }
82
}
83