File size: 2,782 Bytes
5f32fbd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | <?php
declare(strict_types=1);
namespace App\Actions;
use App\Models\UserSupport;
class TicketAction
{
private string $sender;
private array $message;
private string $status;
private string $activityType;
private string $notifyTitle;
public function __construct(private UserSupport $ticket) {}
public static function ticket(UserSupport|int|string $ticketOrId): self
{
$ticket = $ticketOrId instanceof UserSupport ? $ticketOrId : UserSupport::findByTicketId($ticketOrId);
return new self($ticket);
}
public function fromAdminIfTrue(bool $isAdmin): self
{
if ($isAdmin) {
return $this->fromAdmin();
}
return $this->fromUser();
}
public function fromAdmin(): self
{
$this->sender = 'admin';
return $this;
}
public function fromUser(): self
{
$this->sender = 'user';
return $this;
}
public function answer(string $message): self
{
$this->status = $this->isSenderAdmin() ? 'Waiting for answer' : 'Answered';
$this->activityType = __('Support request waiting for your answer');
$this->notifyTitle = __('Support request answered');
return $this->message($message);
}
public function new(string $message): self
{
$this->status = 'Submitted a Ticket';
$this->activityType = __($this->status);
$this->notifyTitle = __('Support request submitted');
return $this->message($message);
}
private function message(string $message): self
{
$this->message = [
'message' => $message,
'sender' => $this->sender,
];
return $this;
}
public function send(): void
{
$this->updateStatus();
$this->ticket->messages()->create($this->message);
if ($this->isSenderAdmin()) {
$this->sendNotify();
} else {
$this->createActivity();
}
}
private function sendNotify(): void
{
Notify::to(
$this->ticket->user,
$this->notifyTitle,
$this->message['message'],
route('dashboard.support.view', $this->ticket->ticket_id)
);
}
private function createActivity(): void
{
CreateActivity::for(
$this->ticket->user,
$this->activityType,
$this->ticket->subject,
route('dashboard.support.view', $this->ticket->ticket_id)
);
}
private function isSenderAdmin(): bool
{
return $this->sender === 'admin';
}
private function updateStatus(): void
{
$this->ticket->update([
'status' => $this->status,
]);
}
}
|