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

Performances   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 83.05%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 53
c 6
b 0
f 0
dl 0
loc 108
ccs 49
cts 59
cp 0.8305
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPerformancesBetween() 0 22 4
A getUpcomingPerformances() 0 9 2
A search() 0 19 3
A getPerformanceZoneAvailabilities() 0 12 3
A getPerformancesForProductionSeason() 0 4 1
A makeNewZoneAvailability() 0 14 1
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
    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
    public function getUpcomingPerformances(int $days = 30): array
23
    {
24
        try {
25
            $start = new DateTime();
26
            $end = new DateTime("now + {$days} days");
27
28
            return $this->getPerformancesBetween($start, $end);
29
        } catch (Exception $e) {
30
            throw new Exception("Unable to get upcoming performances: " . $e->getMessage());
31
        }
32
    }
33
34 1
    public function getPerformancesBetween(DateTime $start, DateTime $end): array
35
    {
36 1
        $sorted = [];
37 1
        $performances = $this->search([
38 1
            'PerformanceStartDate' => $start->format(DATE_ATOM),
39 1
            'PerformanceEndDate' => $end->format(DATE_ATOM),
40 1
        ]);
41
42 1
        foreach ($performances as $performance) {
43
            try {
44 1
                $date = $performance->date();
45 1
                if (!is_null($date)) {
46 1
                    $sorted[$date->getTimestamp()] = $performance;
47
                }
48
            } catch (Exception $e) {
49
                // Skip performances with invalid dates rather than stopping the entire operation
50
                continue;
51
            }
52
        }
53
54 1
        ksort($sorted);
55 1
        return $sorted;
56
    }
57
58 2
    public function getPerformancesForProductionSeason(int $psId): array
59
    {
60 2
        return $this->search([
61 2
            'ProductionSeasonIds' => (string)$psId,
62 2
        ]);
63
    }
64
65
    /**
66
     * @param  array $args
67
     * @return Performance[]
68
     */
69 5
    public function search(array $args = []): array
70
    {
71 5
        $endpoint = sprintf('%1$s/Search', self::RESOURCE);
72 5
        $body = json_encode($args);
73
74 5
        $args = [
75 5
            'body' => $body,
76 5
            'headers' => [
77 5
                'Content-Length' => $body ? strlen($body) : 0,
78 5
            ]
79 5
        ];
80
81 5
        $results = $this->_api->post($endpoint, $args);
82
83 5
        if (!is_array($results)) {
84
            return [];
85
        }
86
87 5
        return array_map(fn($item) => new Performance($item), $results);
88
    }
89
90 2
    public function getPerformanceZoneAvailabilities(int $performanceId): array
91
    {
92
        try {
93 2
            $data = $this->_api->get(sprintf('%1$s/Zones?performanceIds=%2$s', self::RESOURCE, $performanceId));
94
95 1
            if (is_array($data)) {
96 1
                return array_map([$this, 'makeNewZoneAvailability'], $data);
97
            }
98
99
            return [];
100 1
        } catch (Exception $e) {
101 1
            return [];
102
        }
103
    }
104
105 2
    protected function makeNewZoneAvailability(array $data): PerformanceZoneAvailability
106
    {
107 2
        $data = $this->parseArgs($data, [
108 2
            'AvailableCount' => 0,
109 2
            'Id' => 0,
110 2
            'Inactive' => false,
111 2
            'PerformanceId' => 0,
112 2
            'SectionSummaries' => null,
113 2
            'Zone' => null,
114 2
        ]);
115
116 2
        return new PerformanceZoneAvailability([
117 2
            'availableCount' => $data['AvailableCount'],
118 2
            'zone' => $data['Zone'],
119 2
        ]);
120
    }
121
}