Seasons::getById()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 3
nop 1
crap 2
1
<?php
2
3
namespace Clubdeuce\Tessitura\Resources;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\GuzzleException;
8
9
/**
10
 * @link https://www.tessituranetwork.com/REST_v151/TessituraService/HELP/RESOURCES/SEASONS.HTM
11
 */
12
class Seasons
13
{
14
    public const RESOURCE = 'ReferenceData/Seasons';
15
16
    protected Client $client;
17
18 2
    public function __construct(?Client $client = null)
19
    {
20 2
        if (empty($client)) {
21
            $client = new Client();
22
        }
23
24 2
        $this->client = $client;
25
    }
26
27
    /**
28
     * @link https://www.tessituranetwork.com/REST_v151/TessituraService/HELP/API/GET_REFERENCEDATA_SEASONS_ID_FI.HTM
29
     * @throws Exception
30
     */
31 1
    public function getById(int $id): Season
32
    {
33
        try {
34 1
            $response = $this->client->get(sprintf('%s/%s', self::RESOURCE, $id));
35
36 1
            return new Season(json_decode($response->getBody()->getContents(), true));
37 1
        } catch (GuzzleException $e) {
38 1
            throw new Exception($e->getMessage());
39
        }
40
    }
41
42
    /**
43
     * @return Season[]
44
     * @throws Exception
45
     */
46 1
    public function get(): array
47
    {
48
        try {
49 1
            $response = $this->client->get(self::RESOURCE);
50 1
            $data     = json_decode($response->getBody()->getContents(), true);
51
52 1
            return array_map(function (array $season) {
53 1
                return new Season($season);
54 1
            }, $data);
55 1
        } catch (GuzzleException $e) {
56 1
            throw new Exception($e->getMessage());
57
        }
58
    }
59
}
60