ClientFavoriteProviderResource.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Http\Resources;
  3. use App\Enums\GenderEnum;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. use Illuminate\Support\Facades\Storage;
  7. class ClientFavoriteProviderResource extends JsonResource
  8. {
  9. public function toArray(Request $request): array
  10. {
  11. $provider = $this->resource->relationLoaded('provider') ? $this->resource->provider : null;
  12. $profileMediaPath = $this->profile_media_path
  13. ?? ($provider?->relationLoaded('profileMedia') ? $provider->profileMedia?->path : null);
  14. $dailyPrice8h = $this->daily_price_8h ?? $provider?->daily_price_8h;
  15. $dailyPrice6h = $this->daily_price_6h ?? $provider?->daily_price_6h;
  16. $dailyPrice4h = $this->daily_price_4h ?? $provider?->daily_price_4h;
  17. $dailyPrice2h = $this->daily_price_2h ?? $provider?->daily_price_2h;
  18. $gender = $this->gender ?? $provider?->gender?->value;
  19. return [
  20. 'id' => $this->id,
  21. 'client_id' => $this->client_id,
  22. 'provider_id' => $this->provider_id,
  23. 'provider_name' => $this->provider_name ?? ($provider?->relationLoaded('user') ? $provider->user?->name : null),
  24. 'gender' => $gender,
  25. 'gender_label' => $gender ? GenderEnum::tryFrom($gender)?->label() : null,
  26. 'city_name' => $this->city_name ?? null,
  27. 'average_rating' => $this->average_rating ?? $provider?->average_rating,
  28. 'profile_media_url' => $profileMediaPath
  29. ? Storage::temporaryUrl($profileMediaPath, now()->addMinutes(60))
  30. : null,
  31. 'daily_price_8h_base' => $dailyPrice8h,
  32. 'daily_price_6h_base' => $dailyPrice6h,
  33. 'daily_price_4h_base' => $dailyPrice4h,
  34. 'daily_price_2h_base' => $dailyPrice2h,
  35. 'daily_price_8h' => $this->applyCreditCardFee($dailyPrice8h),
  36. 'daily_price_6h' => $this->applyCreditCardFee($dailyPrice6h),
  37. 'daily_price_4h' => $this->applyCreditCardFee($dailyPrice4h),
  38. 'daily_price_2h' => $this->applyCreditCardFee($dailyPrice2h),
  39. 'total_services ' => $this->total_services ?? $provider?->total_services,
  40. 'notes' => $this->notes,
  41. 'created_at' => $this->created_at?->format('Y-m-d H:i'),
  42. 'updated_at' => $this->updated_at?->format('Y-m-d H:i'),
  43. ];
  44. }
  45. private function applyCreditCardFee(mixed $price): ?float
  46. {
  47. if ($price === null) {
  48. return null;
  49. }
  50. $rate = config('services.pagarme.platform_credit_card_fee_rate', 0.16);
  51. if (is_string($rate)) {
  52. $rate = str_replace(',', '.', trim($rate));
  53. }
  54. $rate = (float) $rate;
  55. $rate = $rate > 1 ? $rate / 100 : $rate;
  56. return round((float) $price * (1 + $rate), 2);
  57. }
  58. }