Passed
Pull Request — main (#3)
by Daryl
02:25
created

Performances   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 81.67%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 54
c 6
b 0
f 0
dl 0
loc 109
ccs 49
cts 60
cp 0.8167
rs 10
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getPerformancesBetween() 0 21 4
A getUpcomingPerformances() 0 12 2
A getPerformancesForProductionSeason() 0 4 1
A __construct() 0 4 1
A search() 0 19 3
A getPerformanceZoneAvailabilities() 0 12 3
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
            trigger_error($e->getMessage(), E_USER_WARNING);
31
        }
32
33
        return [];
34
    }
35
36 1
    public function getPerformancesBetween(DateTime $start, DateTime $end): array
37
    {
38 1
        $sorted = [];
39 1
        $performances = $this->search([
40 1
            'PerformanceStartDate' => $start->format(DATE_ATOM),
41 1
            'PerformanceEndDate' => $end->format(DATE_ATOM),
42 1
        ]);
43
44 1
        foreach ($performances as $performance) {
45
            try {
46 1
                $date = $performance->date();
47 1
                if (!is_null($date)) {
48 1
                    $sorted[$date->getTimestamp()] = $performance;
49
                }
50
            } catch (Exception $e) {
51
                trigger_error($e->getMessage(), E_USER_WARNING);
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
}