refactor(controller): extract contribution aggregation into dedicated service

Move fetchAllContributions and merge logic from GraphController into a new
ContributionAggregator service. Replace deprecated TaggedIterator with
AutowireIterator throughout.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-30 12:58:17 +02:00
parent 3e3a6752af
commit c205fed14b
2 changed files with 43 additions and 34 deletions
+3 -34
View File
@@ -2,11 +2,10 @@
namespace App\Controller; namespace App\Controller;
use App\Service\ProviderInterface; use App\Service\ContributionAggregator;
use App\Service\SvgRenderer; use App\Service\SvgRenderer;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@@ -20,8 +19,7 @@ class GraphController
private readonly array $allowedHosts; private readonly array $allowedHosts;
public function __construct( public function __construct(
#[TaggedIterator('app.provider')] private readonly ContributionAggregator $aggregator,
private readonly iterable $providers,
private readonly SvgRenderer $renderer, private readonly SvgRenderer $renderer,
private readonly CacheInterface $cache, private readonly CacheInterface $cache,
private readonly LoggerInterface $logger, private readonly LoggerInterface $logger,
@@ -52,7 +50,7 @@ class GraphController
$cacheMiss = true; $cacheMiss = true;
$item->expiresAfter(3600); $item->expiresAfter(3600);
return $this->renderer->render($this->fetchAllContributions(), $theme); return $this->renderer->render($this->aggregator->aggregate(), $theme);
}); });
$this->logger->debug('GraphController: cache ' . ($cacheMiss ? 'miss' : 'hit'), ['theme' => $theme]); $this->logger->debug('GraphController: cache ' . ($cacheMiss ? 'miss' : 'hit'), ['theme' => $theme]);
@@ -78,34 +76,5 @@ class GraphController
return new Response('{"status":"ok"}', 200, ['Content-Type' => 'application/json']); return new Response('{"status":"ok"}', 200, ['Content-Type' => 'application/json']);
} }
/** @return array<string, int> */
private function fetchAllContributions(): array
{
$contributions = [];
/** @var ProviderInterface $provider */
foreach ($this->providers as $provider) {
if (!$provider->isConfigured()) {
continue;
}
try {
$contributions = $this->merge($contributions, $provider->fetch());
} catch (\Throwable $e) {
$this->logger->warning(sprintf('%s fetch failed: %s', $provider::class, $e->getMessage()), ['exception' => $e]);
}
}
return $contributions;
}
/** @param array<string, int> $base @param array<string, int> $new @return array<string, int> */
private function merge(array $base, array $new): array
{
foreach ($new as $date => $count) {
$base[$date] = ($base[$date] ?? 0) + $count;
}
return $base;
}
} }
+40
View File
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Service;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
final class ContributionAggregator
{
public function __construct(
#[AutowireIterator('app.provider')]
private readonly iterable $providers,
private readonly LoggerInterface $logger,
) {}
/** @return array<string, int> */
public function aggregate(): array
{
$contributions = [];
/** @var ProviderInterface $provider */
foreach ($this->providers as $provider) {
if (!$provider->isConfigured()) {
continue;
}
try {
foreach ($provider->fetch() as $date => $count) {
$contributions[$date] = ($contributions[$date] ?? 0) + $count;
}
} catch (\Throwable $e) {
$this->logger->warning(sprintf('%s fetch failed: %s', $provider::class, $e->getMessage()), ['exception' => $e]);
}
}
return $contributions;
}
}