Skip to content

Commit

Permalink
Updated parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Dec 27, 2024
1 parent bf307e5 commit 71c433f
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 100 deletions.
24 changes: 12 additions & 12 deletions composer.lock

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

4 changes: 2 additions & 2 deletions src/Controller/CalculationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ public function pdf(): PdfResponse
* Export a single calculation to a PDF document.
*/
#[Get(path: '/pdf/{id}', name: 'pdf_id', requirements: self::ID_REQUIREMENT)]
public function pdfOne(Calculation $calculation, LoggerInterface $logger): PdfResponse
public function pdfOne(Calculation $calculation): PdfResponse
{
$minMargin = $this->getMinMargin();
$qrcode = $this->getQrCode($calculation);
$doc = new CalculationReport($this, $calculation, $minMargin, $qrcode, $logger);
$doc = new CalculationReport($this, $calculation, $minMargin, $qrcode);

return $this->renderPdfDocument($doc);
}
Expand Down
25 changes: 9 additions & 16 deletions src/Form/Parameters/AbstractParameterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Abstract type for a parameter.
*/
abstract class AbstractParameterType extends AbstractHelperType
{
public function configureOptions(OptionsResolver $resolver): void
Expand All @@ -44,11 +47,10 @@ public function finishView(FormView $view, FormInterface $form, array $options):
}

$child = $children[$key];
$attributes = \array_merge(
$child->vars['attr'] = \array_merge(
$child->vars['attr'] ?? [],
['data-default' => $this->convertValue($key, $value)]
['data-default' => $this->convertValue($value)]
);
$child->vars['attr'] = $attributes;
}
}

Expand All @@ -65,7 +67,7 @@ protected function addCheckboxType(
): void {
$helper->field($field)
->label($label)
->labelClass('checkbox-inline checkbox-switch')
->labelClass('checkbox-inline')
->help($help)
->addCheckboxType();
}
Expand All @@ -75,18 +77,13 @@ protected function addCheckboxType(
*/
abstract protected function getParameterClass(): string;

private function convertValue(string $key, mixed $value): mixed
private function convertValue(mixed $value): mixed
{
if ($value instanceof \BackedEnum) {
return $value->value;
}
if (\is_bool($value)) {
return $value ? 1 : 0;
}

// special case for minimum margin
if ('minMargin' === $key && \is_numeric($value)) {
return (float) $value * 100.0;
return (int) $value;
}

return $value;
Expand All @@ -98,13 +95,9 @@ private function convertValue(string $key, mixed $value): mixed
private function getDefaultValues(FormInterface $form): array
{
$config = $form->getRoot()->getConfig();
if (!$config->hasOption(AbstractHelperParametersType::DEFAULT_VALUES)) {
return [];
}

$key = $this->getParameterClass()::getCacheKey();
/** @psalm-var array<string, array<string, mixed>> $values */
$values = $config->getOption(AbstractHelperParametersType::DEFAULT_VALUES);
$values = $config->getOption(AbstractHelperParametersType::DEFAULT_VALUES, []);

return $values[$key] ?? [];
}
Expand Down
19 changes: 12 additions & 7 deletions src/Form/User/UserRegistrationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@

use App\Entity\User;
use App\Form\FormHelper;
use App\Traits\TranslatorAwareTrait;
use App\Service\ApplicationService;
use App\Service\CaptchaImageService;
use App\Utils\StringUtils;
use Symfony\Component\Form\Event\PreSetDataEvent;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Service\ServiceMethodsSubscriberTrait;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* Type to register a new user.
*/
class UserRegistrationType extends AbstractUserCaptchaType implements ServiceSubscriberInterface
class UserRegistrationType extends AbstractUserCaptchaType
{
use ServiceMethodsSubscriberTrait;
use TranslatorAwareTrait;
public function __construct(
CaptchaImageService $service,
ApplicationService $application,
private readonly TranslatorInterface $translator,
) {
parent::__construct($service, $application);
}

public function configureOptions(OptionsResolver $resolver): void
{
Expand All @@ -49,7 +54,7 @@ protected function addFormFields(FormHelper $helper): void
->notMapped()
->rowClass('mb-0')
->label('registration.agreeTerms.label')
->updateAttribute('data-error', $this->trans('registration.agreeTerms.error'))
->updateAttribute('data-error', $this->translator->trans('registration.agreeTerms.error'))
->addCheckboxType(false);
parent::addFormFields($helper);

Expand Down
10 changes: 9 additions & 1 deletion src/Parameter/ApplicationParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getDefaultState(): ?CalculationState
public function getDefaultValues(): array
{
// the customer and date parameters are omitted because default values are null
return $this->getParametersDefaultValues(
$values = $this->getParametersDefaultValues(
DefaultParameter::class,
DisplayParameter::class,
HomePageParameter::class,
Expand All @@ -111,6 +111,14 @@ public function getDefaultValues(): array
ProductParameter::class,
SecurityParameter::class,
);

// special case for minimum margin
$key = DefaultParameter::getCacheKey();
/** @psalm-var float $minMargin */
$minMargin = $values[$key]['minMargin'];
$values[$key]['minMargin'] = $minMargin * 100.0;

return $values;
}

/**
Expand Down
25 changes: 5 additions & 20 deletions src/Report/CalculationReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,19 @@
use App\Report\Table\GroupsTable;
use App\Report\Table\ItemsTable;
use App\Report\Table\OverallTable;
use App\Traits\LoggerTrait;
use App\Utils\StringUtils;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\Writer\PngWriter;
use fpdf\Enums\PdfMove;
use fpdf\Enums\PdfTextAlignment;
use fpdf\PdfBorder;
use Psr\Log\LoggerInterface;

/**
* Report for a calculation.
*/
class CalculationReport extends AbstractReport
{
use LoggerTrait;
use PdfMemoryImageTrait;

private const QR_CODE_SIZE = 38.0;
Expand All @@ -47,8 +44,7 @@ public function __construct(
AbstractController $controller,
private readonly Calculation $calculation,
private readonly float $minMargin,
private readonly string $qrcode,
private readonly LoggerInterface $logger
private readonly string $qrcode
) {
parent::__construct($controller);
}
Expand All @@ -61,11 +57,6 @@ public function getCalculation(): Calculation
return $this->calculation;
}

public function getLogger(): LoggerInterface
{
return $this->logger;
}

/**
* Gets the minimum allowed margin.
*/
Expand Down Expand Up @@ -124,8 +115,6 @@ private function checkEndHeight(Calculation $calculation): void

/**
* Gets the QR code image data.
*
* @throws \Exception
*/
private function getQrCodeImageData(): ImageData
{
Expand Down Expand Up @@ -209,14 +198,10 @@ private function renderQrCode(): void
return;
}

try {
$data = $this->getQrCodeImageData();
$x = $this->getPageWidth() - $this->getRightMargin() - self::QR_CODE_SIZE;
$y = $this->getPageHeight() - ReportFooter::FOOTER_OFFSET - self::QR_CODE_SIZE;
$this->imageData($data, $x, $y, self::QR_CODE_SIZE, self::QR_CODE_SIZE, $this->qrcode);
} catch (\Exception $e) {
$this->logException($e, $this->trans('report.calculation.error_qr_code'));
}
$data = $this->getQrCodeImageData();
$x = $this->getPageWidth() - $this->getRightMargin() - self::QR_CODE_SIZE;
$y = $this->getPageHeight() - ReportFooter::FOOTER_OFFSET - self::QR_CODE_SIZE;
$this->imageData($data, $x, $y, self::QR_CODE_SIZE, self::QR_CODE_SIZE, $this->qrcode);
}

private function renderTimestampable(Calculation $calculation): void
Expand Down
1 change: 1 addition & 0 deletions src/Service/CalculationUpdateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CalculationUpdateService implements ServiceSubscriberInterface
use ServiceMethodsSubscriberTrait;
use SessionAwareTrait;
use TranslatorAwareTrait;

private const KEY_DATE = 'calculation.update.date';
private const KEY_INTERVAL = 'calculation.update.interval';
private const KEY_STATES = 'calculation.update.states';
Expand Down
6 changes: 1 addition & 5 deletions src/Traits/AuthorizationCheckerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ trait AuthorizationCheckerAwareTrait
#[SubscribedService]
public function getChecker(): AuthorizationCheckerInterface
{
if ($this->checker instanceof AuthorizationCheckerInterface) {
return $this->checker;
}

return $this->checker = $this->getContainerService(__FUNCTION__, AuthorizationCheckerInterface::class);
return $this->checker ??= $this->getContainerService(__FUNCTION__, AuthorizationCheckerInterface::class);
}

public function setChecker(AuthorizationCheckerInterface $checker): static
Expand Down
6 changes: 1 addition & 5 deletions src/Traits/LoggerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ trait LoggerAwareTrait
#[SubscribedService]
public function getLogger(): LoggerInterface
{
if ($this->logger instanceof LoggerInterface) {
return $this->logger;
}

return $this->logger = $this->getContainerService(__FUNCTION__, LoggerInterface::class);
return $this->logger ??= $this->getContainerService(__FUNCTION__, LoggerInterface::class);
}

public function setLogger(LoggerInterface $logger): static
Expand Down
6 changes: 1 addition & 5 deletions src/Traits/SessionAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ trait SessionAwareTrait
#[SubscribedService]
public function getRequestStack(): RequestStack
{
if ($this->requestStack instanceof RequestStack) {
return $this->requestStack;
}

return $this->requestStack = $this->getContainerService(__FUNCTION__, RequestStack::class);
return $this->requestStack ??= $this->getContainerService(__FUNCTION__, RequestStack::class);
}

public function setRequestStack(RequestStack $requestStack): static
Expand Down
6 changes: 1 addition & 5 deletions src/Traits/TranslatorAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ trait TranslatorAwareTrait
#[SubscribedService]
public function getTranslator(): TranslatorInterface
{
if ($this->translator instanceof TranslatorInterface) {
return $this->translator;
}

return $this->translator = $this->getContainerService(__FUNCTION__, TranslatorInterface::class);
return $this->translator ??= $this->getContainerService(__FUNCTION__, TranslatorInterface::class);
}

public function setTranslator(TranslatorInterface $translator): static
Expand Down
2 changes: 1 addition & 1 deletion tests/Form/Parameters/AbstractParameterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testWithAllOptions(): void
$values = [
'action' => EntityAction::SHOW,
'minMargin' => 1.1,
'text' => 'Fake text',
'name' => 'Fake text',
'value' => true,
];
$this->validateParameters($values);
Expand Down
3 changes: 1 addition & 2 deletions tests/Form/User/UserRegistrationTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ protected function getPreloadedExtensions(): array
->willReturn(false);

$translator = $this->createMockTranslator();
$type = new UserRegistrationType($service, $application);
$type->setTranslator($translator);
$type = new UserRegistrationType($service, $application, $translator);

return [
$type,
Expand Down
Loading

0 comments on commit 71c433f

Please sign in to comment.