PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 All 69 releases
fluent-support / app / Api / Classes / Customers.php

Customers.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.5, at app/Api/Classes/Customers.php

141 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 PHP API.
12 *
13 * Example Usage: $customersApi = FluentSupportApi('customers');
14 */
15 class Customers
16 {
17 private $instance = null;
18
19 private $allowedInstanceMethods = [
20 'all',
21 'get',
22 'find',
23 'first',
24 'paginate'
25 ];
26
27 public function __construct(Customer $instance)
28 {
29 $this->instance = $instance;
30 }
31
32 /**
33 * Paginated list of customers.
34 *
35 * @return object
36 */
37
38 public function getCustomers()
39 {
40 return Customer::paginate();
41 }
42
43 /**
44 * @param int $customerId
45 * @return object|boolean
46 */
47
48 public function getCustomer(int $customerId)
49 {
50 if (is_numeric($customerId)) {
51 return Customer::findOrFail($customerId);
52 }
53 return false;
54 }
55
56 /**
57 * @param array $data
58 * @param int $customer_id
59 * @return object|boolean
60 */
61
62 public function updateCustomer(array $data, int $customer_id)
63 {
64 if (!$customer_id) {
65 return false;
66 }
67 if ($customer = Customer::where('id', $customer_id)->first()) {
68 return $customer->update($data);
69 }
70
71 return false;
72 }
73
74 /**
75 * Create a customer, optionally creating the WP user first.
76 *
77 * @param array $data
78 * @param bool $createWpUser
79 * @return object|boolean
80 */
81
82 public function createCustomerWithOrWithoutWpUser(array $data, $createWpUser = false)
83 {
84 if (!$createWpUser) {
85 $isExist = Customer::where('email', $data['email'])->first();
86 if (!$data['email'] || !is_email($data['email']) || $isExist) {
87 return false;
88 }
89 return Customer::create($data);
90 }
91
92 if (!username_exists($data['username'])) {
93 $authController = new AuthController();
94 $createdUser = $authController->createUser($data);
95 $updateCreatedUser = $authController->maybeUpdateUser($createdUser, $data);
96 if ($createdUser) {
97 $data['user_id'] = $createdUser;
98 return Customer::create($data);
99 }
100 }
101
102 return false;
103 }
104
105 /**
106 * @param int $id
107 * @param bool $withAssociatedData Also delete the customer's tickets and attachments.
108 * @return void
109 */
110 public function deleteCustomer($id, $withAssociatedData = false)
111 {
112 if (!$id) {
113 return;
114 }
115
116 $customer = Customer::findOrFail($id);
117 if ($withAssociatedData) {
118 $ticketService = new TicketService();
119 $tickets = Ticket::where('customer_id', $id)->get();
120 foreach ($tickets as $ticket) {
121 $ticketService->deleteTicket($ticket);
122 }
123 }
124 $customer->delete();
125 }
126
127 public function getInstance()
128 {
129 return $this->instance;
130 }
131
132 public function __call($method, $params)
133 {
134 if (in_array($method, $this->allowedInstanceMethods)) {
135 return call_user_func_array([$this->instance, $method], $params);
136 }
137
138 throw new \Exception(sprintf('Method %s does not exist.', esc_html($method)));
139 }
140 }
141