| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Api\Classes; |
| 4 |
|
| 5 |
use FluentSupport\App\Models\Agent; |
| 6 |
use FluentSupport\App\Models\Customer; |
| 7 |
use FluentSupport\App\Models\Ticket; |
| 8 |
use FluentSupport\App\Services\Tickets\ResponseService; |
| 9 |
use FluentSupport\App\Services\Tickets\TicketService; |
| 10 |
|
| 11 |
/** |
| 12 |
* Tickets class for PHP API |
| 13 |
* Example Usage: $ticketsApi = FluentSupportApi('tickets'); |
| 14 |
* @package FluentSupport\App\Api\Classes |
| 15 |
* |
| 16 |
* @version 1.0.0 |
| 17 |
*/ |
| 18 |
class Tickets |
| 19 |
{ |
| 20 |
private $instance = null; |
| 21 |
|
| 22 |
private $allowedInstanceMethods = [ |
| 23 |
'all', |
| 24 |
'get', |
| 25 |
'find', |
| 26 |
'first', |
| 27 |
'paginate' |
| 28 |
]; |
| 29 |
|
| 30 |
public function __construct(Ticket $instance) |
| 31 |
{ |
| 32 |
$this->instance = $instance; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* getTickets method will return all tickets |
| 37 |
* |
| 38 |
* @return object |
| 39 |
*/ |
| 40 |
public function getTickets() |
| 41 |
{ |
| 42 |
$ticketsQuery = Ticket::with([ |
| 43 |
'customer' => function ($query) { |
| 44 |
$query->select(['first_name', 'last_name', 'id', 'email']); |
| 45 |
}, 'agent' => function ($query) { |
| 46 |
$query->select(['first_name', 'last_name', 'id', 'email']); |
| 47 |
} |
| 48 |
]) |
| 49 |
->orderBy('id', 'DESC'); |
| 50 |
$tickets = $ticketsQuery->paginate(); |
| 51 |
|
| 52 |
if (defined('FLUENTSUPPORTPRO')) { |
| 53 |
foreach ($tickets as $ticket) { |
| 54 |
$ticket->custom_fields = $ticket->customData(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return $tickets; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* getTicket method will return a specific ticket by id |
| 63 |
* @param int $id |
| 64 |
* @return object|false |
| 65 |
*/ |
| 66 |
|
| 67 |
public function getTicket(int $id) |
| 68 |
{ |
| 69 |
if (is_numeric($id)) { |
| 70 |
$ticket = Ticket::findOrFail($id); |
| 71 |
if (defined('FLUENTSUPPORTPRO')) { |
| 72 |
$ticket->custom_fields = $ticket->customData(); |
| 73 |
} |
| 74 |
return $ticket; |
| 75 |
} |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* addResponse method add response to a ticket by agent and ticket ID |
| 81 |
* @param array $data |
| 82 |
* @param int $agentId |
| 83 |
* @param int $ticketId |
| 84 |
* @return array|boolean |
| 85 |
*/ |
| 86 |
|
| 87 |
public function addResponse(array $data, int $agentId, int $ticketId) |
| 88 |
{ |
| 89 |
if (!$agentId || !$ticketId || !$data['content']) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
$agent = Agent::findOrFail($agentId); |
| 94 |
$ticket = Ticket::findOrFail($ticketId); |
| 95 |
|
| 96 |
if ($agent && $ticket) { |
| 97 |
return (new ResponseService())->createResponse($data, $agent, $ticket); |
| 98 |
} |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* createTicket method will create a new ticket |
| 104 |
* @param array $data |
| 105 |
* @return object| boolean |
| 106 |
*/ |
| 107 |
|
| 108 |
public function createTicket(array $data) |
| 109 |
{ |
| 110 |
$customerId = $data['customer_id'] ?? null; |
| 111 |
|
| 112 |
if (!$customerId || !($customer = Customer::find($customerId))) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
if (empty($data['title']) || empty($data['content'])) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
$createdTicket = (new TicketService())->storeTicket($data, $customer); |
| 121 |
|
| 122 |
if (defined('FLUENTSUPPORTPRO')) { |
| 123 |
$createdTicket->custom_fields = $createdTicket->customData(); |
| 124 |
} |
| 125 |
|
| 126 |
return $createdTicket; |
| 127 |
} |
| 128 |
|
| 129 |
public function getInstance() |
| 130 |
{ |
| 131 |
return $this->instance; |
| 132 |
} |
| 133 |
|
| 134 |
public function __call($method, $params) |
| 135 |
{ |
| 136 |
if (in_array($method, $this->allowedInstanceMethods)) { |
| 137 |
return call_user_func_array([$this->instance, $method], $params); |
| 138 |
} |
| 139 |
|
| 140 |
throw new \Exception(sprintf("Method %s does not exist.", esc_html($method))); |
| 141 |
} |
| 142 |
} |
| 143 |
|