Skip to content

Commit

Permalink
added Convertim transport types
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik committed Dec 10, 2024
1 parent f0e5808 commit 8bee1dc
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/DependencyInjection/ShopsysFrameworkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Shopsys\FrameworkBundle\Component\HttpFoundation\TransactionalMasterRequestConditionProviderInterface;
use Shopsys\FrameworkBundle\Component\Router\FriendlyUrl\FriendlyUrlDataProviderInterface;
use Shopsys\FrameworkBundle\Model\Order\Processing\OrderProcessingStack;
use Shopsys\FrameworkBundle\Model\Transport\AbstractTransportTypeEnum;
use Shopsys\FrameworkBundle\Twig\NoVarDumperExtension;
use Shopsys\FrameworkBundle\Twig\VarDumperExtension;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -59,6 +60,9 @@ public function load(array $configs, ContainerBuilder $container): void
$container->registerForAutoconfiguration(DataTypeResolverInterface::class)
->addTag('shopsys.data_type_resolver');

$container->registerForAutoconfiguration(AbstractTransportTypeEnum::class)
->addTag('shopsys.transport_type_enum');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

Expand Down
8 changes: 4 additions & 4 deletions src/Form/Admin/Transport/TransportFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Shopsys\FrameworkBundle\Model\Transport\Transport;
use Shopsys\FrameworkBundle\Model\Transport\TransportData;
use Shopsys\FrameworkBundle\Model\Transport\TransportFacade;
use Shopsys\FrameworkBundle\Model\Transport\TransportTypeEnum;
use Shopsys\FrameworkBundle\Model\Transport\TransportTypeProvider;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
Expand All @@ -41,14 +41,14 @@ class TransportFormType extends AbstractType
/**
* @param \Shopsys\FrameworkBundle\Model\Payment\PaymentFacade $paymentFacade
* @param \Shopsys\FrameworkBundle\Model\Transport\TransportFacade $transportFacade
* @param \Shopsys\FrameworkBundle\Model\Transport\TransportTypeEnum $transportTypeEnum
* @param \Shopsys\FrameworkBundle\Component\Domain\Domain $domain
* @param \Shopsys\FrameworkBundle\Model\Transport\TransportTypeProvider $transportTypeProvider
*/
public function __construct(
private readonly PaymentFacade $paymentFacade,
private readonly TransportFacade $transportFacade,
private readonly TransportTypeEnum $transportTypeEnum,
private readonly Domain $domain,
private readonly TransportTypeProvider $transportTypeProvider,
) {
}

Expand Down Expand Up @@ -105,7 +105,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
])
->add('type', ChoiceType::class, [
'required' => true,
'choices' => $this->transportTypeEnum->getAllIndexedByTranslations(),
'choices' => $this->transportTypeProvider->getAllIndexedByTranslations(),
'constraints' => [
new NotBlank(),
],
Expand Down
28 changes: 28 additions & 0 deletions src/Model/Transport/AbstractTransportTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Model\Transport;

use Shopsys\FrameworkBundle\Component\Enum\AbstractEnum;

abstract class AbstractTransportTypeEnum extends AbstractEnum
{
/**
* @return string[]
*/
public function getEnabledInCartCases(): array
{
return $this->shouldBeDisplayedInDefaultEshopCart() ? $this->getAllCases() : [];
}

/**
* @return bool
*/
abstract public function shouldBeDisplayedInDefaultEshopCart(): bool;

/**
* @return array<string, string>
*/
abstract public function getAllIndexedByTranslations(): array;
}
12 changes: 9 additions & 3 deletions src/Model/Transport/TransportTypeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Shopsys\FrameworkBundle\Model\Transport;

use Shopsys\FrameworkBundle\Component\Enum\AbstractEnum;

class TransportTypeEnum extends AbstractEnum
class TransportTypeEnum extends AbstractTransportTypeEnum
{
public const string TYPE_COMMON = 'common';
public const string TYPE_PACKETERY = 'packetery';
Expand All @@ -23,4 +21,12 @@ public function getAllIndexedByTranslations(): array
t('Personal pickup') => self::TYPE_PERSONAL_PICKUP,
];
}

/**
* @return bool
*/
public function shouldBeDisplayedInDefaultEshopCart(): bool
{
return true;
}
}
44 changes: 44 additions & 0 deletions src/Model/Transport/TransportTypeProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Model\Transport;

class TransportTypeProvider
{
/**
* @param iterable<\Shopsys\FrameworkBundle\Model\Transport\AbstractTransportTypeEnum> $transportTypeEnums
*/
public function __construct(
protected readonly iterable $transportTypeEnums,
) {
}

/**
* @return array
*/
public function getAllIndexedByTranslations(): array
{
$allIndexedByTranslations = [];

foreach ($this->transportTypeEnums as $transportTypeEnum) {
$allIndexedByTranslations = array_merge($allIndexedByTranslations, $transportTypeEnum->getAllIndexedByTranslations());
}

return $allIndexedByTranslations;
}

/**
* @return array<string, string>
*/
public function getAllEnabledInCartIndexedByTranslations(): array
{
$enabledInCartCases = [];

foreach ($this->transportTypeEnums as $transportTypeEnum) {
$enabledInCartCases = array_merge($enabledInCartCases, $transportTypeEnum->getEnabledInCartCases());
}

return $enabledInCartCases;
}
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1125,3 +1125,7 @@ services:

twig.stringloader:
class: Twig\Loader\ArrayLoader

Shopsys\FrameworkBundle\Model\Transport\TransportTypeProvider:
arguments:
$transportTypeEnums: !tagged shopsys.transport_type_enum

0 comments on commit 8bee1dc

Please sign in to comment.