aggregate() now fires every configured provider's startFetch() in one pass before resolving any of them, so wall-clock cost is roughly max(providers) instead of sum(providers). Partial-failure isolation and per-date summation behavior are unchanged.
52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Service\Provider\ProviderInterface;
|
|
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
|
|
{
|
|
$pending = [];
|
|
|
|
/** @var ProviderInterface $provider */
|
|
foreach ($this->providers as $provider) {
|
|
if (!$provider->isConfigured()) {
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
$pending[] = [$provider, $provider->startFetch()];
|
|
} catch (\Throwable $e) {
|
|
$this->logger->warning(sprintf('%s fetch failed: %s', $provider::class, $e->getMessage()), ['exception' => $e]);
|
|
}
|
|
}
|
|
|
|
$contributions = [];
|
|
|
|
foreach ($pending as [$provider, $handle]) {
|
|
try {
|
|
foreach ($provider->resolveFetch($handle) 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;
|
|
}
|
|
}
|