File manager - Edit - /var/www/html/homologBancodetalentos/app/Http/Repositories/TalentBank/BancoTalentosNoticiasRepository.php
Back
<?php namespace App\Http\Repositories\TalentBank; use App\Support\TalentBank\IntegrationLog; use Illuminate\Http\Client\ConnectionException; use Illuminate\Support\Facades\Http; class BancoTalentosNoticiasRepository { private const INTEGRATION = 'banco_talentos_noticias_api'; /** * @return array{ok: bool, status: int|null, data: array<string, mixed>, error: string|null} */ public function listarNoticiasPublicadas(int $page = 1, int $perPage = 12): array { return $this->request('', [ 'page' => max(1, $page), 'per_page' => max(1, $perPage), 'categoria' => 'BANCO_DE_TALENTOS', 'formato_bt' => 1, ], 'listar_noticias_publicadas'); } /** * @return array{ok: bool, status: int|null, data: array<string, mixed>, error: string|null} */ public function buscarNoticiaPorSlug(string $slug): array { return $this->request('/'.ltrim($slug, '/'), [ 'formato_bt' => 1, ], 'buscar_noticia_por_slug'); } /** * @return array{ok: bool, status: int|null, data: array<string, mixed>, error: string|null} */ public function listarNoticiasDestaque(int $limit = 6): array { return $this->request('', [ 'page' => 1, 'per_page' => max(1, $limit), 'destaque' => 1, 'categoria' => 'BANCO_DE_TALENTOS', 'formato_bt' => 1, ], 'listar_noticias_destaque'); } /** * @param array<string, mixed> $query * @return array{ok: bool, status: int|null, data: array<string, mixed>, error: string|null} */ private function request(string $path, array $query, string $operation): array { $baseUrl = rtrim((string) config('services.banco_talentos_noticias.base_url'), '/'); $endpoint = trim((string) config('services.banco_talentos_noticias.endpoint', 'banco-talentos/noticias'), '/'); $url = $baseUrl.'/'.$endpoint.$path; try { $response = Http::withHeaders($this->headers()) ->acceptJson() ->timeout((int) config('services.banco_talentos_noticias.timeout', 8)) ->get($url, $query); } catch (ConnectionException $e) { IntegrationLog::error(self::INTEGRATION, $operation.'_connection_error', [ 'url' => $url, 'message' => $e->getMessage(), ]); return ['ok' => false, 'status' => null, 'data' => [], 'error' => 'connection']; } catch (\Throwable $e) { IntegrationLog::error(self::INTEGRATION, $operation.'_unexpected_error', [ 'url' => $url, 'message' => $e->getMessage(), ]); return ['ok' => false, 'status' => null, 'data' => [], 'error' => 'unexpected']; } if (! $response->ok()) { IntegrationLog::warning(self::INTEGRATION, $operation.'_not_ok', [ 'url' => $url, 'status' => $response->status(), 'body' => mb_substr((string) $response->body(), 0, 1000), ]); return ['ok' => false, 'status' => $response->status(), 'data' => [], 'error' => 'offline']; } $payload = $response->json(); if (! is_array($payload)) { IntegrationLog::warning(self::INTEGRATION, $operation.'_invalid_payload', [ 'url' => $url, 'status' => $response->status(), ]); return ['ok' => false, 'status' => $response->status(), 'data' => [], 'error' => 'invalid_response']; } return ['ok' => true, 'status' => $response->status(), 'data' => $payload, 'error' => null]; } /** * @return array<string, string> */ private function headers(): array { $headers = []; $token = (string) config('services.banco_talentos_noticias.token'); $tokenHeader = (string) config('services.banco_talentos_noticias.token_header', 'Authorization'); if ($token !== '') { $headers[$tokenHeader] = strcasecmp($tokenHeader, 'Authorization') === 0 ? 'Bearer '.$token : $token; } return $headers; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings