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