<?php

declare(strict_types=1);

namespace KupShop\MetricsBundle;

use KupShop\KupShopBundle\Metrics\AbstractStatusProvider;
use Prometheus\MetricFamilySamples;
use Symfony\Component\DependencyInjection\ServiceLocator;

class StatusProviderLocator
{
    private $locator;

    public function __construct(ServiceLocator $locator)
    {
        $this->locator = $locator;
    }

    public function getProviders(): iterable
    {
        foreach ($this->locator->getProvidedServices() as $service) {
            yield $this->locator->get($service);
        }
    }

    public function getStatuses(): array
    {
        $statuses = [
            'name' => 'kupshop_status',
            'type' => 'gauge',
            'help' => 'Basic functionality tests',
            'labelNames' => [],
            'samples' => [],
        ];

        /** @var AbstractStatusProvider $provider */
        foreach ($this->getProviders() as $provider) {
            $statuses['samples'] = array_merge($statuses['samples'], $provider->getSample());
        }

        return [new MetricFamilySamples($statuses)];
    }
}
