diff --git a/src/Controller/GraphController.php b/src/Controller/GraphController.php index 455b980..6fbec66 100644 --- a/src/Controller/GraphController.php +++ b/src/Controller/GraphController.php @@ -2,11 +2,10 @@ namespace App\Controller; -use App\Service\ProviderInterface; +use App\Service\ContributionAggregator; use App\Service\SvgRenderer; use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; -use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -20,8 +19,7 @@ class GraphController private readonly array $allowedHosts; public function __construct( - #[TaggedIterator('app.provider')] - private readonly iterable $providers, + private readonly ContributionAggregator $aggregator, private readonly SvgRenderer $renderer, private readonly CacheInterface $cache, private readonly LoggerInterface $logger, @@ -52,7 +50,7 @@ class GraphController $cacheMiss = true; $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]); @@ -78,34 +76,5 @@ class GraphController return new Response('{"status":"ok"}', 200, ['Content-Type' => 'application/json']); } - /** @return array */ - 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 $base @param array $new @return array */ - private function merge(array $base, array $new): array - { - foreach ($new as $date => $count) { - $base[$date] = ($base[$date] ?? 0) + $count; - } - - return $base; - } } diff --git a/src/Service/ContributionAggregator.php b/src/Service/ContributionAggregator.php new file mode 100644 index 0000000..370d05f --- /dev/null +++ b/src/Service/ContributionAggregator.php @@ -0,0 +1,40 @@ + */ + 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; + } +}