<?php

namespace External\MSSQLBundle\Util;

use External\MSSQLBundle\Exception\MoneyException;
use External\MSSQLBundle\Synchronizers\SynchronizerInterface;
use Symfony\Component\DependencyInjection\ServiceLocator;

class SynchronizerLocator
{
    private $locator;
    private $servicesByTypes;

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

    /**
     * @return SynchronizerInterface
     *
     * @throws MoneyException
     */
    public function getServiceByType(string $type)
    {
        $id = $this->servicesByTypes[$type] ?? null;
        if (!$this->locator->get($id)) {
            throw new MoneyException(
                sprintf('Unknown synchronizer type \'%s\'!', $type)
            );
        }

        return $this->locator->get($id);
    }
}
