Skip to content

Instantly share code, notes, and snippets.

@hlspablo
Last active March 2, 2026 18:24
Show Gist options
  • Select an option

  • Save hlspablo/5231323241a1df25f94e7fc1cb314e30 to your computer and use it in GitHub Desktop.

Select an option

Save hlspablo/5231323241a1df25f94e7fc1cb314e30 to your computer and use it in GitHub Desktop.
<?php
namespace App\Services;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class LotecaClient
{
protected string $baseUrl;
protected string $login;
protected string $password;
protected string $cacheKey = 'loteca.jwt';
public function __construct()
{
$this->baseUrl = rtrim(config('services.loteca.base_url', 'https://loteca.app.br/webservice'), '/');
$this->login = (string) config('services.loteca.login', env('LOTECA_LOGIN'));
$this->password = (string) config('services.loteca.password', env('LOTECA_PASSWORD'));
}
public function ensureToken(): ?string
{
$token = Cache::get($this->cacheKey);
if ($token && $this->checkToken($token)) {
return $token;
}
$token = $this->login();
if ($token) {
// cache for 55 minutes by default
Cache::put($this->cacheKey, $token, now()->addMinutes(55));
}
return $token;
}
public function clearToken(): void
{
Cache::forget($this->cacheKey);
}
public function checkToken(string $token): bool
{
try {
$response = Http::withHeaders([
'x-jwt' => $token,
])->get($this->baseUrl . '/user/get');
if (!$response->ok()) {
return false;
}
$json = $response->json();
return ($json['status'] ?? null) === 'success';
} catch (\Throwable $e) {
Log::warning('Loteca token check failed', ['error' => $e->getMessage()]);
return false;
}
}
public function login(): ?string
{
try {
$response = Http::asForm()->post($this->baseUrl . '/login/post', [
'login' => $this->login,
'password' => $this->password,
]);
if (!$response->ok()) {
Log::warning('Loteca login HTTP failed', ['status' => $response->status(), 'body' => $response->body()]);
return null;
}
$json = $response->json();
if (($json['status'] ?? null) !== 'success') {
Log::warning('Loteca login business failed', ['json' => $json]);
return null;
}
return $json['data']['token'] ?? null;
} catch (\Throwable $e) {
Log::error('Loteca login exception', ['error' => $e->getMessage()]);
return null;
}
}
/**
* @return array<int, array<string, mixed>>|null
*/
public function getResults(string $token, int $from = 0, int $limit = 10): ?array
{
try {
$response = Http::withHeaders([
'x-jwt' => $token,
])->get($this->baseUrl . '/results/get', [
'from' => $from,
'limit' => $limit,
]);
if (!$response->ok()) {
Log::warning('Loteca getResults HTTP failed', ['status' => $response->status(), 'body' => $response->body()]);
return null;
}
$json = $response->json();
if (($json['status'] ?? null) !== 'success') {
Log::warning('Loteca getResults business failed', ['json' => $json]);
return null;
}
return $json['data']['results'] ?? [];
} catch (\Throwable $e) {
Log::error('Loteca getResults exception', ['error' => $e->getMessage()]);
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment