Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 58 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| CashActivityController | |
0.00% |
0 / 58 |
|
0.00% |
0 / 8 |
420 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| create | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| get | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getAll | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| update | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| delete | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getActivityByDateRange | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| getDailyTransactions | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace BuyerKiosk\Cash\Controllers; |
| 4 | |
| 5 | |
| 6 | use BuyerKiosk\Cash\CashActivityMapper; |
| 7 | class CashActivityController { |
| 8 | private $repository; |
| 9 | private $mapper; |
| 10 | |
| 11 | public function __construct(CashActivityRepository $repository, CashActivityMapper $mapper = null) { |
| 12 | $this->repository = $repository; |
| 13 | if($mapper != null) { |
| 14 | $this->mapper = $mapper; |
| 15 | } else { |
| 16 | $mapper = new CashActivityMapper(); |
| 17 | } |
| 18 | |
| 19 | } |
| 20 | |
| 21 | |
| 22 | public function create($jsonData) { |
| 23 | try { |
| 24 | // Map JSON to CashActivity object |
| 25 | $activity = $this->mapper->mapFromJson($jsonData); |
| 26 | |
| 27 | // Save to database |
| 28 | $success = $this->repository->save($activity); |
| 29 | |
| 30 | if ($success) { |
| 31 | return ['status' => 'success', 'message' => 'Activity created successfully']; |
| 32 | } |
| 33 | |
| 34 | return ['status' => 'error', 'message' => 'Failed to create activity']; |
| 35 | |
| 36 | } catch (\Exception $e) { |
| 37 | return ['status' => 'error', 'message' => $e->getMessage()]; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public function get($id) { |
| 42 | try { |
| 43 | $activity = $this->repository->findById($id); |
| 44 | |
| 45 | if (!$activity) { |
| 46 | return ['status' => 'error', 'message' => 'Activity not found']; |
| 47 | } |
| 48 | |
| 49 | return ['status' => 'success', 'data' => $activity->toArray()]; |
| 50 | |
| 51 | } catch (\Exception $e) { |
| 52 | return ['status' => 'error', 'message' => $e->getMessage()]; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public function getAll() { |
| 57 | try { |
| 58 | $activities = $this->repository->findAll(); |
| 59 | return [ |
| 60 | 'status' => 'success', |
| 61 | 'data' => array_map(function($activity) { |
| 62 | return $activity->toArray(); |
| 63 | }, $activities) |
| 64 | ]; |
| 65 | |
| 66 | } catch (\Exception $e) { |
| 67 | return ['status' => 'error', 'message' => $e->getMessage()]; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public function update($id, $jsonData) { |
| 72 | try { |
| 73 | // Map JSON to CashActivity object |
| 74 | $activity = $this->mapper->mapFromJson($jsonData); |
| 75 | $activity->setId($id); |
| 76 | |
| 77 | // Update in database |
| 78 | $success = $this->repository->update($activity); |
| 79 | |
| 80 | if ($success) { |
| 81 | return ['status' => 'success', 'message' => 'Activity updated successfully']; |
| 82 | } |
| 83 | |
| 84 | return ['status' => 'error', 'message' => 'Failed to update activity']; |
| 85 | |
| 86 | } catch (\Exception $e) { |
| 87 | return ['status' => 'error', 'message' => $e->getMessage()]; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | public function delete($id) { |
| 92 | try { |
| 93 | $success = $this->repository->delete($id); |
| 94 | |
| 95 | if ($success) { |
| 96 | return ['status' => 'success', 'message' => 'Activity deleted successfully']; |
| 97 | } |
| 98 | |
| 99 | return ['status' => 'error', 'message' => 'Failed to delete activity']; |
| 100 | |
| 101 | } catch (\Exception $e) { |
| 102 | return ['status' => 'error', 'message' => $e->getMessage()]; |
| 103 | } |
| 104 | } |
| 105 | // Add this new method to CashActivityController.php |
| 106 | public function getActivityByDateRange($startDate, $endDate) { |
| 107 | try { |
| 108 | $activities = $this->repository->findByDateRange($startDate, $endDate); |
| 109 | |
| 110 | return [ |
| 111 | 'status' => 'success', |
| 112 | 'data' => array_map(function($activity) { |
| 113 | return $activity->toArray(); |
| 114 | }, $activities), |
| 115 | ]; |
| 116 | |
| 117 | } catch (\Exception $e) { |
| 118 | return ['status' => 'error', 'message' => $e->getMessage()]; |
| 119 | } |
| 120 | } |
| 121 | public function getDailyTransactions($date) { |
| 122 | try { |
| 123 | $transactions = $this->repository->findByDate($date); |
| 124 | return [ |
| 125 | 'status' => 'success', |
| 126 | 'data' => array_map(function($transaction) { |
| 127 | return $transaction->toArray(); |
| 128 | }, $transactions) |
| 129 | ]; |
| 130 | } catch (\Exception $e) { |
| 131 | return ['status' => 'error', 'message' => $e->getMessage()]; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | } |