Skip to content

Commit

Permalink
Corrections of parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Dec 20, 2024
1 parent e481818 commit b307b5c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 48 deletions.
1 change: 1 addition & 0 deletions src/Form/Parameters/UserParametersType.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of the Calculation package.
*
Expand Down
47 changes: 26 additions & 21 deletions src/Parameter/AbstractParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,6 @@ protected function createParameter(string $class, ?ParameterInterface $default =
}
}

if ($parameter instanceof EntityParameterInterface) {
$parameter->updateEntities($this->manager);
}

/** @phpstan-var T */
return $parameter;
}
Expand Down Expand Up @@ -199,17 +195,17 @@ protected function getBackedEnumString(string $type, AbstractProperty $property)
*/
protected function getCachedParameter(string $class, ?ParameterInterface $default = null): ParameterInterface
{
$parameters = $this->cache->get(
$parameter = $this->cache->get(
$class::getCacheKey(),
fn (): ParameterInterface => $this->createParameter($class, $default)
);

if ($parameters instanceof EntityParameterInterface) {
$parameters->updateEntities($this->manager);
if ($parameter instanceof EntityParameterInterface) {
$this->updateEntities($parameter);
}

/** @phpstan-var T */
return $parameters;
return $parameter;
}

/**
Expand Down Expand Up @@ -369,19 +365,6 @@ protected function isDefaultValue(ParameterInterface $parameter, string $name, m
*/
abstract protected function loadProperties(): array;

/**
* @psalm-template TEntity of EntityInterface
*
* @psalm-param TEntity $entity
*
* @psalm-return ?TEntity
*/
protected function refreshEntity(EntityInterface $entity): ?EntityInterface
{
return $this->manager->getRepository($entity::class)
->find($entity->getId());
}

protected function saveParameter(?ParameterInterface $parameter, ?ParameterInterface $default = null): bool
{
if (!$parameter instanceof ParameterInterface) {
Expand Down Expand Up @@ -440,4 +423,26 @@ protected function saveParameters(array $parameters, array $defaults = []): bool

return $saved;
}

protected function updateEntities(EntityParameterInterface $parameter): void
{
$metaDatas = \array_filter(
$this->getMetaDatas($parameter),
static fn (MetaData $metaData): bool => $metaData->isEntityInterfaceType(),
);
if ([] === $metaDatas) {
return;
}

$accessor = $this->getAccessor();
foreach ($metaDatas as $metaData) {
$value = $accessor->getValue($parameter, $metaData->property);
if (!$value instanceof EntityInterface) {
continue;
}
$value = $this->manager->getRepository($value::class)
->find($value->getId());
$accessor->setValue($parameter, $metaData->property, $value);
}
}
}
13 changes: 0 additions & 13 deletions src/Parameter/DefaultParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use App\Entity\CalculationState;
use App\Entity\Category;
use App\Traits\MathTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
Expand Down Expand Up @@ -87,16 +86,4 @@ public function setState(?CalculationState $state): self

return $this;
}

public function updateEntities(EntityManagerInterface $manager): void
{
if ($this->category instanceof Category) {
$this->category = $manager->getRepository(Category::class)
->find($this->category->getId());
}
if ($this->state instanceof CalculationState) {
$this->state = $manager->getRepository(CalculationState::class)
->find($this->state->getId());
}
}
}
3 changes: 0 additions & 3 deletions src/Parameter/EntityParameterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

namespace App\Parameter;

use Doctrine\ORM\EntityManagerInterface;

interface EntityParameterInterface extends ParameterInterface
{
public function updateEntities(EntityManagerInterface $manager): void;
}
9 changes: 0 additions & 9 deletions src/Parameter/ProductParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use App\Attribute\Parameter;
use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
Expand Down Expand Up @@ -73,12 +72,4 @@ public function setQuantity(float $quantity): self

return $this;
}

public function updateEntities(EntityManagerInterface $manager): void
{
if ($this->product instanceof Product) {
$this->product = $manager->getRepository(Product::class)
->find($this->product->getId());
}
}
}
4 changes: 2 additions & 2 deletions tests/Parameter/OptionsParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class OptionsParameterTest extends ParameterTestCase
{
public static function getParameterNames(): \Generator
{
yield ['printAddress', 'option_print_address'];
yield ['qrCode', 'option_qr_code'];
yield ['printAddress', 'print_address'];
yield ['qrCode', 'qr_code'];
}

public static function getParameterValues(): \Generator
Expand Down

0 comments on commit b307b5c

Please sign in to comment.