Passed
Pull Request — main (#15)
by
unknown
02:23
created

ProductionSeason::last_performance_date()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.5625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 21
ccs 9
cts 12
cp 0.75
crap 6.5625
rs 9.2222
1
<?php
2
3
namespace Clubdeuce\Tessitura\Resources;
4
5
use Clubdeuce\Tessitura\Base\Base;
6
use DateTime;
7
use DateTimeZone;
8
use Exception;
9
use InvalidArgumentException;
10
use Throwable;
11
12
/**
13
 * @method string[] response()
14
 */
15
class ProductionSeason extends Base
16
{
17
18
    /**
19
     * @var string[]
20
     */
21
    protected array $_response = [];
22
23
    /**
24
     * @var Performance[]
25
     */
26
    protected array $_performances = [];
27
28
    /**
29
     * @throws InvalidArgumentException
30
     */
31 4
    public function first_performance_date(string $timezone = 'America/New_York'): DateTime|bool
32
    {
33
        try {
34 4
            $timezone = new DateTimeZone($timezone);
35 1
        } catch (Exception $e) {
36 1
            throw new InvalidArgumentException($e->getMessage());
37
        }
38
39 3
        if ($date = isset($this->response()['FirstPerformanceDate']) ? $this->response()['FirstPerformanceDate'] : false) {
40
            try {
41 2
                $result = DateTime::createFromFormat(' Y-m-d\TG:i:sp', $date, $timezone);
42 2
                if ($result === false) {
43
                    throw new Exception("Invalid date format for FirstPerformanceDate");
44
                }
45 2
                return $result;
46
            } catch (Throwable $e) {
47
                throw new Exception("Unable to parse FirstPerformanceDate: " . $e->getMessage());
48
            }
49
        }
50
51 1
        return false;
52
    }
53
54
    /**
55
     * @throws InvalidArgumentException
56
     */
57 3
    public function last_performance_date(string $timezone = 'America/New_York'): DateTime|bool
58
    {
59
        try {
60 3
            $timezone = new DateTimeZone($timezone);
61 1
        } catch (Exception $e) {
62 1
            throw new InvalidArgumentException($e->getMessage());
63
        }
64
65 2
        if ($date = isset($this->response()['LastPerformanceDate']) ? $this->response()['LastPerformanceDate'] : false) {
66
            try {
67 1
                $result = DateTime::createFromFormat(' Y-m-d\TG:i:sp', $date, $timezone);
68 1
                if ($result === false) {
69
                    throw new Exception("Invalid date format for LastPerformanceDate");
70
                }
71 1
                return $result;
72
            } catch (Throwable $e) {
73
                throw new Exception("Unable to parse LastPerformanceDate: " . $e->getMessage());
74
            }
75
        }
76
77 1
        return false;
78
    }
79
80
    /**
81
     * @return Performance[]
82
     */
83 1
    public function performances(): array
84
    {
85 1
        return $this->_performances;
86
    }
87
88
}
89