| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\EventTickets\Models; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use Give\Donations\Models\Donation; |
| 7 |
use Give\EventTickets\Factories\EventTicketFactory; |
| 8 |
use Give\EventTickets\Repositories\EventRepository; |
| 9 |
use Give\EventTickets\Repositories\EventTicketRepository; |
| 10 |
use Give\EventTickets\Repositories\EventTicketTypeRepository; |
| 11 |
use Give\Framework\Exceptions\Primitives\Exception; |
| 12 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 13 |
use Give\Framework\Models\Contracts\ModelCrud; |
| 14 |
use Give\Framework\Models\Model; |
| 15 |
use Give\Framework\Models\ModelQueryBuilder; |
| 16 |
use Give\Framework\Models\ValueObjects\Relationship; |
| 17 |
use Give\Framework\Support\Facades\DateTime\Temporal; |
| 18 |
use Give\Framework\Support\ValueObjects\Money; |
| 19 |
|
| 20 |
/** |
| 21 |
* @since 3.6.0 |
| 22 |
*/ |
| 23 |
class EventTicket extends Model implements ModelCrud /*, ModelHasFactory */ |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @inheritdoc |
| 27 |
* |
| 28 |
* @since 3.20.0 Add amount to the properties array |
| 29 |
* @since 3.6.0 |
| 30 |
*/ |
| 31 |
protected $properties = [ |
| 32 |
'id' => 'int', // @todo Maybe use UUID instead of auto-incrementing integer |
| 33 |
'eventId' => 'int', |
| 34 |
'ticketTypeId' => 'int', |
| 35 |
'donationId' => 'int', |
| 36 |
'amount' => Money::class, |
| 37 |
'createdAt' => DateTime::class, |
| 38 |
'updatedAt' => DateTime::class, |
| 39 |
]; |
| 40 |
|
| 41 |
/** |
| 42 |
* @inheritdoc |
| 43 |
*/ |
| 44 |
protected $relationships = [ |
| 45 |
'event' => Relationship::BELONGS_TO, |
| 46 |
'ticketType' => Relationship::BELONGS_TO, |
| 47 |
'donation' => Relationship::BELONGS_TO, |
| 48 |
]; |
| 49 |
|
| 50 |
/** |
| 51 |
* @since 3.6.0 |
| 52 |
* |
| 53 |
* @return EventTicket|null |
| 54 |
*/ |
| 55 |
public static function find($id) |
| 56 |
{ |
| 57 |
return give(EventTicketRepository::class)->getById($id); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* @since 3.6.0 |
| 62 |
* |
| 63 |
* @return ModelQueryBuilder |
| 64 |
*/ |
| 65 |
public static function findByEvent($eventId): ModelQueryBuilder |
| 66 |
{ |
| 67 |
return give(EventTicketRepository::class)->queryByEventId($eventId); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* @since 3.6.0 |
| 73 |
* |
| 74 |
* @return ModelQueryBuilder |
| 75 |
*/ |
| 76 |
public static function findByTicketType($ticketTypeId): ModelQueryBuilder |
| 77 |
{ |
| 78 |
return give(EventTicketRepository::class)->queryByTicketTypeId($ticketTypeId); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* @since 3.6.0 |
| 84 |
* |
| 85 |
* @return $this |
| 86 |
* @throws Exception|InvalidArgumentException |
| 87 |
*/ |
| 88 |
public static function create(array $attributes): EventTicket |
| 89 |
{ |
| 90 |
$event = new static($attributes); |
| 91 |
|
| 92 |
give(EventTicketRepository::class)->insert($event); |
| 93 |
|
| 94 |
return $event; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @since 3.6.0 |
| 99 |
* |
| 100 |
* @return void |
| 101 |
* @throws Exception|InvalidArgumentException |
| 102 |
*/ |
| 103 |
public function save() |
| 104 |
{ |
| 105 |
if (!$this->id) { |
| 106 |
give(EventTicketRepository::class)->insert($this); |
| 107 |
} else{ |
| 108 |
give(EventTicketRepository::class)->update($this); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @since 3.6.0 |
| 114 |
* |
| 115 |
* @throws Exception|InvalidArgumentException |
| 116 |
*/ |
| 117 |
public function delete(): bool |
| 118 |
{ |
| 119 |
return give(EventTicketRepository::class)->delete($this); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* @since 3.6.0 |
| 124 |
* |
| 125 |
* @return ModelQueryBuilder<EventTicket> |
| 126 |
*/ |
| 127 |
public static function query(): ModelQueryBuilder |
| 128 |
{ |
| 129 |
return give(EventTicketRepository::class)->prepareQuery(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @since 3.6.0 |
| 134 |
* |
| 135 |
* @return ModelQueryBuilder<Event> |
| 136 |
*/ |
| 137 |
public function event(): ModelQueryBuilder |
| 138 |
{ |
| 139 |
return give(EventRepository::class)->queryById($this->eventId); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @since 3.6.0 |
| 144 |
* |
| 145 |
* @return ModelQueryBuilder<EventTicketType> |
| 146 |
*/ |
| 147 |
public function ticketType(): ModelQueryBuilder |
| 148 |
{ |
| 149 |
return give(EventTicketTypeRepository::class)->queryById($this->ticketTypeId); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* @since 3.6.0 |
| 154 |
* |
| 155 |
* @return ModelQueryBuilder<Donation> |
| 156 |
*/ |
| 157 |
public function donation(): ModelQueryBuilder |
| 158 |
{ |
| 159 |
return give()->donations->queryById($this->donationId); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* @since 3.20.0 Add amount to the properties array |
| 164 |
* @since 3.6.0 |
| 165 |
* |
| 166 |
* @param object $object |
| 167 |
*/ |
| 168 |
public static function fromQueryBuilderObject($object): EventTicket |
| 169 |
{ |
| 170 |
return new EventTicket([ |
| 171 |
'id' => (int)$object->id, |
| 172 |
'eventId' => (int)$object->event_id, |
| 173 |
'ticketTypeId' => (int)$object->ticket_type_id, |
| 174 |
'donationId' => (int)$object->donation_id, |
| 175 |
'amount' => new Money($object->amount, $object->currency), |
| 176 |
'createdAt' => Temporal::toDateTime($object->created_at), |
| 177 |
'updatedAt' => Temporal::toDateTime($object->updated_at), |
| 178 |
]); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* @since 3.6.0 |
| 183 |
*/ |
| 184 |
public static function factory(): EventTicketFactory |
| 185 |
{ |
| 186 |
return new EventTicketFactory(static::class); |
| 187 |
} |
| 188 |
} |
| 189 |
|