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:
2026-05-31 00:08:21 +02:00
parent fad176419c
commit 2f3268c0b7
20 changed files with 37 additions and 36 deletions
+111
View File
@@ -0,0 +1,111 @@
<?php
declare(strict_types=1);
namespace App\Service\Provider;
use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClient;
use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClientRegistryInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Fetches the last 365 days of contributions from the GitHub GraphQL API.
*
* Required token scopes: read:user
*/
final class GitHubProvider implements ProviderInterface
{
use ProbeTrait;
private const GRAPHQL_URL = 'https://api.github.com/graphql';
public function __construct(
private readonly HttpClientInterface $client,
private readonly GraphQLApiClientRegistryInterface $registry,
private readonly string $username,
private readonly string $token,
private readonly LoggerInterface $logger,
) {}
public function getName(): string
{
return 'github';
}
public function isConfigured(): bool
{
return $this->username !== '' && $this->token !== '';
}
public function ping(): void
{
$this->client->request('GET', 'https://api.github.com/user', [
'headers' => ['Authorization' => "Bearer {$this->token}"],
])->getContent();
}
/**
* @return array<string, int> date (Y-m-d) => contribution count
*/
public function fetch(): array
{
$this->logger->debug('GitHubProvider: fetching contributions', ['user' => $this->username]);
$from = (new \DateTimeImmutable('-365 days'))->format('Y-m-d\T00:00:00\Z');
$to = (new \DateTimeImmutable())->format('Y-m-d\T23:59:59\Z');
/** @var GraphQLApiClient $graphqlClient */
$graphqlClient = $this->registry->get('github');
$query = $graphqlClient->buildQuery(
['user' => ['login' => $this->username]],
[
'contributionsCollection' => [
'_parameters' => ['from' => $from, 'to' => $to],
'contributionCalendar' => [
'weeks' => [
'contributionDays' => ['date', 'contributionCount'],
],
],
],
]
)->getGraphQLQuery();
// GitHub's GraphQL API requires application/json — the bundle's built-in
// transport sends form_params, so we use Symfony HttpClient here instead.
$response = $this->client->request('POST', self::GRAPHQL_URL, [
'headers' => [
'Authorization' => "Bearer {$this->token}",
'Content-Type' => 'application/json',
],
'json' => ['query' => $query],
]);
$data = $response->toArray();
if (isset($data['errors'])) {
throw new ServiceUnavailableHttpException(null, 'GitHub GraphQL error: ' . json_encode($data['errors']));
}
$result = [];
$weeks = $data['data']['user']['contributionsCollection']['contributionCalendar']['weeks'] ?? [];
foreach ($weeks as $week) {
foreach ($week['contributionDays'] as $day) {
if ($day['contributionCount'] > 0) {
$result[$day['date']] = $day['contributionCount'];
}
}
}
$this->logger->info('GitHubProvider: fetched contributions', [
'user' => $this->username,
'days' => count($result),
'total' => array_sum($result),
]);
return $result;
}
}
+101
View File
@@ -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;
}
}
+82
View File
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace App\Service\Provider;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Fetches contribution data from the Gitea heatmap endpoint.
*
* Endpoint: GET /api/v1/users/{username}/heatmap
* Returns: [{"timestamp": 1234567890, "contributions": 3}, ...]
*
* Required token scopes: read:user
*/
final class GiteaProvider implements ProviderInterface
{
use ProbeTrait;
public function __construct(
private readonly HttpClientInterface $client,
private readonly string $username,
private readonly string $token,
private readonly string $baseUrl,
private readonly LoggerInterface $logger,
) {}
public function getName(): string
{
return 'gitea';
}
public function isConfigured(): bool
{
return $this->username !== '' && $this->token !== '' && $this->baseUrl !== '';
}
public function ping(): void
{
$baseUrl = rtrim($this->baseUrl, '/');
$this->client->request('GET', "$baseUrl/api/v1/user", [
'headers' => ['Authorization' => "token {$this->token}"],
])->getContent();
}
/**
* @return array<string, int> date (Y-m-d) => contribution count
*/
public function fetch(): array
{
$baseUrl = rtrim($this->baseUrl, '/');
$this->logger->debug('GiteaProvider: fetching contributions', ['user' => $this->username, 'url' => $baseUrl]);
$response = $this->client->request('GET', "$baseUrl/api/v1/users/{$this->username}/heatmap", [
'headers' => ['Authorization' => "token {$this->token}"],
]);
$data = $response->toArray();
$cutoff = (new \DateTimeImmutable('-365 days'))->getTimestamp();
$result = [];
foreach ($data as $entry) {
if ($entry['timestamp'] < $cutoff) {
continue;
}
$date = date('Y-m-d', $entry['timestamp']);
$result[$date] = ($result[$date] ?? 0) + (int) $entry['contributions'];
}
$this->logger->info('GiteaProvider: fetched contributions', [
'user' => $this->username,
'days' => count($result),
'total' => array_sum($result),
]);
return $result;
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace App\Service\Provider;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
trait ProbeTrait
{
public function probe(): ProviderStatus
{
if (!$this->isConfigured()) {
return new ProviderStatus($this->getName(), ProviderStatusType::NotConfigured);
}
try {
$this->ping();
return new ProviderStatus($this->getName(), ProviderStatusType::Ok);
} catch (\Throwable $e) {
return $this->statusFromException($e);
}
}
private function statusFromException(\Throwable $e): ProviderStatus
{
[$error, $message] = $this->classifyException($e);
return new ProviderStatus($this->getName(), ProviderStatusType::Error, $error, $message);
}
/** @return array{ProviderErrorCode, string} */
private function classifyException(\Throwable $e): array
{
if ($e instanceof TransportExceptionInterface) {
return [ProviderErrorCode::UrlUnreachable, 'Could not reach the server: ' . $e->getMessage()];
}
if ($e instanceof HttpExceptionInterface) {
$code = $e->getResponse()->getStatusCode();
return match (true) {
$code === 401 => [ProviderErrorCode::AuthFailed, 'Invalid or expired token — verify your credentials'],
$code === 403 => [ProviderErrorCode::AuthFailed, 'Access denied — token lacks the required scopes'],
$code === 404 => [ProviderErrorCode::UrlUnreachable, 'Endpoint not found — check the configured URL'],
default => [ProviderErrorCode::Unknown, "HTTP {$code}: " . $e->getMessage()],
};
}
$msg = $e->getMessage();
$lower = strtolower($msg);
$error = match (true) {
str_contains($msg, 'not found') || str_contains($msg, 'Could not resolve') => ProviderErrorCode::UserNotFound,
str_contains($msg, 'GraphQL error')
&& (str_contains($lower, 'unauthorized') || str_contains($lower, 'bad credentials')) => ProviderErrorCode::AuthFailed,
default => ProviderErrorCode::Unknown,
};
return [$error, $msg];
}
}
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace App\Service\Provider;
enum ProviderErrorCode: string
{
case AuthFailed = 'auth_failed';
case UrlUnreachable = 'url_unreachable';
case UserNotFound = 'user_not_found';
case Unknown = 'unknown';
}
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace App\Service\Provider;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
final class ProviderHealthChecker
{
public function __construct(
#[AutowireIterator('app.provider')]
private readonly iterable $providers,
) {}
/**
* @return array{status: string, providers: array<string, array<string, string>>}
*/
public function check(): array
{
$statuses = [];
$hasError = false;
/** @var ProviderInterface $provider */
foreach ($this->providers as $provider) {
$status = $provider->probe();
$statuses[$status->name] = $status->toArray();
if ($status->status === ProviderStatusType::Error) {
$hasError = true;
}
}
return [
'status' => $hasError ? 'degraded' : 'ok',
'providers' => $statuses,
];
}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace App\Service\Provider;
interface ProviderInterface
{
/** @return array<string, int> date (Y-m-d) => contribution count */
public function fetch(): array;
public function isConfigured(): bool;
public function getName(): string;
public function probe(): ProviderStatus;
public function ping(): void;
}
+29
View File
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
namespace App\Service\Provider;
final class ProviderStatus
{
public function __construct(
public readonly string $name,
public readonly ProviderStatusType $status,
public readonly ?ProviderErrorCode $error = null,
public readonly ?string $message = null,
) {}
/** @return array<string, string> */
public function toArray(): array
{
$data = ['status' => $this->status->value];
if ($this->error !== null) {
$data['error'] = $this->error->value;
}
if ($this->message !== null) {
$data['message'] = $this->message;
}
return $data;
}
}
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace App\Service\Provider;
enum ProviderStatusType: string
{
case Ok = 'ok';
case Error = 'error';
case NotConfigured = 'not_configured';
}