refactor: reorganise Service/ into Provider/ and Renderer/ sub-namespaces
Move all provider-related classes, enums, interface and trait into App\Service\Provider; move SvgRenderer into App\Service\Renderer. ContributionAggregator stays at the Service root as the orchestrator. Test namespaces and use statements updated to match. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Provider;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
/**
|
||||
* Fetches the last 365 days of push/merge events from the GitLab REST API.
|
||||
*
|
||||
* Required token scopes: read_user, read_api
|
||||
* Works with both gitlab.com and self-hosted instances.
|
||||
*/
|
||||
final class GitLabProvider implements ProviderInterface
|
||||
{
|
||||
use ProbeTrait;
|
||||
|
||||
public function __construct(
|
||||
private readonly HttpClientInterface $client,
|
||||
private readonly string $username,
|
||||
private readonly string $token,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly string $baseUrl = '',
|
||||
) {}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'gitlab';
|
||||
}
|
||||
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
return $this->username !== '' && $this->token !== '';
|
||||
}
|
||||
|
||||
public function ping(): void
|
||||
{
|
||||
$baseUrl = rtrim($this->baseUrl !== '' ? $this->baseUrl : 'https://gitlab.com', '/');
|
||||
|
||||
$this->client->request('GET', "$baseUrl/api/v4/user", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
])->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, int> date (Y-m-d) => event count
|
||||
*/
|
||||
public function fetch(): array
|
||||
{
|
||||
$baseUrl = rtrim($this->baseUrl !== '' ? $this->baseUrl : 'https://gitlab.com', '/');
|
||||
|
||||
$this->logger->debug('GitLabProvider: fetching contributions', ['user' => $this->username, 'url' => $baseUrl]);
|
||||
|
||||
$userResponse = $this->client->request('GET', "$baseUrl/api/v4/users", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
'query' => ['username' => $this->username],
|
||||
]);
|
||||
|
||||
$users = $userResponse->toArray();
|
||||
if (empty($users)) {
|
||||
throw new NotFoundHttpException("GitLab: user '{$this->username}' not found on $baseUrl");
|
||||
}
|
||||
$userId = $users[0]['id'];
|
||||
|
||||
$result = [];
|
||||
$after = (new \DateTimeImmutable('-365 days'))->format('Y-m-d');
|
||||
$page = 1;
|
||||
|
||||
do {
|
||||
$response = $this->client->request('GET', "$baseUrl/api/v4/users/$userId/events", [
|
||||
'headers' => ['PRIVATE-TOKEN' => $this->token],
|
||||
'query' => [
|
||||
'after' => $after,
|
||||
'per_page' => 100,
|
||||
'page' => $page,
|
||||
],
|
||||
]);
|
||||
|
||||
$events = $response->toArray();
|
||||
|
||||
foreach ($events as $event) {
|
||||
$date = substr($event['created_at'], 0, 10);
|
||||
$result[$date] = ($result[$date] ?? 0) + 1;
|
||||
}
|
||||
|
||||
$page++;
|
||||
} while (count($events) === 100);
|
||||
|
||||
$this->logger->info('GitLabProvider: fetched contributions', [
|
||||
'user' => $this->username,
|
||||
'pages' => $page - 1,
|
||||
'days' => count($result),
|
||||
'total' => array_sum($result),
|
||||
]);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user