<?php

namespace KupShop\SystemInspectionBundle\Util;

use KupShop\SystemInspectionBundle\Inspections\Compile\CompileInspectionInterface;
use KupShop\SystemInspectionBundle\Inspections\InspectionInterface;
use KupShop\SystemInspectionBundle\Inspections\User\UserInspectionInterface;
use Symfony\Component\DependencyInjection\ServiceLocator;

class InspectionLocator
{
    private $compileLocator;
    private $userLocator;

    public function __construct(ServiceLocator $compileLocator, ServiceLocator $userLocator)
    {
        $this->compileLocator = $compileLocator;
        $this->userLocator = $userLocator;
    }

    /**
     * @param $type string
     *
     * @return InspectionInterface[]
     */
    public function getInspections($type)
    {
        $locator = match ($type) {
            CompileInspectionInterface::INSPECTION_KEYWORD => $this->compileLocator,
            UserInspectionInterface::INSPECTION_KEYWORD => $this->userLocator,
            default => throw new \Exception("Unknown inspection type {$type}"),
        };

        $inspections = [];
        foreach ($locator->getProvidedServices() as $className) {
            if ($locator->has($className)) {
                $inspections[] = $locator->get($className);
            }
        }

        return $inspections;
    }
}
