Passed
Push — main ( 19d97c...3a0d8d )
by Daryl
02:38
created

Performances   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 82.76%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 52
c 7
b 0
f 0
dl 0
loc 132
ccs 48
cts 58
cp 0.8276
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPerformancesBetween() 0 21 4
A getUpcomingPerformances() 0 12 2
A getPerformancesForProductionSeason() 0 4 1
A makeNewZoneAvailability() 0 14 1
A search() 0 19 3
A getPerformanceZoneAvailabilities() 0 8 2
1
<?php
2
3
namespace Clubdeuce\Tessitura\Resources;
4
5
use Clubdeuce\Tessitura\Base\Base;
6
use Clubdeuce\Tessitura\Interfaces\ApiInterface;
7
use Clubdeuce\Tessitura\Interfaces\ResourceInterface;
8
use DateTime;
9
use Exception;
10
11
class Performances extends Base implements ResourceInterface
12
{
13
    public const RESOURCE = 'TXN/Performances';
14
    protected ApiInterface $_api;
15
16 8
    public function __construct(ApiInterface $api)
17
    {
18 8
        $this->_api = $api;
19 8
        parent::__construct();
20
    }
21
22
    /**
23
     * Get upcoming performances
24
     *
25
     * @param int $days Number of days to look ahead for performances. Defaults to thirty days.
26
     * @return Performance[]
27
     */
28
    public function getUpcomingPerformances(int $days = 30): array
29
    {
30
        try {
31
            $start = new DateTime();
32
            $end = new DateTime("now + {$days} days");
33
34
            return $this->getPerformancesBetween($start, $end);
35
        } catch (Exception $e) {
36
            trigger_error($e->getMessage(), E_USER_WARNING);
37
        }
38
39
        return [];
40
    }
41
42
    /**
43
     * Get performances between two dates.
44
     *
45
     * @param DateTime $start
46
     * @param DateTime $end
47
     * @return Performance[]
48
     */
49 1
    public function getPerformancesBetween(DateTime $start, DateTime $end): array
50
    {
51 1
        $sorted = [];
52 1
        $performances = $this->search([
53 1
            'PerformanceStartDate' => $start->format(DATE_ATOM),
54 1
            'PerformanceEndDate' => $end->format(DATE_ATOM),
55 1
        ]);
56
57 1
        foreach ($performances as $performance) {
58
            try {
59 1
                $date = $performance->date();
60 1
                if (!is_null($date)) {
61 1
                    $sorted[$date->getTimestamp()] = $performance;
62
                }
63
            } catch (Exception $e) {
64
                trigger_error($e->getMessage(), E_USER_WARNING);
65
            }
66
        }
67
68 1
        ksort($sorted);
69 1
        return $sorted;
70
    }
71
72
    /**
73
     * @param int $psId
74
     * @return Performance[]
75
     */
76 2
    public function getPerformancesForProductionSeason(int $psId): array
77
    {
78 2
        return $this->search([
79 2
            'ProductionSeasonIds' => (string)$psId,
80 2
        ]);
81
    }
82
83
    /**
84
     * @param  mixed[] $args
85
     * @return Performance[]
86
     */
87 5
    public function search(array $args = []): array
88
    {
89 5
        $endpoint = sprintf('%1$s/Search', self::RESOURCE);
90 5
        $body = json_encode($args);
91
92 5
        $args = [
93 5
            'body' => $body,
94 5
            'headers' => [
95 5
                'Content-Length' => $body ? strlen($body) : 0,
96 5
            ]
97 5
        ];
98
99 5
        $results = $this->_api->post($endpoint, $args);
100
101 5
        if (!is_array($results)) {
102
            return [];
103
        }
104
105 5
        return array_map(fn($item) => new Performance($item), $results);
106
    }
107
108
    /**
109
     * @param int $performanceId
110
     * @return PerformanceZoneAvailability[]
111
     */
112 2
    public function getPerformanceZoneAvailabilities(int $performanceId): array
113
    {
114
        try {
115 2
            $data = $this->_api->get(sprintf('%1$s/Zones?performanceIds=%2$s', self::RESOURCE, $performanceId));
116
117 1
            return array_map([$this, 'makeNewZoneAvailability'], $data);
118 1
        } catch (Exception $e) {
119 1
            return [];
120
        }
121
    }
122
123
    /**
124
     * Create a new PerformanceZoneAvailability instance from the provided data.
125
     *
126
     * @param mixed[] $data
127
     * @return PerformanceZoneAvailability
128
     */
129 2
    protected function makeNewZoneAvailability(array $data): PerformanceZoneAvailability
130
    {
131 2
        $data = $this->parseArgs($data, [
132 2
            'AvailableCount' => 0,
133 2
            'Id' => 0,
134 2
            'Inactive' => false,
135 2
            'PerformanceId' => 0,
136 2
            'SectionSummaries' => null,
137 2
            'Zone' => null,
138 2
        ]);
139
140 2
        return new PerformanceZoneAvailability([
141 2
            'availableCount' => $data['AvailableCount'],
142 2
            'zone' => $data['Zone'],
143 2
        ]);
144
    }
145
}
146