PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.0
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.0
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 1.5.5 All 68 releases
fluent-support / app / Api / Classes / Customers.php

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

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