Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10 from smartbooster/fix_dry_run_and_admin_export
Browse files Browse the repository at this point in the history
Set dryRun on transaction declaration + Add Export to admin
  • Loading branch information
mathieu-ducrot authored Jul 6, 2021
2 parents 5e3a984 + 0b01be9 commit 6486349
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
CHANGELOG for 1.x
===================

## v1.x - (2021-XX-XX)
## v1.0.0 - (2021-07-06)

### Added

* [[#1](https://github.com/smartbooster/parameter-bundle/issues/1)] Init bundle
* [[#2](https://github.com/smartbooster/parameter-bundle/issues/2)] Add bundle Configuration
* [[#2](https://github.com/smartbooster/parameter-bundle/issues/2)] Add Parameter Entity
* [[#2](https://github.com/smartbooster/parameter-bundle/issues/2)] Add ParameterProvider
Expand Down
14 changes: 14 additions & 0 deletions src/Admin/ParameterAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,18 @@ protected function configureFormFields(FormMapper $form): void
->end()
;
}

public function getExportFormats(): array
{
return ['csv'];
}

public function getExportFields(): array
{
return [
$this->trans('field.label_code') => 'code',
$this->trans('field.label_value') => 'value',
$this->trans('field.label_help') => 'help',
];
}
}
20 changes: 11 additions & 9 deletions src/Loader/ParameterLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ParameterLoader
"nb_deleted" => 0,
"nb_inserted" => 0,
];
private bool $dryRun;
private bool $dryRun = false;

public function __construct(EntityManagerInterface $entityManager)
{
Expand All @@ -37,12 +37,14 @@ public function addParameter(string $code, array $data): void
*/
public function load(bool $dryRun = false): array
{
$this->entityManager->getConnection()->beginTransaction();
$this->dryRun = $dryRun;
if (false === $this->dryRun) {
$this->entityManager->getConnection()->beginTransaction();
}

try {
// @phpstan-ignore-next-line
$existingParameters = $this->entityManager->getRepository(Parameter::class)->findExisting();
$existingParameters = $this->entityManager->getRepository(Parameter::class)->findAllByCode();
$i = 1;
foreach ($existingParameters as $code => $parameter) {
$this->handleExistingParameters($code, $parameter, $i);
Expand All @@ -55,16 +57,16 @@ public function load(bool $dryRun = false): array
++$i;
}

if (!$this->dryRun) {
if (false === $this->dryRun) {
$this->entityManager->flush();
$this->entityManager->clear();
$this->entityManager->getConnection()->commit();
}
// @codeCoverageIgnoreStart
} catch (\Exception $e) {
$this->entityManager->getConnection()->rollBack();
if (false === $this->dryRun) {
$this->entityManager->getConnection()->rollBack();
}
}
// @codeCoverageIgnoreEnd

return $this->logs;
}
Expand All @@ -85,7 +87,7 @@ protected function handleExistingParameters(string $code, Parameter $parameter,
$this->logs["nb_deleted"]++;
}

if (($i % self::BATCH_SIZE) === 0 && !$this->dryRun) {
if (($i % self::BATCH_SIZE) === 0 && false === $this->dryRun) {
$this->entityManager->flush();
$this->entityManager->clear();
}
Expand All @@ -103,7 +105,7 @@ protected function handleParametersToInsert(string $code, array $data, int $i):
$this->entityManager->persist($parameter);
$this->logs["nb_inserted"]++;

if (($i % self::BATCH_SIZE) === 0 && !$this->dryRun) {
if (($i % self::BATCH_SIZE) === 0 && false === $this->dryRun) {
$this->entityManager->flush();
$this->entityManager->clear();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/ParameterRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ParameterRepository extends EntityRepository
/**
* @return Parameter[]
*/
public function findExisting(): array
public function findAllByCode(): array
{
return $this->createQueryBuilder('p', 'p.code')
->getQuery()
Expand Down

0 comments on commit 6486349

Please # to comment.