This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2 Add ParameterLoader service + ParameterLoadCommand
- Loading branch information
1 parent
1db2615
commit db2577f
Showing
19 changed files
with
497 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/vendor/ | ||
composer.lock | ||
/phpmetrics/ | ||
/phpunit-coverage-html/ | ||
coverage.* | ||
.php_cs.cache | ||
.phpunit.result.cache | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Smart\ParameterBundle\Command; | ||
|
||
use Smart\ParameterBundle\Loader\ParameterLoader; | ||
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 Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* @author Mathieu Ducrot <[email protected]> | ||
*/ | ||
class ParameterLoadCommand extends Command | ||
{ | ||
private ParameterLoader $parameterLoader; | ||
|
||
public function __construct(ParameterLoader $parameterLoader) | ||
{ | ||
$this->parameterLoader = $parameterLoader; | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setName('smart:parameter:load') | ||
->setDescription("Execute a loading of every parameters in the database.") | ||
->setHelp(<<<EOT | ||
The <info>%command.name%</info> command executes a loading of every parameters in the database: | ||
<info>%command.full_name%</info> | ||
Detail behavior on load: | ||
- If a parameter isn't found in the database by his code, it will be inserted. | ||
- If a parameter is found in the database by his code, his help attribut will be updated but the value will remain the same. | ||
- If an existing parameter is not found in the configuration it will be deleted from the database. | ||
EOT | ||
) | ||
->addOption('dry-run', null, InputOption::VALUE_NONE, "Simulates the result of the command without making any changes to the database") | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$io->title('SmartParameterBundle'); | ||
$io->text('Parameters are being loaded.'); | ||
|
||
$dryRun = $input->getOption('dry-run'); | ||
$logs = $this->parameterLoader->load($dryRun); | ||
|
||
$io->text('Execution results:'); | ||
$io->listing([ | ||
"$logs[nb_inserted] parameters inserted.", | ||
"$logs[nb_updated] parameters updated.", | ||
"$logs[nb_skipped] parameters skipped.", | ||
"$logs[nb_deleted] parameters deleted.", | ||
]); | ||
|
||
if ($dryRun) { | ||
$io->text("<info>The command has dry-run option, so no change was done in the database.</info>"); | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace Smart\ParameterBundle\Loader; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Smart\ParameterBundle\Entity\Parameter; | ||
|
||
/** | ||
* @author Mathieu Ducrot <[email protected]> | ||
*/ | ||
class ParameterLoader | ||
{ | ||
const BATCH_SIZE = 20; | ||
|
||
private EntityManagerInterface $entityManager; | ||
private array $parameters; | ||
private array $logs = [ | ||
"nb_updated" => 0, | ||
"nb_skipped" => 0, | ||
"nb_deleted" => 0, | ||
"nb_inserted" => 0, | ||
]; | ||
private bool $dryRun; | ||
|
||
public function __construct(EntityManagerInterface $entityManager) | ||
{ | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
public function addParameter(string $code, array $data): void | ||
{ | ||
$this->parameters[$code] = $data; | ||
} | ||
|
||
/** | ||
* if dryRun mode is set to true the changes are not flush in the database | ||
*/ | ||
public function load(bool $dryRun = false): array | ||
{ | ||
$this->entityManager->getConnection()->beginTransaction(); | ||
$this->dryRun = $dryRun; | ||
|
||
try { | ||
// @phpstan-ignore-next-line | ||
$existingParameters = $this->entityManager->getRepository(Parameter::class)->findExisting(); | ||
$i = 1; | ||
foreach ($existingParameters as $code => $parameter) { | ||
$this->handleExistingParameters($code, $parameter, $i); | ||
++$i; | ||
} | ||
|
||
$parameterToInsert = array_diff_key($this->parameters, $existingParameters); | ||
foreach ($parameterToInsert as $code => $data) { | ||
$this->handleParametersToInsert($code, $data, $i); | ||
++$i; | ||
} | ||
|
||
if (!$this->dryRun) { | ||
$this->entityManager->flush(); | ||
$this->entityManager->clear(); | ||
$this->entityManager->getConnection()->commit(); | ||
} | ||
// @codeCoverageIgnoreStart | ||
} catch (\Exception $e) { | ||
$this->entityManager->getConnection()->rollBack(); | ||
} | ||
// @codeCoverageIgnoreEnd | ||
|
||
return $this->logs; | ||
} | ||
|
||
protected function handleExistingParameters(string $code, Parameter $parameter, int $i): void | ||
{ | ||
if (isset($this->parameters[$code])) { | ||
// update help for existing parameters | ||
if (isset($this->parameters[$code]['help']) and $parameter->getHelp() !== $this->parameters[$code]['help']) { | ||
$parameter->setHelp($this->parameters[$code]['help']); | ||
$this->entityManager->persist($parameter); | ||
$this->logs["nb_updated"]++; | ||
} else { | ||
$this->logs["nb_skipped"]++; | ||
} | ||
} else { | ||
$this->entityManager->remove($parameter); | ||
$this->logs["nb_deleted"]++; | ||
} | ||
|
||
if (($i % self::BATCH_SIZE) === 0 and !$this->dryRun) { | ||
$this->entityManager->flush(); | ||
$this->entityManager->clear(); | ||
} | ||
} | ||
|
||
protected function handleParametersToInsert(string $code, array $data, int $i): void | ||
{ | ||
$parameter = new Parameter(); | ||
$parameter->setCode($code); | ||
$parameter->setValue($data['value']); | ||
if (isset($data['help'])) { | ||
$parameter->setHelp($data['help']); | ||
} | ||
|
||
$this->entityManager->persist($parameter); | ||
$this->logs["nb_inserted"]++; | ||
|
||
if (($i % self::BATCH_SIZE) === 0 and !$this->dryRun) { | ||
$this->entityManager->flush(); | ||
$this->entityManager->clear(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.