File manager - Edit - /var/www/html/homologBancodetalentos/app/Http/Services/TalentBank/BancoTalentosNoticiasService.php
Back
<?php namespace App\Http\Services\TalentBank; use App\Http\Repositories\TalentBank\BancoTalentosNoticiasRepository; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; class BancoTalentosNoticiasService { private const CACHE_PREFIX = 'bt:noticias:'; public function __construct( private readonly BancoTalentosNoticiasRepository $repository = new BancoTalentosNoticiasRepository() ) { } /** * @return array{ * items: array<int, array<string, mixed>>, * pagination: array<string, int>, * source: string * } */ public function listarNoticiasPublicadas(int $page = 1, int $perPage = 12): array { $cacheKey = self::CACHE_PREFIX."lista:{$page}:{$perPage}"; $lastSuccessKey = $cacheKey.':last_success'; $ttl = (int) config('services.banco_talentos_noticias.cache_ttl', 300); $cached = Cache::get($cacheKey); if (is_array($cached)) { return $cached + ['source' => 'cache']; } $result = $this->repository->listarNoticiasPublicadas($page, $perPage); if (! $result['ok']) { return $this->fallback($lastSuccessKey, $page, $perPage); } $normalized = [ 'items' => $this->normalizeCollection(data_get($result['data'], 'data', [])) ->filter(fn (array $item) => (bool) ($item['exibir_banco_talentos'] ?? false)) ->values() ->all(), 'pagination' => [ 'current_page' => (int) data_get($result['data'], 'meta.current_page', $page), 'last_page' => (int) data_get($result['data'], 'meta.last_page', 1), 'per_page' => (int) data_get($result['data'], 'meta.per_page', $perPage), 'total' => (int) data_get($result['data'], 'meta.total', 0), ], 'source' => 'api', ]; Cache::put($cacheKey, $normalized, now()->addSeconds(max(30, $ttl))); Cache::put($lastSuccessKey, $normalized, now()->addHours(2)); return $normalized; } /** * @return array<string, mixed>|null */ public function buscarNoticiaPorSlug(string $slug): ?array { $cacheKey = self::CACHE_PREFIX.'detalhe:'.$slug; $ttl = (int) config('services.banco_talentos_noticias.cache_ttl', 300); $cached = Cache::get($cacheKey); if (is_array($cached)) { return $cached; } $result = $this->repository->buscarNoticiaPorSlug($slug); if (! $result['ok']) { return null; } $data = data_get($result['data'], 'data'); if (! is_array($data)) { return null; } $normalized = $this->normalizeItem($data); Cache::put($cacheKey, $normalized, now()->addSeconds(max(30, $ttl))); return (bool) ($normalized['exibir_banco_talentos'] ?? false) ? $normalized : null; } /** * @return array<int, array<string, mixed>> */ public function listarNoticiasDestaque(int $limit = 6): array { $cacheKey = self::CACHE_PREFIX."destaques:{$limit}"; $lastSuccessKey = $cacheKey.':last_success'; $ttl = (int) config('services.banco_talentos_noticias.cache_ttl', 300); $cached = Cache::get($cacheKey); if (is_array($cached)) { return $cached; } $result = $this->repository->listarNoticiasDestaque($limit); if (! $result['ok']) { $fallback = Cache::get($lastSuccessKey); return is_array($fallback) ? $fallback : []; } $items = $this->normalizeCollection(data_get($result['data'], 'data', [])) ->filter(fn (array $item) => (bool) ($item['destaque'] ?? false)) ->filter(fn (array $item) => (bool) ($item['exibir_banco_talentos'] ?? false)) ->take(max(1, $limit)) ->values() ->all(); Cache::put($cacheKey, $items, now()->addSeconds(max(30, $ttl))); Cache::put($lastSuccessKey, $items, now()->addHours(2)); return $items; } /** * Retorna notícias para o carrossel: prioriza fixadas e completa com publicadas. * * @return array<int, array<string, mixed>> */ public function listarNoticiasCarrossel(int $limit = 8): array { $limit = max(1, $limit); $destaques = $this->listarNoticiasDestaque(min(3, $limit)); if (count($destaques) >= $limit) { return array_slice($destaques, 0, $limit); } $publicadas = $this->listarNoticiasPublicadas(1, max(12, $limit * 3)); $idsDestaque = array_map( static fn (array $item): string => (string) ($item['id'] ?? ''), $destaques ); $complementares = array_values(array_filter( $publicadas['items'] ?? [], static fn (array $item): bool => ! in_array((string) ($item['id'] ?? ''), $idsDestaque, true) )); return array_slice(array_merge($destaques, $complementares), 0, $limit); } /** * @param mixed $items * @return Collection<int, array<string, mixed>> */ private function normalizeCollection(mixed $items): Collection { if (! is_array($items)) { return collect(); } return collect($items) ->filter(fn ($item) => is_array($item)) ->map(fn (array $item) => $this->normalizeItem($item)) ->values(); } /** * @param array<string, mixed> $item * @return array<string, mixed> */ private function normalizeItem(array $item): array { $slug = (string) ($item['slug'] ?? $item['id'] ?? ''); $dataPublicacao = (string) ($item['data_publicacao_formatada'] ?? $item['data_publicacao'] ?? ''); $imagem = (string) ($item['imagem'] ?? $item['featuredPhoto'] ?? $item['foto'] ?? ''); return [ 'id' => (string) ($item['id'] ?? $slug), 'slug' => $slug, 'titulo' => (string) ($item['titulo'] ?? $item['title'] ?? 'Sem título'), 'resumo' => (string) ($item['descricao_resumo'] ?? $item['summary'] ?? ''), 'conteudo' => (string) ($item['descricao'] ?? $item['description'] ?? ''), 'imagem' => $imagem, 'data' => $dataPublicacao, 'data_publicacao' => (string) ($item['data_publicacao'] ?? ''), 'destaque' => (bool) ($item['destaque'] ?? false), 'exibir_banco_talentos' => (bool) ($item['exibir_banco_talentos'] ?? false), ]; } /** * @return array{ * items: array<int, array<string, mixed>>, * pagination: array<string, int>, * source: string * } */ private function fallback(string $lastSuccessKey, int $page, int $perPage): array { $fallback = Cache::get($lastSuccessKey); if (is_array($fallback)) { return $fallback + ['source' => 'stale_cache']; } return [ 'items' => [], 'pagination' => [ 'current_page' => $page, 'last_page' => 1, 'per_page' => $perPage, 'total' => 0, ], 'source' => 'empty', ]; } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings