File manager - Edit - /var/www/html/portal/app/Helpers/EmpresaTurismoFileHelper.php
Back
<?php namespace App\Helpers; use App\Models\Endereco; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; /** * Uploads e resolução de caminhos de arquivos do módulo Empresas de Turismo. */ class EmpresaTurismoFileHelper { private const DEFAULT_USER_PHOTO = 'pmar/assets/img/icons/usuario.png'; private const DEFAULT_DOCUMENT = 'pmar/assets/img/icons/neutro.png'; private const UPLOAD_FOLDER = 'empresas-de-turismo'; /** @var list<string> */ public const DOCUMENT_FIELDS = [ 'contratoSocial', 'cartaoCNPJ', 'registroJuntaComercial', 'alvaraDeFuncionamento', 'inscricaoEstadual', 'inscricaoMunicipal', ]; /** * Monta campos de upload e links externos dos documentos da empresa. * * @return array<string, string|null> */ public static function buildEmpresaDocumentPayload(Request $request, ?\App\Models\Empresa $currentEmpresa = null): array { $payload = []; foreach (self::DOCUMENT_FIELDS as $field) { $payload[$field] = self::uploadEmpresaDocument($request, $field, $currentEmpresa?->{$field}); $linkField = $field . 'Link'; $linkValue = $request->input($linkField); if ($linkValue === null) { $payload[$linkField] = $currentEmpresa?->{$linkField}; } else { $trimmed = trim((string) $linkValue); $payload[$linkField] = $trimmed !== '' ? $trimmed : null; } } return $payload; } /** * Regras de validação dos links externos dos documentos. * * @return array<string, string> */ public static function documentLinkValidationRules(): array { $rules = []; foreach (self::DOCUMENT_FIELDS as $field) { $rules[$field . 'Link'] = 'nullable|url|max:2048'; } return $rules; } /** * Mensagens de validação dos links externos dos documentos. * * @return array<string, string> */ public static function documentLinkValidationMessages(): array { $messages = []; foreach (self::DOCUMENT_FIELDS as $field) { $linkField = $field . 'Link'; $messages[$linkField . '.url'] = __('O link do documento deve ser uma URL válida (ex.: Google Drive).'); $messages[$linkField . '.max'] = __('O link do documento excede o tamanho máximo permitido.'); } return $messages; } /** * URL de acesso ao documento (upload local ou link externo). */ public static function documentAccessUrl(?string $filePath, ?string $externalLink): ?string { if ($externalLink !== null && trim($externalLink) !== '') { return trim($externalLink); } return self::publicUrl($filePath); } /** * Indica se há arquivo ou link externo para o documento. */ public static function hasDocumentReference(?string $filePath, ?string $externalLink): bool { return self::hasStoredDocument($filePath) || ($externalLink !== null && trim($externalLink) !== ''); } /** * Processa foto de usuário (responsável/preposto). */ public static function uploadUserPhoto(Request $request, string $inputName, ?User $user = null): string { $path = ($user && $user->photo) ? $user->photo : self::DEFAULT_USER_PHOTO; if ($request->hasFile($inputName) && $request->file($inputName)->isValid()) { $uploaded = FileUploadHelper::processSafeImageUpload($request->file($inputName), 'contas'); if ($uploaded) { return $uploaded; } } return $path; } /** * Processa comprovante de endereço (imagem ou PDF). */ public static function uploadComprovanteEndereco(Request $request, ?Endereco $endereco = null): string { if ($request->hasFile('comprovante') && $request->file('comprovante')->isValid()) { $file = $request->file('comprovante'); $uploaded = FileUploadHelper::processSafeGeneralUploadPublicDisk($file, self::UPLOAD_FOLDER . '/comprovantes'); if ($uploaded) { return $uploaded; } } return $endereco?->comprovante ?? self::DEFAULT_DOCUMENT; } /** * Indica se há documento armazenado (não placeholder e não vazio). */ public static function hasStoredDocument(?string $relativePath): bool { if ($relativePath === null || $relativePath === '') { return false; } return $relativePath !== self::DEFAULT_DOCUMENT; } /** * URL pública para download (storage link ou caminho legado em public/). */ public static function publicUrl(?string $relativePath): ?string { if (!self::hasStoredDocument($relativePath)) { return null; } $normalized = str_replace('\\', '/', ltrim($relativePath, '/')); if (str_starts_with($normalized, 'storage/')) { return asset($normalized); } if (Storage::disk('public')->exists($relativePath) || Storage::disk('public')->exists($normalized)) { return Storage::disk('public')->url($normalized); } if (file_exists(public_path($normalized))) { return asset($normalized); } return asset($normalized); } /** * Processa documento da empresa (PDF ou imagem). */ public static function uploadEmpresaDocument(Request $request, string $inputName, ?string $currentPath = null): ?string { if ($request->hasFile($inputName) && $request->file($inputName)->isValid()) { $file = $request->file($inputName); $uploaded = FileUploadHelper::processSafeGeneralUploadPublicDisk($file, self::UPLOAD_FOLDER . '/documentos'); if ($uploaded) { return $uploaded; } } return $currentPath; } /** * Caminho absoluto de imagem para DomPDF. */ public static function resolveImagePathForPdf(?string $relativePath): string { $default = public_path(self::DEFAULT_USER_PHOTO); if (empty($relativePath) || filter_var($relativePath, FILTER_VALIDATE_URL)) { return $default; } $normalized = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, ltrim($relativePath, '/\\')); $publicPath = public_path($normalized); if (file_exists($publicPath)) { return $publicPath; } if (Storage::disk('public')->exists($relativePath)) { return Storage::disk('public')->path($relativePath); } return $default; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.1 |
proxy
|
phpinfo
|
Settings