-
Notifications
You must be signed in to change notification settings - Fork 1
/
Customers.php
206 lines (184 loc) · 5.13 KB
/
Customers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace TheBooking;
use TheBooking\Bus\Commands\EditCustomer;
use TheBooking\Classes\Customer;
use VSHM_Framework\db;
defined('ABSPATH') || exit;
/**
* Class Customers
*
* @package TheBooking
*/
class Customers
{
/**
* @var Customer[]
*/
private $customers = [];
/**
* @var string
*/
public static $table_name = 'tbkl_customers';
/**
* @var string[]
*/
public static $table_structure = [
'customer_name' => ['type' => 'text', 'null' => TRUE],
'email' => 'varchar',
'access_token' => 'varchar',
'phone' => ['type' => 'varchar', 'null' => TRUE],
'wp_user' => 'int',
'created' => 'int',
'birthday' => ['type' => 'varchar', 'null' => TRUE],
'timezone' => ['type' => 'varchar', 'null' => TRUE],
'updated' => 'timestamp'
];
protected function __construct()
{
tbkg()->loader->add_action('tbk-loaded', $this, 'gather');
tbkg()->loader->add_action('profile_update', $this, 'wpProfileIsUpdated', 10, 2);
tbkg()->loader->add_action('deleted_user', $this, 'wpProfileIsDeleted', 10, 3);
}
/**
* @return string[]
*/
public static function getSchema()
{
return self::$table_structure;
}
public function gather()
{
$this->customers = [];
$records = db::select(self::$table_name, '*', [], FALSE);
if ($records instanceof \WP_Error) {
switch ($records->get_error_code()) {
case 404:
/**
* Database table is not found.
*/
db::create_table(self::$table_name, self::$table_structure);
break;
default:
break;
}
return;
}
foreach ($records as $record) {
$customer = new Customer();
$customer->name($record->customer_name);
$customer->email($record->email);
$customer->birthday($record->birthday);
$customer->phone($record->phone);
$customer->wp_user($record->wp_user);
$customer->access_token($record->access_token);
$customer->id($record->id);
$customer->timezone($record->timezone);
$this->customers[ $record->id ] = $customer;
}
}
public function delete($id)
{
db::delete(self::$table_name, ['id' => $id]);
unset($this->customers[ $id ]);
}
/**
* When a WordPress user is deleted, apply changes to the mapped customer.
*
* @param $user_id
* @param $user_reassigned
* @param \WP_User $old_user
*/
public function wpProfileIsDeleted($user_id, $user_reassigned, $old_user)
{
foreach (tbkg()->customers->all() as $customer) {
if ($customer->wp_user() === $user_id) {
$command = new EditCustomer(
$customer->name(),
$customer->email(),
$customer->phone(),
$user_reassigned ?: 0,
$customer->birthday(),
$customer->id(),
$customer->timezone()
);
tbkg()->bus->dispatch($command);
}
}
}
/**
* When a WordPress user is updated, apply changes to the mapped customer.
*
* @param $user_id
* @param \WP_User $old_data
*/
public function wpProfileIsUpdated($user_id, $old_data)
{
foreach (tbkg()->customers->all() as $customer) {
if ($customer->wp_user() === $user_id) {
$user = get_user_by('ID', $user_id);
$command = new EditCustomer(
$user->display_name,
$user->user_email,
$customer->phone(),
$user_id,
$customer->birthday(),
$customer->id(),
$customer->timezone()
);
tbkg()->bus->dispatch($command);
}
}
}
/**
* @return Customer[]
*/
public function all()
{
return $this->customers;
}
/**
* @param int $id
*
* @return FALSE|Customer
*/
public function get($id)
{
if (!isset($this->customers[ $id ])) {
return FALSE;
}
return $this->customers[ $id ];
}
/**
* Returns a customer from a WordPress User ID.
*
* @param int $wp_id
*
* @return Customer
*/
public function getByWpId($wp_id)
{
if ($wp_id === 0) {
return NULL;
}
foreach ($this->customers as $customer) {
if ($customer->wp_user() === $wp_id) {
return $customer;
}
}
return NULL;
}
/**
* @return int
*/
public function count()
{
return count($this->customers);
}
/**
* @return self
*/
public static function instance()
{
return new self();
}
}