EventRepository.php
2 years ago
EventTicketRepository.php
1 year ago
EventTicketTypeRepository.php
2 years ago
EventTicketRepository.php
251 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\EventTickets\Repositories; |
| 4 | |
| 5 | use Give\Donations\Models\Donation; |
| 6 | use Give\EventTickets\Models\EventTicket; |
| 7 | use Give\Framework\Database\DB; |
| 8 | use Give\Framework\Exceptions\Primitives\Exception; |
| 9 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 10 | use Give\Framework\Models\ModelQueryBuilder; |
| 11 | use Give\Framework\Support\Facades\DateTime\Temporal; |
| 12 | use Give\Framework\Support\ValueObjects\Money; |
| 13 | use Give\Helpers\Hooks; |
| 14 | use Give\Log\Log; |
| 15 | |
| 16 | /** |
| 17 | * @since 3.6.0 |
| 18 | */ |
| 19 | class EventTicketRepository |
| 20 | { |
| 21 | |
| 22 | /** |
| 23 | * @since 3.20.0 Add "amount" column to the properties array |
| 24 | * @since 3.6.0 |
| 25 | * |
| 26 | * @var string[] |
| 27 | */ |
| 28 | private $requiredProperties = [ |
| 29 | 'eventId', |
| 30 | 'ticketTypeId', |
| 31 | 'donationId', |
| 32 | 'amount', |
| 33 | ]; |
| 34 | |
| 35 | /** |
| 36 | * @since 3.6.0 |
| 37 | */ |
| 38 | public function getById(int $id): ?EventTicket |
| 39 | { |
| 40 | return $this->prepareQuery() |
| 41 | ->where('id', $id) |
| 42 | ->get(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @since 3.6.0 |
| 47 | */ |
| 48 | public function queryById(int $id): ModelQueryBuilder |
| 49 | { |
| 50 | return $this->prepareQuery() |
| 51 | ->where('id', $id); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @since 3.20.0 Add "amount" column to the insert statement |
| 56 | * @since 3.6.0 |
| 57 | * |
| 58 | * @throws Exception|InvalidArgumentException |
| 59 | */ |
| 60 | public function insert(EventTicket $eventTicket) |
| 61 | { |
| 62 | $this->validate($eventTicket); |
| 63 | |
| 64 | Hooks::doAction('givewp_events_event_ticket_creating', $eventTicket); |
| 65 | |
| 66 | $createdDateTime = Temporal::withoutMicroseconds($eventTicket->createdAt ?: Temporal::getCurrentDateTime()); |
| 67 | |
| 68 | DB::query('START TRANSACTION'); |
| 69 | |
| 70 | try { |
| 71 | DB::table('give_event_tickets') |
| 72 | ->insert([ |
| 73 | 'event_id' => $eventTicket->eventId, |
| 74 | 'ticket_type_id' => $eventTicket->ticketTypeId, |
| 75 | 'donation_id' => $eventTicket->donationId, |
| 76 | 'amount' => $eventTicket->amount->formatToMinorAmount(), |
| 77 | 'created_at' => $createdDateTime->format('Y-m-d H:i:s'), |
| 78 | 'updated_at' => $createdDateTime->format('Y-m-d H:i:s'), |
| 79 | ]); |
| 80 | |
| 81 | $eventTicketId = DB::last_insert_id(); |
| 82 | } catch (Exception $exception) { |
| 83 | DB::query('ROLLBACK'); |
| 84 | |
| 85 | Log::error('Failed creating an event ticket', compact('eventTicket')); |
| 86 | |
| 87 | throw new $exception('Failed creating an event ticket'); |
| 88 | } |
| 89 | |
| 90 | $eventTicket->id = $eventTicketId; |
| 91 | $eventTicket->createdAt = $createdDateTime; |
| 92 | $eventTicket->updatedAt = $createdDateTime; |
| 93 | |
| 94 | DB::query('COMMIT'); |
| 95 | |
| 96 | Hooks::doAction('givewp_events_event_ticket_created', $eventTicket); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @since 3.20.0 Add "amount" column to the update statement |
| 101 | * @since 3.6.0 |
| 102 | * |
| 103 | * @throws Exception|InvalidArgumentException |
| 104 | */ |
| 105 | public function update(EventTicket $eventTicket) |
| 106 | { |
| 107 | $this->validate($eventTicket); |
| 108 | |
| 109 | Hooks::doAction('givewp_events_event_ticket_updating', $eventTicket); |
| 110 | |
| 111 | $updatedDateTime = Temporal::withoutMicroseconds(Temporal::getCurrentDateTime()); |
| 112 | |
| 113 | DB::query('START TRANSACTION'); |
| 114 | |
| 115 | try { |
| 116 | |
| 117 | DB::table('give_event_tickets') |
| 118 | ->where('id', $eventTicket->id) |
| 119 | ->update([ |
| 120 | 'event_id' => $eventTicket->eventId, |
| 121 | 'ticket_type_id' => $eventTicket->ticketTypeId, |
| 122 | 'donation_id' => $eventTicket->donationId, |
| 123 | 'amount' => $eventTicket->amount->formatToMinorAmount(), |
| 124 | 'updated_at' => $updatedDateTime->format('Y-m-d H:i:s'), |
| 125 | ]); |
| 126 | } catch (Exception $exception) { |
| 127 | DB::query('ROLLBACK'); |
| 128 | |
| 129 | Log::error('Failed updating an event ticket', compact('eventTicket')); |
| 130 | |
| 131 | throw new $exception('Failed updating an event ticket'); |
| 132 | } |
| 133 | |
| 134 | $eventTicket->updatedAt = $updatedDateTime; |
| 135 | |
| 136 | DB::query('COMMIT'); |
| 137 | |
| 138 | Hooks::doAction('givewp_events_event_ticket_updated', $eventTicket); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @since 3.6.0 |
| 143 | * |
| 144 | * @throws Exception |
| 145 | */ |
| 146 | public function delete(EventTicket $eventTicket): bool |
| 147 | { |
| 148 | DB::query('START TRANSACTION'); |
| 149 | |
| 150 | Hooks::doAction('givewp_events_event_ticket_deleting', $eventTicket); |
| 151 | |
| 152 | try { |
| 153 | DB::table('give_event_tickets') |
| 154 | ->where('id', $eventTicket->id) |
| 155 | ->delete(); |
| 156 | } catch (Exception $exception) { |
| 157 | DB::query('ROLLBACK'); |
| 158 | |
| 159 | Log::error('Failed deleting an event ticket', compact('eventTicket')); |
| 160 | |
| 161 | throw new $exception('Failed deleting an event ticket'); |
| 162 | } |
| 163 | |
| 164 | DB::query('COMMIT'); |
| 165 | |
| 166 | Hooks::doAction('givewp_events_event_ticket_deleted', $eventTicket); |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @since 3.6.0 |
| 173 | */ |
| 174 | private function validate(EventTicket $eventTicket): void |
| 175 | { |
| 176 | foreach ($this->requiredProperties as $key) { |
| 177 | if (!isset($eventTicket->$key)) { |
| 178 | throw new InvalidArgumentException("'$key' is required."); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @since 3.20.0 Add "amount" column to the select statement |
| 185 | * @since 3.6.0 |
| 186 | * @return ModelQueryBuilder<EventTicket> |
| 187 | */ |
| 188 | public function prepareQuery(): ModelQueryBuilder |
| 189 | { |
| 190 | $builder = new ModelQueryBuilder(EventTicket::class); |
| 191 | |
| 192 | return $builder->from('give_event_tickets') |
| 193 | ->select( |
| 194 | 'id', |
| 195 | 'event_id', |
| 196 | 'ticket_type_id', |
| 197 | 'donation_id', |
| 198 | 'amount', |
| 199 | 'created_at', |
| 200 | 'updated_at' |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @since 3.6.0 |
| 206 | */ |
| 207 | public function queryByEventId(int $eventId): ModelQueryBuilder |
| 208 | { |
| 209 | return $this->prepareQuery() |
| 210 | ->where('event_id', $eventId); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @since 3.6.0 |
| 215 | */ |
| 216 | public function queryByTicketTypeId(int $ticketTypeId): ModelQueryBuilder |
| 217 | { |
| 218 | return $this->prepareQuery() |
| 219 | ->where('ticket_type_id', $ticketTypeId); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @since 3.6.0 |
| 224 | * |
| 225 | * @param int $donationId |
| 226 | * |
| 227 | * @return ModelQueryBuilder |
| 228 | */ |
| 229 | public function queryByDonationId(int $donationId): ModelQueryBuilder |
| 230 | { |
| 231 | return $this->prepareQuery() |
| 232 | ->where('donation_id', $donationId); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @since 3.20.0 Refactored to use event ticket amount instead of ticket type price |
| 237 | * @since 3.6.0 |
| 238 | */ |
| 239 | public function getTotalByDonation(Donation $donation): Money |
| 240 | { |
| 241 | $eventTickets = $this->queryByDonationId($donation->id)->getAll() ?? []; |
| 242 | $currency = $donation->amount->getCurrency(); |
| 243 | |
| 244 | return array_reduce($eventTickets, static function (Money $carry, EventTicket $eventTicket) { |
| 245 | return $carry->add( |
| 246 | $eventTicket->amount |
| 247 | ); |
| 248 | }, new Money(0, $currency)); |
| 249 | } |
| 250 | } |
| 251 |