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,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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user