Completed
Push — master ( 7f4096...bb097f )
by Brian
01:14
created

Dnd5eApi::skills()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Darkgoldblade01\Dnd5eApi;
4
5
use Darkgoldblade01\Dnd5eApi\Api\AbilityScores;
6
use Darkgoldblade01\Dnd5eApi\Api\Skills;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Exception\BadResponseException;
9
use GuzzleHttp\Exception\GuzzleException;
10
11
class Dnd5eApi
12
{
13
14
    /**
15
     * Base URI
16
     *
17
     * The base URI for the API.
18
     *
19
     * @var string
20
     */
21
    protected $base_uri = 'https://www.dnd5eapi.co/api/';
22
23
    /**
24
     * @var Client
25
     */
26
    private $client;
27
28
    public function __construct() {
29
        $this->client = new Client([
30
            "base_uri" => $this->base_uri,
31
        ]);
32
    }
33
34
    /**
35
     * Ability Scores
36
     *
37
     * Return a new instance of the DND API
38
     * under the Ability Scores.
39
     *
40
     * @return AbilityScores
41
     */
42
    protected function ability_scores(): AbilityScores
43
    {
44
        return new AbilityScores();
45
    }
46
47
    /**
48
     * Skills
49
     *
50
     * Return a new instance of the DND API
51
     * under the Skills.
52
     *
53
     * @return Skills
54
     */
55
    protected function skills(): Skills
56
    {
57
        return new Skills();
58
    }
59
60
    /**
61
     * @param null $endpoint
62
     * @return mixed
63
     * @throws GuzzleException|NotFoundException
64
     */
65
    protected function get($endpoint = null) {
66
        try {
67
            $response = $this->client->get($endpoint);
68
        } catch (BadResponseException $e) {
69
            if($e->getCode() === '404') {
70
                throw new NotFoundException('The ability skill ID of `' . $endpoint .'` does not exist.');
71
            } else {
72
                throw new \Exception($e);
73
            }
74
        }
75
        return json_decode($response->getBody()->getContents(), true);
76
    }
77
}
78