| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Api\Classes; |
| 4 |
|
| 5 |
use FluentSupport\App\Http\Controllers\AuthController; |
| 6 |
use FluentSupport\App\Models\Customer; |
| 7 |
use FluentSupport\App\Models\Ticket; |
| 8 |
use FluentSupport\App\Services\Tickets\TicketService; |
| 9 |
|
| 10 |
/** |
| 11 |
* Customers class for PHP API |
| 12 |
* |
| 13 |
* Example Usage: $customersApi = FluentSupportApi('customers'); |
| 14 |
* |
| 15 |
* @package FluentSupport\App\Api\Classes |
| 16 |
* |
| 17 |
* @version 1.0.0 |
| 18 |
*/ |
| 19 |
class Customers |
| 20 |
{ |
| 21 |
private $instance = null; |
| 22 |
|
| 23 |
private $allowedInstanceMethods = [ |
| 24 |
'all', |
| 25 |
'get', |
| 26 |
'find', |
| 27 |
'first', |
| 28 |
'paginate' |
| 29 |
]; |
| 30 |
|
| 31 |
public function __construct(Customer $instance) |
| 32 |
{ |
| 33 |
$this->instance = $instance; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* getCustomers method will return a all available customers |
| 38 |
* |
| 39 |
* @return object |
| 40 |
*/ |
| 41 |
|
| 42 |
public function getCustomers() |
| 43 |
{ |
| 44 |
return Customer::paginate(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* getCustomer method will return a specific customer by id |
| 49 |
* |
| 50 |
* @param int $customerId |
| 51 |
* @return object|boolean |
| 52 |
*/ |
| 53 |
|
| 54 |
public function getCustomer(int $customerId) |
| 55 |
{ |
| 56 |
if (is_numeric($customerId)) { |
| 57 |
return Customer::findOrFail($customerId); |
| 58 |
} |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* updateCustomer method will update the specific customer by id |
| 64 |
* |
| 65 |
* @param array $data |
| 66 |
* @param int $customer_id |
| 67 |
* @return object|boolean |
| 68 |
*/ |
| 69 |
|
| 70 |
public function updateCustomer(array $data, int $customer_id) |
| 71 |
{ |
| 72 |
if (!$customer_id) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
if ($customer = Customer::where('id', $customer_id)->first()) { |
| 76 |
return $customer->update($data); |
| 77 |
} |
| 78 |
|
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* createCustomerWithOrWithoutWpUser method will create a customer |
| 84 |
* also it will create a wp user if you want to create one |
| 85 |
* if you want to create a wp user too then the process will be 1st it will create a wp user |
| 86 |
* after creating the wp user successfully it will create a fluent support customer |
| 87 |
* |
| 88 |
* @param array $data |
| 89 |
* @param bool $createWpUser |
| 90 |
* @return object|boolean |
| 91 |
*/ |
| 92 |
|
| 93 |
public function createCustomerWithOrWithoutWpUser(array $data, $createWpUser = false) |
| 94 |
{ |
| 95 |
if (!$createWpUser) { |
| 96 |
$isExist = Customer::where('email', $data['email'])->first(); |
| 97 |
if (!$data['email'] || !is_email($data['email']) || $isExist) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
return Customer::create($data); |
| 101 |
} |
| 102 |
|
| 103 |
if (!username_exists($data['username'])) { |
| 104 |
$authController = new AuthController(); |
| 105 |
$createdUser = $authController->createUser($data); |
| 106 |
$updateCreatedUser = $authController->maybeUpdateUser($createdUser, $data); |
| 107 |
if ($createdUser) { |
| 108 |
$data['user_id'] = $createdUser; |
| 109 |
return Customer::create($data); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* deleteCustomer method will delete customer with or without customer tickets and attachments |
| 118 |
* @param int $id |
| 119 |
* @param bool $withAssociatedData | this will delete all tickets and attachments of this customer |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public function deleteCustomer($id, $withAssociatedData = false) |
| 123 |
{ |
| 124 |
if (!$id) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
$customer = Customer::findOrFail($id); |
| 129 |
if ($withAssociatedData) { |
| 130 |
$ticketService = new TicketService(); |
| 131 |
$tickets = Ticket::where('customer_id', $id)->get(); |
| 132 |
foreach ($tickets as $ticket) { |
| 133 |
$ticketService->deleteTicket($ticket); |
| 134 |
} |
| 135 |
} |
| 136 |
$customer->delete(); |
| 137 |
} |
| 138 |
|
| 139 |
public function getInstance() |
| 140 |
{ |
| 141 |
return $this->instance; |
| 142 |
} |
| 143 |
|
| 144 |
public function __call($method, $params) |
| 145 |
{ |
| 146 |
if (in_array($method, $this->allowedInstanceMethods)) { |
| 147 |
return call_user_func_array([$this->instance, $method], $params); |
| 148 |
} |
| 149 |
|
| 150 |
throw new \Exception(sprintf('Method %s does not exist.', esc_html($method))); |
| 151 |
} |
| 152 |
} |
| 153 |
|