Skip to content

Commit

Permalink
Simplified data query.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Dec 28, 2024
1 parent 71c433f commit 5e94ebf
Show file tree
Hide file tree
Showing 29 changed files with 418 additions and 218 deletions.
40 changes: 20 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions resources/diagrams/abstract_property.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ classDiagram
+getName() string
+setName(string)
+getValue() string|null
+setValue(string|null)
+getBoolean() bool
+setBoolean(bool)
+setValue(mixed)
+getArray() array|null
+setArray(array|null)
+getBoolean() bool
+setBoolean(bool)
+getDate() DateTimeInterface|null
+setDate(DateTimeInterface|null)
+getFloat() float
+setFloat(float)
+getInteger() int
+setInteger(int)
+setString(string|null)
}

class EntityInterface {
Expand Down
6 changes: 4 additions & 2 deletions src/Generator/AbstractEntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ abstract class AbstractEntityGenerator implements GeneratorInterface, ServiceSub

private readonly Generator $generator;

public function __construct(private readonly EntityManagerInterface $manager, FakerService $fakerService)
{
public function __construct(
private readonly EntityManagerInterface $manager,
FakerService $fakerService
) {
$this->generator = $fakerService->getGenerator();
}

Expand Down
7 changes: 5 additions & 2 deletions src/Generator/CalculationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
*/
class CalculationGenerator extends AbstractEntityGenerator
{
public function __construct(EntityManagerInterface $manager, FakerService $fakerService, private readonly CalculationService $service)
{
public function __construct(
EntityManagerInterface $manager,
FakerService $fakerService,
private readonly CalculationService $service
) {
parent::__construct($manager, $fakerService);
}

Expand Down
70 changes: 51 additions & 19 deletions src/Resolver/DataQueryValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@
use App\Interfaces\SortModeInterface;
use App\Interfaces\TableInterface;
use App\Service\UrlGeneratorService;
use App\Table\AbstractCategoryItemTable;
use App\Table\CalculationTable;
use App\Table\CategoryTable;
use App\Table\DataQuery;
use App\Table\LogTable;
use App\Table\SearchTable;
use App\Traits\CookieTrait;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Exception\ValidationFailedException;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Value resolver for {@link DataQuery}.
Expand All @@ -38,11 +41,8 @@
{
use CookieTrait;

public function __construct(
private PropertyAccessorInterface $accessor,
private ValidatorInterface $validator,
private TranslatorInterface $translator,
) {
public function __construct(private ValidatorInterface $validator)
{
}

/**
Expand Down Expand Up @@ -137,22 +137,54 @@ private function updateParameters(DataQuery $query, Request $request): void

/**
* @psalm-param InputBag<string> $inputBag
*
* @psalm-suppress PropertyTypeCoercion
*/
private function updateQuery(DataQuery $query, InputBag $inputBag): void
{
/** @psalm-var mixed $value */
foreach ($inputBag as $key => $value) { // @phpstan-ignore varTag.type
if (UrlGeneratorService::PARAM_CALLER === $key) {
continue;
}
if (!$this->accessor->isWritable($query, $key)) {
$message = $this->formatError($key, $this->translator->trans('schema.fields.error'));
throw new BadRequestHttpException($message);
}
if (TableInterface::PARAM_VIEW === $key) {
$value = TableView::tryFrom((string) $value) ?? $query->view; // @phpstan-ignore property.nonObject
/** @psalm-var string[] $keys */
$keys = $inputBag->keys();
foreach ($keys as $key) {
switch ($key) {
case UrlGeneratorService::PARAM_CALLER:
// skipped
break;
case TableInterface::PARAM_ID:
$query->id = $inputBag->getInt($key);
break;
case TableInterface::PARAM_SEARCH:
$query->search = $inputBag->getString($key);
break;
case TableInterface::PARAM_SORT:
$query->sort = $inputBag->getString($key);
break;
case TableInterface::PARAM_ORDER:
// @phpstan-ignore assign.propertyType
$query->order = $inputBag->getString($key);
break;
case TableInterface::PARAM_OFFSET:
$query->offset = $inputBag->getInt($key);
break;
case TableInterface::PARAM_LIMIT:
$query->limit = $inputBag->getInt($key);
break;
case TableInterface::PARAM_VIEW:
$query->view = $inputBag->getEnum($key, TableView::class, $query->view);
break;
case CategoryTable::PARAM_GROUP:
case CalculationTable::PARAM_STATE:
case CalculationTable::PARAM_EDITABLE:
case AbstractCategoryItemTable::PARAM_CATEGORY:
$query->addParameter($key, $inputBag->getInt($key));
break;
case LogTable::PARAM_LEVEL:
case LogTable::PARAM_CHANNEL:
case SearchTable::PARAM_ENTITY:
$query->addParameter($key, $inputBag->getString($key));
break;
default:
throw new BadRequestHttpException(\sprintf('Invalid parameter "%s".', $key));
}
$this->accessor->setValue($query, $key, $value);
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/Service/CalculationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
use App\Repository\GroupMarginRepository;
use App\Repository\GroupRepository;
use App\Traits\MathTrait;
use App\Traits\TranslatorAwareTrait;
use Symfony\Contracts\Service\ServiceMethodsSubscriberTrait;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Service to update calculation totals.
Expand All @@ -44,11 +42,9 @@
* user_margin: float,
* groups: ServiceGroupType[]}
*/
class CalculationService implements ServiceSubscriberInterface
class CalculationService
{
use MathTrait;
use ServiceMethodsSubscriberTrait;
use TranslatorAwareTrait;

/**
* Empty row identifier.
Expand Down Expand Up @@ -89,7 +85,8 @@ public function __construct(
private readonly GlobalMarginRepository $globalRepository,
private readonly GroupMarginRepository $marginRepository,
private readonly GroupRepository $groupRepository,
private readonly ApplicationService $service
private readonly ApplicationService $service,
private readonly TranslatorInterface $translator,
) {
}

Expand Down Expand Up @@ -548,4 +545,9 @@ private function reduceGroup(array $group): float
0.0
);
}

private function trans(string $id): string
{
return $this->translator->trans($id);
}
}
8 changes: 4 additions & 4 deletions src/Table/AbstractCategoryItemTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function addSearch(DataQuery $query, QueryBuilder $builder, string $al
{
$repository = $this->getRepository();
$result = parent::addSearch($query, $builder, $alias);
$categoryId = $query->categoryId;
$categoryId = $query->getIntParameter(self::PARAM_CATEGORY);
if (0 !== $categoryId) {
/** @psalm-var string $field */
$field = $repository->getSearchFields('category.id', $alias);
Expand All @@ -63,7 +63,7 @@ protected function addSearch(DataQuery $query, QueryBuilder $builder, string $al

return true;
}
$groupId = $query->groupId;
$groupId = $query->getIntParameter(CategoryTable::PARAM_GROUP);
if (0 !== $groupId) {
/** @psalm-var string $field */
$field = $repository->getSearchFields('group.id', $alias);
Expand All @@ -87,10 +87,10 @@ protected function updateResults(DataQuery $query, DataResults &$results): void
{
parent::updateResults($query, $results);
if (!$query->callback) {
$categoryId = $query->categoryId;
$categoryId = $query->getIntParameter(self::PARAM_CATEGORY);
$results->addParameter(self::PARAM_CATEGORY, $categoryId);

$groupId = $query->groupId;
$groupId = $query->getIntParameter(CategoryTable::PARAM_GROUP);
$results->addParameter(CategoryTable::PARAM_GROUP, $groupId);

$results->addCustomData('dropdown', $this->getDropDownValues());
Expand Down
2 changes: 1 addition & 1 deletion src/Table/AbstractTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ protected function updateResults(DataQuery $query, DataResults &$results): void
{
$results->pageList = $this->getAllowedPageList($results->totalNotFiltered);
$query->limit = [] !== $results->pageList ? \min($query->limit, \max($results->pageList)) : $query->limit;
$results->params = \array_merge($query->parameters(), $results->params);
$results->params = \array_merge($query->params(), $results->params);
if ($query->callback) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Table/CalculationTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function addSearch(DataQuery $query, QueryBuilder $builder, string $al
{
$repository = $this->getRepository();
$result = parent::addSearch($query, $builder, $alias);
$stateId = $query->stateId;
$stateId = $query->getIntParameter(self::PARAM_STATE);
if (0 !== $stateId) {
/** @psalm-var string $field */
$field = $repository->getSearchFields('state.id', $alias);
Expand All @@ -82,7 +82,7 @@ protected function addSearch(DataQuery $query, QueryBuilder $builder, string $al
return true;
}

$stateEditable = $query->stateEditable;
$stateEditable = $query->getIntParameter(self::PARAM_EDITABLE);
if (0 !== $stateEditable) {
/** @psalm-var string $field */
$field = $repository->getSearchFields('state.editable', $alias);
Expand Down Expand Up @@ -123,10 +123,10 @@ protected function updateResults(DataQuery $query, DataResults &$results): void
parent::updateResults($query, $results);
if (!$query->callback) {
$results->addAttribute('row-style', 'styleTextMuted');
$stateId = $query->stateId;
$stateId = $query->getIntParameter(self::PARAM_STATE);
$results->addParameter(self::PARAM_STATE, $stateId);

$stateEditable = $query->stateEditable;
$stateEditable = $query->getIntParameter(self::PARAM_EDITABLE);
$results->addParameter(self::PARAM_EDITABLE, $stateEditable);

$results->addCustomData('dropdown', $this->getDropDownValues());
Expand Down
4 changes: 2 additions & 2 deletions src/Table/CategoryTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function formatTasks(int $value, array $entity): string
protected function addSearch(DataQuery $query, QueryBuilder $builder, string $alias): bool
{
$result = parent::addSearch($query, $builder, $alias);
$groupId = $query->groupId;
$groupId = $query->getIntParameter(self::PARAM_GROUP);
if (0 === $groupId) {
return $result;
}
Expand Down Expand Up @@ -131,7 +131,7 @@ protected function updateResults(DataQuery $query, DataResults &$results): void
{
parent::updateResults($query, $results);
if (!$query->callback) {
$groupId = $query->groupId;
$groupId = $query->getIntParameter(self::PARAM_GROUP);
$results->addParameter(self::PARAM_GROUP, $groupId);
$results->addCustomData('group', $this->getGroup($groupId));
$results->addCustomData('dropdown', $this->getDropDownValues());
Expand Down
Loading

0 comments on commit 5e94ebf

Please sign in to comment.