File manager - Edit - /var/www/html/pcadigital/api/app/Http/Services/OccurrenceService.php
Back
<?php namespace App\Http\Services; use App\Classes\BaseServiceResponse; use App\Enums\OccurrenceStatusEnum; use App\Http\Requests\OccurrenceRequest; use App\Http\Resources\OccurrenceResource; use App\Models\Address; use App\Models\Category; use App\Models\Comment; use App\Models\Historic; use App\Models\Occurrence; use App\Models\OccurrenceImage; use App\Models\SubCategory; use App\Repositories\OccurrenceRepository; use Carbon\Carbon; use Exception; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; use Symfony\Component\HttpKernel\Exception\HttpException; class OccurrenceService extends BaseService { private OccurrenceRepository $occurrenceRepository; private NotificationService $notificationService; public function __construct() { $this->occurrenceRepository = new OccurrenceRepository(); $this->notificationService = new NotificationService(); } public function index(): BaseServiceResponse { $occurrences = Occurrence::all(); return $this->success(OccurrenceResource::collection($occurrences)); } public function store(OccurrenceRequest $request): BaseServiceResponse { $status = $this->validateCategories($request); $occurrence = $this->createOccurrenceWithStatus($request, $status); return $this->success(new OccurrenceResource($occurrence)); } public function show(Occurrence $occurrence): BaseServiceResponse { return $this->success( ['category' => new OccurrenceResource($occurrence)] ); } public function getAllResolvedOccurrencesByUser(string $id) { $occurrences = $this->occurrenceRepository->getResolvedOccurrencesByUser($id); $filterOccurrences = $this->filterResolvedOccurrences($occurrences); return $filterOccurrences; } private function validateCategories(OccurrenceRequest $request): OccurrenceStatusEnum { if (isset($request['category_id']) && !isset($request['sub_category_id'])) { return OccurrenceStatusEnum::WITHOUT_ORGAN; } if (isset($request['sub_category_id'])) { return OccurrenceStatusEnum::OPEN; } return OccurrenceStatusEnum::OMBUDSMAN; } private function createOccurrenceWithStatus(OccurrenceRequest $request, OccurrenceStatusEnum $status): Occurrence { try { DB::beginTransaction(); $data = $request->all(); $occurrence = $this->createOccurrence($data); $this->createAddres($data, $occurrence); $historic = new Historic(); $historic->occurrence()->associate($occurrence); $historic->status = $status; $historic->created_by = $data['created_by']; $historic->status = $status; $historic->is_send_to_citizen = false; if ($status == OccurrenceStatusEnum::OPEN) { $category = Category::find($request['category_id']); $subCategory = SubCategory::find($request['sub_category_id']); $historic->organ_name = $subCategory->organ_name; $historic->organ_id = $subCategory->organ_id; $historic->category()->associate($category); $historic->subCategory()->associate($subCategory); } if ($status == OccurrenceStatusEnum::OMBUDSMAN) { $historic->others = $request['others']; } if ($status == OccurrenceStatusEnum::WITHOUT_ORGAN) { $category = Category::find($request['category_id']); $historic->category()->associate($category); } $historic->save(); foreach ($data['images'] as $fileBase64) { $isBase64 = isBase64($fileBase64); if ($isBase64) { $size_in_bytes = (int) (strlen(rtrim($fileBase64, '=')) * 3 / 4); $size_in_kb = $size_in_bytes / 1024; $size_in_mb = $size_in_kb / 1024; if ($size_in_mb > 10) { throw new Exception('Image cannot be size more than 10mb.', JsonResponse::HTTP_NOT_ACCEPTABLE); } $types = ['jpg', 'png', 'jpeg']; $extension = explode('/', explode(':', substr($fileBase64, 0, strpos($fileBase64, ';')))[1])[1]; if (in_array($extension, $types) == false) { throw new Exception('Only jpg, jpeg and png images can be saved.', JsonResponse::HTTP_NOT_ACCEPTABLE); } $replace = substr($fileBase64, 0, strpos($fileBase64, ',') + 1); $file = str_replace($replace, '', $fileBase64); $file = str_replace(' ', '+', $file); $fileName = Carbon::now()->format('d-m-Y') . '_' . uniqid() . '.' . $extension; Storage::disk('public')->put($fileName, base64_decode($file), ['visibility' => 'public', 'directory_visibility' => 'public']); $occurrenceImage = new OccurrenceImage(); $occurrenceImage->image_url = "/storage/" . $fileName; $occurrenceImage->name = $fileName; $occurrenceImage->occurrence()->associate($occurrence); $occurrenceImage->save(); } else { throw new Exception('The file is not a base64', JsonResponse::HTTP_NOT_ACCEPTABLE); } } $this->notificationService->createWhenHasNewOccurrence($historic, $request->bearerToken()); } catch (Exception $exception) { DB::rollBack(); throw $exception; } DB::commit(); return $occurrence; } /* $data array has: [ 'name' => $data['name'], 'description' => $data['description'], 'phone' => $data['phone'], 'email' => $data['email'], 'code' => Carbon::now()->format('His') . uniqid(), 'created_by' => $data['created_by'], 'latitude' => $data['latitude'], 'longitude' => $data['longitude'], ] */ private function createOccurrence(array $data): Occurrence { $data['code'] = Carbon::now()->format('His') . uniqid(); return Occurrence::create($data); } private function createAddres(array $data, Occurrence $occurrence): Address { $address = new Address(); $address->street = $data['street']; $address->number = $data['number']; $address->city = $data['city']; $address->zip_code = $data['zip_code']; $address->state = $data['state']; $address->neighborhood = $data['neighborhood']; $address->reference_point = $data['reference_point']; $address->occurrence()->associate($occurrence); $address->save(); return $address; } private function filterResolvedOccurrences(array $allOccurrences) { $filtred = []; foreach ($allOccurrences as $key => $occurrence) { if ($occurrence->status == OccurrenceStatusEnum::DISAPPROVED->value && $occurrence->final_comment == null) { continue; } $occurrenceImages = OccurrenceImage::where(['occurrence_id' => $occurrence->occurrence_id])->get(); $comments = Comment::where(['occurrence_id' => $occurrence->occurrence_id])->get(); $occurrence = collect($occurrence)->toArray(); $occurrence['images'] = $occurrenceImages; $occurrence['comments'] = $comments; $filtred[] = $occurrence; } $paginated = paginate($filtred); return $paginated; } public function getAllFinished(): BaseServiceResponse { $occurrences = $this->occurrenceRepository->getResolvedOccurrences(); $data = []; foreach ($occurrences as $occurrence) { $historic = Historic::find(['id' => $occurrence->historic_id])->first(); $category = null; $subCategory = null; if ($historic->category_id) { $category = Category::find(['id' => $historic->category_id])->first(); } if ($historic->sub_category_id) { $subCategory = SubCategory::find(['id' => $historic->sub_category_id])->first(); } $address = Address::find(['occurrence_id' => $occurrence->id]); $data[] = [ 'status' => $historic->status, 'category' => $category->name, 'subcategory' => $subCategory->name, 'address' => $address, 'latitude'=> $occurrence->latitude, 'longitude'=> $occurrence->longitude, ]; } return $this->success(['occurrences' => $data], JsonResponse::HTTP_OK); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.01 |
proxy
|
phpinfo
|
Settings