Skip to content

Commit

Permalink
Removed configuration user_sync_data. Changed command `anzusystems:…
Browse files Browse the repository at this point in the history
…user:sync-base`, now it loads user data from file from argument.
  • Loading branch information
Tomas Hermanek committed Jul 8, 2024
1 parent 972032f commit bdb0197
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
35 changes: 29 additions & 6 deletions src/Command/SyncBaseUsersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use AnzuSystems\CommonBundle\Domain\User\UserSyncFacade;
use AnzuSystems\CommonBundle\Exception\ValidationException;
use AnzuSystems\CommonBundle\Model\User\UserDto;
use AnzuSystems\Contracts\AnzuApp;
use AnzuSystems\SerializerBundle\Serializer;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

Expand All @@ -20,24 +22,45 @@
)]
final class SyncBaseUsersCommand extends Command
{
private const string USERS_FILE_PATH_OPT = 'file';
private const string USERS_FILE_PATH_DEFAULT = 'users.json';

public function __construct(
private readonly string $usersData,
private readonly Serializer $serializer,
private readonly UserSyncFacade $userFacade,
) {
parent::__construct();
}

public function configure(): void
{
$this
->addOption(
name: self::USERS_FILE_PATH_OPT,
mode: InputOption::VALUE_REQUIRED,
default: AnzuApp::getDataDir() . '/' . self::USERS_FILE_PATH_DEFAULT
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if (empty($this->usersData)) {
$output->writeln('Users data contains empty string');
$filePath = $input->getOption(self::USERS_FILE_PATH_OPT);

if (false === file_exists($filePath)) {
$output->writeln("<error>File not found at path: ({$filePath})</error>");

return Command::FAILURE;
}

$contents = file_get_contents($filePath);
if (false === json_validate($contents)) {
$output->writeln("<error>Invalid json content at path: ({$filePath})</error>");

return Command::SUCCESS;
return Command::FAILURE;
}

/** @var UserDto[] $users */
$users = $this->serializer->deserializeIterable($this->usersData, UserDto::class, []);
$users = $this->serializer->deserializeIterable($contents, UserDto::class, []);

foreach ($users as $userDto) {
$output->writeln($userDto->getId() . ' ' . $userDto->getEmail());
Expand Down Expand Up @@ -65,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

$output->writeln(PHP_EOL . 'Done');
$output->writeln('<info>Done</info>');

return Command::SUCCESS;
}
Expand Down
1 change: 0 additions & 1 deletion src/DependencyInjection/AnzuSystemsCommonExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ private function loadSettings(ContainerBuilder $container): void
);

$container->getDefinition(SyncBaseUsersCommand::class)
->setArgument('$usersData', $settings['user_sync_data'])
->setArgument('$serializer', new Reference(Serializer::class))
->setArgument('$userFacade', new Reference(UserSyncFacade::class))
;
Expand Down
1 change: 0 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ private function addSettingsSection(): NodeDefinition
->scalarNode('app_redis')->cannotBeEmpty()->end()
->scalarNode('app_cache_proxy_enabled')->defaultTrue()->end()
->scalarNode('user_entity_class')->defaultValue('App\\Entity\\User')->end()
->scalarNode('user_sync_data')->defaultValue('[]')->end()
->scalarNode('app_entity_namespace')->defaultValue('App\\Entity')->end()
->scalarNode('app_value_object_namespace')->defaultValue('App\\Model\\ValueObject')->end()
->scalarNode('app_enum_namespace')->defaultValue('App\\Model\\Enum')->end()
Expand Down

0 comments on commit bdb0197

Please sign in to comment.