PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.5.4
Fluent Support – Helpdesk & Customer Support Ticket System v1.5.4
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.5.4, at app/Api/Classes/Customers.php

139 lines 3.7 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 class Customers
10 {
11 private $instance = null;
12
13 private $allowedInstanceMethods = [
14 'all',
15 'get',
16 'find',
17 'first',
18 'paginate'
19 ];
20
21 public function __construct(Customer $instance)
22 {
23 $this->instance = $instance;
24 }
25
26 /**
27 * getCustomers method will return a all available customers
28 *
29 * @return object
30 */
31
32 public function getCustomers()
33 {
34 return Customer::paginate();
35 }
36
37 /**
38 * getCustomer method will return a specific customer by id
39 *
40 * @param int $customerId
41 * @return object
42 */
43
44 public function getCustomer(int $customerId)
45 {
46 if(is_numeric($customerId)){
47 return Customer::findOrFail($customerId);
48 }
49 return false;
50 }
51
52 /**
53 * updateCustomer method will update the specific customer by id
54 *
55 * @param array $data
56 * @param int $customer_id
57 * @return object
58 */
59
60 public function updateCustomer(array $data, int $customer_id)
61 {
62 if(!$customer_id) {
63 return false;
64 }
65 if($customer = Customer::where('id', $customer_id)->first()){
66 return $customer->update($data);
67 }
68 return false;
69 }
70
71 /**
72 * createCustomerWithOrWithoutWpUser method will create a customer
73 * also it will create a wp user if you want to create one
74 * if you want to create a wp user too then the process will be 1st it will create a wp user
75 * then after creating the wp user successfully it will create a fluent support customer
76 *
77 * @param array $data
78 * @param bool $createWpUser
79 * @return object
80 */
81
82 public function createCustomerWithOrWithoutWpUser(array $data, bool $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
103 /**
104 * deleteCustomer method will delete customer with or without customer tickets and attachments
105 * @param int $id
106 * @param bool $withAssociatedData | this will delete all tickets and attachments of this customer
107 */
108 public function deleteCustomer(int $id, bool $withAssociatedData=false)
109 {
110 if(!$id){
111 return;
112 }
113
114 $customer = Customer::findOrFail($id);
115 if ($withAssociatedData){
116 $tickets = Ticket::where('customer_id', $id);
117 foreach ($tickets->get() as $ticket){
118 (new \FluentSupport\App\Hooks\Handlers\CleanupHandler())->deleteTicketAttachments($ticket);
119 }
120 $tickets->delete();
121 }
122 $customer->delete();
123 }
124
125 public function getInstance()
126 {
127 return $this->instance;
128 }
129
130 public function __call($method, $params)
131 {
132 if (in_array($method, $this->allowedInstanceMethods)) {
133 return call_user_func_array([$this->instance, $method], $params);
134 }
135
136 throw new \Exception("Method {$method} does not exist.");
137 }
138 }
139