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

Performances::makeNewZoneAvailability()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

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