Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ScheduleApiController | |
0.00% |
0 / 43 |
|
0.00% |
0 / 4 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getTodaySchedule | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| getScheduleForDate | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 | |||
| refreshSchedule | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Workbook\Controllers; |
| 4 | |
| 5 | use BuyerKiosk\Workbook\ScheduleManager; |
| 6 | use BuyerKiosk\Workbook\WorkbookAbly; |
| 7 | use Exception; |
| 8 | |
| 9 | /** |
| 10 | * Schedule System API Controller |
| 11 | * |
| 12 | * Handles REST API endpoints for the Workbook Schedule System including |
| 13 | * schedule retrieval, date-specific queries, and cache refresh. |
| 14 | * |
| 15 | * @package BuyerKiosk\Workbook |
| 16 | */ |
| 17 | class ScheduleApiController |
| 18 | { |
| 19 | /** |
| 20 | * @var \Slim\Slim Slim application instance |
| 21 | */ |
| 22 | private $app; |
| 23 | |
| 24 | /** |
| 25 | * @var \Store Store object |
| 26 | */ |
| 27 | private $store; |
| 28 | |
| 29 | /** |
| 30 | * @var ScheduleManager Schedule manager instance |
| 31 | */ |
| 32 | private $manager; |
| 33 | |
| 34 | /** |
| 35 | * Constructor |
| 36 | * |
| 37 | * @param \Slim\Slim $app Slim application instance |
| 38 | * @param \Store $store Store object |
| 39 | */ |
| 40 | public function __construct($app, \Store $store) |
| 41 | { |
| 42 | $this->app = $app; |
| 43 | $this->store = $store; |
| 44 | $this->manager = new ScheduleManager($app, $store); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * GET /api/:typeNum/workbook/schedule/ |
| 49 | * Get today's schedule for the store |
| 50 | * |
| 51 | * @param string $typeNum Store type number |
| 52 | */ |
| 53 | public function getTodaySchedule(string $typeNum): void |
| 54 | { |
| 55 | try { |
| 56 | $schedule = $this->manager->getTodaySchedule(); |
| 57 | |
| 58 | $this->app->response->headers->set('Content-Type', 'application/json'); |
| 59 | echo json_encode([ |
| 60 | 'success' => true, |
| 61 | 'data' => $schedule |
| 62 | ]); |
| 63 | } catch (Exception $e) { |
| 64 | error_log("ScheduleApiController::getTodaySchedule error: " . $e->getMessage()); |
| 65 | $this->app->halt(500, json_encode([ |
| 66 | 'error' => 'Failed to retrieve today\'s schedule' |
| 67 | ])); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * GET /api/:typeNum/workbook/schedule/:date/ |
| 73 | * Get schedule for a specific date |
| 74 | * |
| 75 | * @param string $typeNum Store type number |
| 76 | * @param string $date Date in Y-m-d format |
| 77 | */ |
| 78 | public function getScheduleForDate(string $typeNum, string $date): void |
| 79 | { |
| 80 | try { |
| 81 | // Validate and parse date |
| 82 | $dateObj = \DateTime::createFromFormat('Y-m-d', $date, new \DateTimeZone($this->store->getTimeZone())); |
| 83 | |
| 84 | if (!$dateObj) { |
| 85 | $this->app->halt(400, json_encode([ |
| 86 | 'error' => 'Invalid date format. Use Y-m-d (e.g., 2025-03-05)' |
| 87 | ])); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | $schedule = $this->manager->getScheduleForDate($dateObj); |
| 92 | |
| 93 | $this->app->response->headers->set('Content-Type', 'application/json'); |
| 94 | echo json_encode([ |
| 95 | 'success' => true, |
| 96 | 'data' => $schedule |
| 97 | ]); |
| 98 | } catch (Exception $e) { |
| 99 | error_log("ScheduleApiController::getScheduleForDate error: " . $e->getMessage()); |
| 100 | $this->app->halt(500, json_encode([ |
| 101 | 'error' => 'Failed to retrieve schedule for date' |
| 102 | ])); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * POST /api/:typeNum/workbook/schedule/refresh/ |
| 108 | * Refresh schedule cache from provider |
| 109 | * |
| 110 | * @param string $typeNum Store type number |
| 111 | */ |
| 112 | public function refreshSchedule(string $typeNum): void |
| 113 | { |
| 114 | try { |
| 115 | $result = $this->manager->refreshSchedule(); |
| 116 | |
| 117 | // Publish to Ably for real-time updates |
| 118 | try { |
| 119 | $ably = new WorkbookAbly($typeNum); |
| 120 | $ably->scheduleRefresh(); |
| 121 | } catch (Exception $e) { |
| 122 | error_log("ScheduleApiController::refreshSchedule Ably error: " . $e->getMessage()); |
| 123 | } |
| 124 | |
| 125 | $this->app->response->headers->set('Content-Type', 'application/json'); |
| 126 | echo json_encode($result); |
| 127 | } catch (Exception $e) { |
| 128 | error_log("ScheduleApiController::refreshSchedule error: " . $e->getMessage()); |
| 129 | $this->app->halt(500, json_encode([ |
| 130 | 'error' => 'Failed to refresh schedule cache' |
| 131 | ])); |
| 132 | } |
| 133 | } |
| 134 | } |