Files
kupshop/bundles/KupShop/FeedsBundle/FeedLocator.php
2025-08-02 16:30:27 +02:00

98 lines
2.6 KiB
PHP

<?php
namespace KupShop\FeedsBundle;
use KupShop\FeedsBundle\Exceptions\UnknownFeedTypeException;
use KupShop\FeedsBundle\Feed\IFeed;
use Symfony\Component\DependencyInjection\ServiceLocator;
class FeedLocator
{
/** @var ServiceLocator */
private $container;
/** @var array */
private $aliases = [];
/** @var array */
private $serviceIDs = [];
/** @var array */
private $configurableIDs = [];
public function __construct(ServiceLocator $container)
{
$this->container = $container;
}
public function addFeed($serviceID, string $type, string $name, bool $configurable, bool $isAllowed)
{
if (!$isAllowed || ($configurable && !findModule(\Modules::FEED_GENERATOR))) {
return;
}
$this->aliases[$type] = $name;
$this->serviceIDs[$type] = $serviceID;
if ($configurable) {
$this->configurableIDs[] = $type;
}
}
public function sortAliases()
{
$primary = [];
$configurable = [];
$other = [];
foreach ($this->aliases as $key => $alias) {
if ($key === 'configurable') {
$primary[$key] = $alias;
} elseif (($key === 'external') || ($key === 'deprecated')) {
$other[$key] = $alias;
} elseif ($this->isTypeConfigurable($key)) {
$configurable[$key] = $alias;
} else {
$other[$key] = '[Zastaralý] '.$alias;
}
}
array_multisort($configurable, SORT_STRING);
array_multisort($other, SORT_STRING);
$this->aliases = array_merge($primary, $configurable, $other);
}
public function getAliases(): array
{
return $this->aliases;
}
/**
* @throws UnknownFeedTypeException
*/
public function getServiceByType(string $type): IFeed
{
if (!isset($this->serviceIDs[$type])) {
// throw new UnknownFeedTypeException('Unknown feed type: '.$type);
$type = 'deprecated';
}
/** @var $feedService IFeed */
$feedService = $this->container->get($this->serviceIDs[$type]);
return $feedService;
}
/**
* @throws UnknownFeedTypeException
*/
public function getAliasByType(string $type): string
{
if (!isset($this->aliases[$type])) {
throw new UnknownFeedTypeException($type);
}
return $this->aliases[$type];
}
public function isTypeConfigurable(string $type): bool
{
return in_array($type, $this->configurableIDs);
}
}