| 1 |
<?php |
| 2 |
/** |
| 3 |
* Faker api |
| 4 |
* |
| 5 |
* @package Timetics |
| 6 |
*/ |
| 7 |
namespace Timetics\Core\DummyData; |
| 8 |
|
| 9 |
use Timetics\Base\Api; |
| 10 |
use Timetics\Core\Appointments\Appointment; |
| 11 |
use Timetics\Core\Bookings\Booking; |
| 12 |
use Timetics\Core\Customers\Customer; |
| 13 |
use Timetics\Core\DummyData\Factory; |
| 14 |
use Timetics\Core\Staffs\Staff; |
| 15 |
use Timetics\Utils\Singleton; |
| 16 |
|
| 17 |
class Api_Faker extends Api { |
| 18 |
use Singleton; |
| 19 |
|
| 20 |
/** |
| 21 |
* Store api namespace |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $namespace = 'timetics/v1'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Store rest base |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $rest_base = 'faker'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Register rest routes |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function register_routes() { |
| 40 |
/** |
| 41 |
* Register route |
| 42 |
* |
| 43 |
* @var void |
| 44 |
*/ |
| 45 |
register_rest_route( |
| 46 |
$this->namespace, $this->rest_base, [ |
| 47 |
[ |
| 48 |
'methods' => \WP_REST_Server::READABLE, |
| 49 |
'callback' => [$this, 'generate_faker'], |
| 50 |
'permission_callback' => function () { |
| 51 |
return current_user_can( 'manage_timetics' ); |
| 52 |
}, |
| 53 |
], |
| 54 |
] |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get all bookings |
| 60 |
* |
| 61 |
* @param WP_Rest_Request $request |
| 62 |
* |
| 63 |
* @return JSON |
| 64 |
*/ |
| 65 |
public function generate_faker( $request ) { |
| 66 |
$this->generate_staff(); |
| 67 |
$this->generate_customer(); |
| 68 |
$this->genere_meeting(); |
| 69 |
$this->generate_booking(); |
| 70 |
|
| 71 |
$data = [ |
| 72 |
'status' => 1, |
| 73 |
'status_code' => 200, |
| 74 |
'message' => __( 'Successfully generated dummy data', 'timetics' ), |
| 75 |
]; |
| 76 |
|
| 77 |
return rest_ensure_response( $data ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Generate meetings |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
private function genere_meeting() { |
| 86 |
$items = 1; |
| 87 |
$factory = Factory::create(); |
| 88 |
$visibility = ['enabled', 'disabled']; |
| 89 |
$staff = $this->get_staff(); |
| 90 |
|
| 91 |
for ( $i = 1; $i <= $items; $i++ ) { |
| 92 |
$meeting = new Appointment(); |
| 93 |
|
| 94 |
$appointment_data = [ |
| 95 |
'name' => $factory->sentence, |
| 96 |
'description' => $factory->paragraph( 7 ), |
| 97 |
'type' => 'one-to-one', |
| 98 |
'locations' => $this->get_locations( $factory->address ), |
| 99 |
'staff' => [$staff], |
| 100 |
'duration' => $this->get_duration(), |
| 101 |
'price' => $this->get_price(), |
| 102 |
'capacity' => 1, |
| 103 |
'visibility' => $visibility[array_rand( $visibility )], |
| 104 |
'schedule' => $this->get_schedule( $staff ), |
| 105 |
'timezone' => 'Asia/Dhaka', |
| 106 |
'availability' => $this->get_availability(), |
| 107 |
]; |
| 108 |
|
| 109 |
$meeting->set_props( $appointment_data ); |
| 110 |
$meeting->save(); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Generate staff |
| 116 |
* |
| 117 |
* @return void |
| 118 |
*/ |
| 119 |
private function generate_staff() { |
| 120 |
$items = 1; |
| 121 |
$factory = Factory::create(); |
| 122 |
|
| 123 |
for ( $i = 1; $i <= $items; $i++ ) { |
| 124 |
$staff = new Staff(); |
| 125 |
$args = [ |
| 126 |
'first_name' => $factory->first_name, |
| 127 |
'last_name' => $factory->last_name, |
| 128 |
'user_email' => $factory->email, |
| 129 |
'user_login' => $factory->username, |
| 130 |
'phone' => $factory->phone_number, |
| 131 |
'schedule' => timetics_get_option( 'availability' ), |
| 132 |
'user_pass' => $factory->password, |
| 133 |
]; |
| 134 |
|
| 135 |
$staff->create( $args ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Generate customer |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
private function generate_customer() { |
| 145 |
$items = 1; |
| 146 |
$factory = Factory::create(); |
| 147 |
|
| 148 |
for ( $i = 1; $i <= $items; $i++ ) { |
| 149 |
$customer = new Customer(); |
| 150 |
$args = [ |
| 151 |
'first_name' => $factory->first_name, |
| 152 |
'last_name' => $factory->last_name, |
| 153 |
'email' => $factory->email, |
| 154 |
'user_name' => $factory->username, |
| 155 |
'phone' => $factory->phone_number, |
| 156 |
'password' => $factory->password, |
| 157 |
]; |
| 158 |
|
| 159 |
$customer->set_props( $args ); |
| 160 |
$customer->save(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
private function generate_booking() { |
| 165 |
$item = 1; |
| 166 |
$factory = Factory::create(); |
| 167 |
|
| 168 |
for ( $i = 1; $i <= $item; $i++ ) { |
| 169 |
$booking = new Booking(); |
| 170 |
$customer = new Customer( $this->get_customer() ); |
| 171 |
$staff = new Staff( $this->get_staff() ); |
| 172 |
$meeting = new Appointment( $this->get_meeting() ); |
| 173 |
|
| 174 |
$args = [ |
| 175 |
'customer' => $customer->get_id(), |
| 176 |
'appointment' => $meeting->get_id(), |
| 177 |
'staff' => $staff->get_id(), |
| 178 |
'customer_fname' => $customer->get_first_name(), |
| 179 |
'customer_lname' => $customer->get_last_name(), |
| 180 |
'customer_email' => $customer->get_email(), |
| 181 |
'customer_phone' => $customer->get_phone(), |
| 182 |
'staff_fname' => $staff->get_first_name(), |
| 183 |
'staff_lname' => $staff->get_last_name(), |
| 184 |
'staff_email' => $staff->get_email(), |
| 185 |
'meeting_name' => $meeting->get_name(), |
| 186 |
'meeting_description' => $meeting->get_description(), |
| 187 |
'meeting_type' => $meeting->get_type(), |
| 188 |
'description' => $meeting->get_description(), |
| 189 |
'start_date' => $factory->date, |
| 190 |
'date' => $factory->date, |
| 191 |
'end_date' => $factory->date, |
| 192 |
'start_time' => $factory->time, |
| 193 |
'end_time' => $factory->time, |
| 194 |
'order_total' => $meeting->get_price(), |
| 195 |
'post_status' => 'completed', |
| 196 |
'location' => $factory->address, |
| 197 |
'location_type' => 'in-person', |
| 198 |
'timezone' => 'Asia/Dhaka', |
| 199 |
]; |
| 200 |
|
| 201 |
$booking->set_props( $args ); |
| 202 |
|
| 203 |
$booking->save(); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Get meeting |
| 209 |
* |
| 210 |
* @return integer |
| 211 |
*/ |
| 212 |
private function get_meeting() { |
| 213 |
$meetings = $this->get_meeting_ids(); |
| 214 |
|
| 215 |
return $meetings[array_rand( $meetings )]; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Get staff id |
| 220 |
* |
| 221 |
* @return integer |
| 222 |
*/ |
| 223 |
private function get_staff() { |
| 224 |
$staff = $this->get_staff_ids(); |
| 225 |
|
| 226 |
return $staff[array_rand( $staff )]; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Get customer id |
| 231 |
* |
| 232 |
* @return integer |
| 233 |
*/ |
| 234 |
private function get_customer() { |
| 235 |
$customers = $this->get_customer_ids(); |
| 236 |
|
| 237 |
return $customers[array_rand( $customers )]; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Get staff ids |
| 242 |
* |
| 243 |
* @return array |
| 244 |
*/ |
| 245 |
private function get_staff_ids() { |
| 246 |
$staff = new Staff(); |
| 247 |
|
| 248 |
$users = $staff->all(); |
| 249 |
|
| 250 |
$ids = []; |
| 251 |
|
| 252 |
foreach ( $users['items'] as $user ) { |
| 253 |
$ids[] = $user->ID; |
| 254 |
} |
| 255 |
|
| 256 |
return $ids; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Get all meeting ids |
| 261 |
* |
| 262 |
* @return array |
| 263 |
*/ |
| 264 |
private function get_meeting_ids() { |
| 265 |
$ids = []; |
| 266 |
$meeting = new Appointment(); |
| 267 |
$meetings = $meeting->all(); |
| 268 |
|
| 269 |
foreach ( $meetings['items'] as $meeting_ob ) { |
| 270 |
$ids[] = $meeting_ob->ID; |
| 271 |
} |
| 272 |
|
| 273 |
return $ids; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Get customer ids |
| 278 |
* |
| 279 |
* @return array |
| 280 |
*/ |
| 281 |
private function get_customer_ids() { |
| 282 |
$customer = new Customer(); |
| 283 |
|
| 284 |
$users = $customer->all(); |
| 285 |
|
| 286 |
$ids = []; |
| 287 |
|
| 288 |
foreach ( $users['items'] as $user ) { |
| 289 |
$ids[] = $user->ID; |
| 290 |
} |
| 291 |
|
| 292 |
return $ids; |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Generate meeting schedule |
| 297 |
* |
| 298 |
* @return array |
| 299 |
*/ |
| 300 |
private function get_schedule( $staff ) { |
| 301 |
return [ |
| 302 |
$staff => [ |
| 303 |
'Sun' => [ |
| 304 |
[ |
| 305 |
'start' => '9:00am', |
| 306 |
'end' => '5:00pm', |
| 307 |
], |
| 308 |
], |
| 309 |
'Mon' => [ |
| 310 |
[ |
| 311 |
'start' => '9:00am', |
| 312 |
'end' => '5:00pm', |
| 313 |
], |
| 314 |
], |
| 315 |
'Tue' => [ |
| 316 |
[ |
| 317 |
'start' => '9:00am', |
| 318 |
'end' => '5:00pm', |
| 319 |
], |
| 320 |
], |
| 321 |
'Wed' => [ |
| 322 |
[ |
| 323 |
'start' => '9:00am', |
| 324 |
'end' => '5:00pm', |
| 325 |
], |
| 326 |
], |
| 327 |
'Thu' => [ |
| 328 |
[ |
| 329 |
'start' => '9:00am', |
| 330 |
'end' => '5:00pm', |
| 331 |
], |
| 332 |
], |
| 333 |
'Fri' => [ |
| 334 |
[ |
| 335 |
'start' => '9:00am', |
| 336 |
'end' => '5:00pm', |
| 337 |
], |
| 338 |
], |
| 339 |
'Sat' => [ |
| 340 |
[ |
| 341 |
'start' => '9:00am', |
| 342 |
'end' => '5:00pm', |
| 343 |
], |
| 344 |
], |
| 345 |
], |
| 346 |
]; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Generate locations |
| 351 |
* |
| 352 |
* @param string $address |
| 353 |
* |
| 354 |
* @return string |
| 355 |
*/ |
| 356 |
private function get_locations( $address ) { |
| 357 |
return [ |
| 358 |
[ |
| 359 |
'location' => $address, |
| 360 |
'location_type' => 'inperson', |
| 361 |
], |
| 362 |
]; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Generate availability |
| 367 |
* |
| 368 |
* @return array |
| 369 |
*/ |
| 370 |
private function get_availability() { |
| 371 |
return [ |
| 372 |
'start' => gmdate( 'Y-m-d' ), |
| 373 |
'end' => gmdate( 'Y-m-d', strtotime( '+1 month', time() ) ), |
| 374 |
]; |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Get generate price |
| 379 |
* |
| 380 |
* @return array |
| 381 |
*/ |
| 382 |
private function get_price() { |
| 383 |
return [ |
| 384 |
[ |
| 385 |
'ticket_name' => 'default', |
| 386 |
'ticket_price' => random_int( 30, 100 ), |
| 387 |
'ticket_quantity' => 1, |
| 388 |
], |
| 389 |
]; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Get meeting duration |
| 394 |
* |
| 395 |
* @return string |
| 396 |
*/ |
| 397 |
private function get_duration() { |
| 398 |
$numbers = [15, 30, 60]; |
| 399 |
$number = $numbers[array_rand( $numbers )]; |
| 400 |
$unit = 'minute'; |
| 401 |
|
| 402 |
$duration = "$number $unit"; |
| 403 |
|
| 404 |
return $duration; |
| 405 |
} |
| 406 |
} |
| 407 |
|