Completed
Push — master ( 15368f...a37bd1 )
by Brian
01:09 queued 11s
created

Dnd5eApi::proficiencies()   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\Proficiencies;
7
use Darkgoldblade01\Dnd5eApi\Api\Skills;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Exception\BadResponseException;
10
use GuzzleHttp\Exception\GuzzleException;
11
12
class Dnd5eApi
13
{
14
15
    /**
16
     * Base URI
17
     *
18
     * The base URI for the API.
19
     *
20
     * @var string
21
     */
22
    protected $base_uri = 'https://www.dnd5eapi.co/api/';
23
24
    /**
25
     * @var Client
26
     */
27
    private $client;
28
29
    public function __construct() {
30
        $this->client = new Client([
31
            "base_uri" => $this->base_uri,
32
        ]);
33
    }
34
35
    /**
36
     * Ability Scores
37
     *
38
     * Return a new instance of the DND API
39
     * under the Ability Scores.
40
     *
41
     * @return AbilityScores
42
     */
43
    protected function ability_scores(): AbilityScores
44
    {
45
        return new AbilityScores();
46
    }
47
48
    /**
49
     * Skills
50
     *
51
     * Return a new instance of the DND API
52
     * under the Skills.
53
     *
54
     * @return Skills
55
     */
56
    protected function skills(): Skills
57
    {
58
        return new Skills();
59
    }
60
61
    /**
62
     * Proficiencies
63
     *
64
     * Return a new instance of the DND API
65
     * under the Proficiencies.
66
     *
67
     * @return Proficiencies
68
     */
69
    protected function proficiencies(): Proficiencies
70
    {
71
        return new Proficiencies();
72
    }
73
74
    /**
75
     * @param null $endpoint
76
     * @return mixed
77
     * @throws GuzzleException|NotFoundException
78
     */
79
    protected function get($endpoint = null) {
80
        if(strpos($endpoint, '_') !== false) {
81
            $endpoint = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $endpoint)));
82
83
        }
84
        try {
85
            $response = $this->client->get($endpoint);
86
        } catch (BadResponseException $e) {
87
            if($e->getCode() === '404') {
88
                throw new NotFoundException('The ability skill ID of `' . $endpoint .'` does not exist.');
89
            } else {
90
                throw new \Exception($e);
91
            }
92
        }
93
        return json_decode($response->getBody()->getContents(), true);
94
    }
95
}
96