Skip to content

Commit

Permalink
Add converter alias resolver class
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Jun 13, 2024
1 parent d568470 commit 81bdb6b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ services:
class: 'Smile\GdprDump\Converter\ConverterFactory'
arguments:
- '@service_container'
- '@di.converter_alias_resolver'

converter.condition_builder:
class: 'Smile\GdprDump\Converter\ConditionBuilder'

database.factory:
class: 'Smile\GdprDump\Database\DatabaseFactory'

di.converter_alias_resolver:
class: 'Smile\GdprDump\DependencyInjection\ConverterAliasResolver'

dumper:
class: 'Smile\GdprDump\Dumper\MysqlDumper'
public: true # used by functional tests
Expand Down
10 changes: 6 additions & 4 deletions src/Converter/ConverterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
namespace Smile\GdprDump\Converter;

use RuntimeException;
use Smile\GdprDump\DependencyInjection\Compiler\ConverterAliasPass;
use Smile\GdprDump\DependencyInjection\ConverterAliasResolver;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

class ConverterFactory
{
public function __construct(private ContainerInterface $container)
{
public function __construct(
private ContainerInterface $container,
private ConverterAliasResolver $converterAliasResolver
) {
}

/**
Expand All @@ -22,7 +24,7 @@ public function create(string $name, array $parameters = []): ConverterInterface
{
try {
/** @var ConverterInterface $converter */
$converter = $this->container->get(ConverterAliasPass::ALIAS_PREFIX . $name);
$converter = $this->container->get($this->converterAliasResolver->getAliasByName($name));
} catch (ServiceNotFoundException) {
throw new RuntimeException(sprintf('The converter "%s" is not defined.', $name));
}
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/Compiler/ConverterAliasPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ public function process(ContainerBuilder $container): void
);
}

// Get the alias name
$aliasName = $this->getAliasName($className);
if ($container->hasDefinition($aliasName)) {
throw new RuntimeException(
sprintf('The alias "%s" conflicts with an existing service.', $aliasName)
);
}

// Create the alias
$alias = new Alias($className, true);
$container->setAlias($aliasName, $alias);
}
}

/**
* Get a converter alias name (class name with first letter in lower caps).
*
* The alias name contains a prefix to prevent any conflict with other services.
* Get alias of converter service by class name (class name with first letter in lower caps).
*/
private function getAliasName(string $className): string
{
Expand Down
18 changes: 18 additions & 0 deletions src/DependencyInjection/ConverterAliasResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Smile\GdprDump\DependencyInjection;

use Smile\GdprDump\DependencyInjection\Compiler\ConverterAliasPass;

class ConverterAliasResolver
{
/**
* Get service alias by converter name (e.g. "randomizeText").
*/
public function getAliasByName(string $name): string
{
return ConverterAliasPass::ALIAS_PREFIX . $name;
}
}
3 changes: 2 additions & 1 deletion tests/unit/Converter/ConverterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use RuntimeException;
use Smile\GdprDump\Converter\ConverterFactory;
use Smile\GdprDump\DependencyInjection\Compiler\ConverterAliasPass;
use Smile\GdprDump\DependencyInjection\ConverterAliasResolver;
use Smile\GdprDump\Tests\Framework\Mock\Converter\ConverterMock;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
Expand Down Expand Up @@ -51,6 +52,6 @@ private function createFactory(): ConverterFactory
)
);

return new ConverterFactory($containerMock);
return new ConverterFactory($containerMock, new ConverterAliasResolver());
}
}

0 comments on commit 81bdb6b

Please sign in to comment.