| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Services; |
| 6 |
|
| 7 |
use Yatra\Repositories\SavedTripRepository; |
| 8 |
|
| 9 |
/** |
| 10 |
* Saved Trip Service |
| 11 |
* |
| 12 |
* Contains business logic for saved trips/wishlist. |
| 13 |
* |
| 14 |
* @package Yatra\Services |
| 15 |
*/ |
| 16 |
class SavedTripService |
| 17 |
{ |
| 18 |
private SavedTripRepository $repository; |
| 19 |
|
| 20 |
public function __construct() |
| 21 |
{ |
| 22 |
$this->repository = new SavedTripRepository(); |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Check if trip is saved by user |
| 28 |
* |
| 29 |
* @param int $userId User ID |
| 30 |
* @param int $tripId Trip ID |
| 31 |
* @return bool |
| 32 |
*/ |
| 33 |
public function isSaved(int $userId, int $tripId): bool |
| 34 |
{ |
| 35 |
return $this->repository->isSaved($userId, $tripId); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Save trip for user |
| 40 |
* |
| 41 |
* @param int $userId User ID |
| 42 |
* @param int $tripId Trip ID |
| 43 |
* @return array {success: bool, message: string} |
| 44 |
*/ |
| 45 |
public function saveTrip(int $userId, int $tripId): array |
| 46 |
{ |
| 47 |
$tripRepository = new \Yatra\Repositories\TripRepository(); |
| 48 |
$trip = $tripRepository->find($tripId); |
| 49 |
|
| 50 |
if (!$trip) { |
| 51 |
return [ |
| 52 |
'success' => false, |
| 53 |
'message' => __('Trip not found.', 'yatra'), |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
// Block only non-public statuses; allow publish/published, empty/legacy rows, and custom “live” labels. |
| 58 |
$status = strtolower(trim((string) ($trip->status ?? ''))); |
| 59 |
$blocked = ['draft', 'archived', 'trash', 'cancelled', 'canceled', 'pending', 'private']; |
| 60 |
if ($status !== '' && in_array($status, $blocked, true)) { |
| 61 |
return [ |
| 62 |
'success' => false, |
| 63 |
'message' => __('Trip is not available.', 'yatra'), |
| 64 |
]; |
| 65 |
} |
| 66 |
|
| 67 |
$result = $this->repository->saveTrip($userId, $tripId); |
| 68 |
|
| 69 |
if ($result) { |
| 70 |
return [ |
| 71 |
'success' => true, |
| 72 |
'message' => __('Trip saved to wishlist.', 'yatra'), |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
return [ |
| 77 |
'success' => false, |
| 78 |
'message' => __('Failed to save trip.', 'yatra'), |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Remove saved trip |
| 84 |
* |
| 85 |
* @param int $userId User ID |
| 86 |
* @param int $tripId Trip ID |
| 87 |
* @return array {success: bool, message: string} |
| 88 |
*/ |
| 89 |
public function removeTrip(int $userId, int $tripId): array |
| 90 |
{ |
| 91 |
if (!$this->repository->isSaved($userId, $tripId)) { |
| 92 |
return [ |
| 93 |
'success' => true, |
| 94 |
'message' => __('Trip removed from wishlist.', 'yatra'), |
| 95 |
]; |
| 96 |
} |
| 97 |
|
| 98 |
$result = $this->repository->removeTrip($userId, $tripId); |
| 99 |
|
| 100 |
if ($result) { |
| 101 |
return [ |
| 102 |
'success' => true, |
| 103 |
'message' => __('Trip removed from wishlist.', 'yatra'), |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
return [ |
| 108 |
'success' => false, |
| 109 |
'message' => __('Failed to remove trip.', 'yatra'), |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get user's saved trips |
| 115 |
* |
| 116 |
* @param int $userId User ID |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
public function getUserSavedTrips(int $userId): array |
| 120 |
{ |
| 121 |
return $this->repository->getUserSavedTrips($userId); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Get count of saved trips |
| 126 |
* |
| 127 |
* @param int $userId User ID |
| 128 |
* @return int |
| 129 |
*/ |
| 130 |
public function getCount(int $userId): int |
| 131 |
{ |
| 132 |
return $this->repository->getCount($userId); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|