-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract credit cards to separate form type and code cleanup
- Loading branch information
1 parent
5347811
commit 3610926
Showing
8 changed files
with
255 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CommerceWeavers\SyliusTpayPlugin\Form\Type; | ||
|
||
use CommerceWeavers\SyliusTpayPlugin\Repository\CreditCardRepositoryInterface; | ||
use Sylius\Component\Core\Model\CustomerInterface; | ||
use Sylius\Component\Core\Model\OrderInterface; | ||
use Sylius\Component\Core\Model\ShopUserInterface; | ||
use Sylius\Component\Order\Context\CartContextInterface; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Translation\TranslatableMessage; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
final class TpayCreditCardChoiceType extends AbstractType | ||
{ | ||
public function __construct( | ||
private readonly TokenStorageInterface $tokenStorage, | ||
private readonly TranslatorInterface $translator, | ||
private readonly CreditCardRepositoryInterface $creditCardRepository, | ||
private readonly CartContextInterface $cartContext, | ||
) { | ||
} | ||
|
||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$token = $this->tokenStorage->getToken(); | ||
|
||
$user = $token?->getUser(); | ||
|
||
if (!$user instanceof ShopUserInterface) { | ||
return; | ||
} | ||
|
||
/** @var OrderInterface $order */ | ||
$order = $this->cartContext->getCart(); | ||
$channel = $order->getChannel(); | ||
/** @var CustomerInterface $customer */ | ||
$customer = $user->getCustomer(); | ||
|
||
$creditCards = $this->creditCardRepository->findByCustomerAndChannel($customer, $channel); | ||
|
||
$choices = []; | ||
|
||
if (count($creditCards) !== 0) { | ||
$choices = $this->mapChoices($creditCards); | ||
} | ||
|
||
$resolver->setDefaults([ | ||
'choices' => $choices, | ||
'required' => false, | ||
'label' => 'commerce_weavers_sylius_tpay.shop.order_summary.card.use_saved_credit_card.label', | ||
'placeholder' => new TranslatableMessage('commerce_weavers_sylius_tpay.shop.credit_card.use_new_card'), | ||
]); | ||
} | ||
|
||
public function getParent(): string | ||
{ | ||
return ChoiceType::class; | ||
} | ||
|
||
public function getBlockPrefix(): string | ||
{ | ||
return 'cw_tpay_credit_card_choice'; | ||
} | ||
|
||
private function mapChoices(array $creditCards): array | ||
{ | ||
$choices = []; | ||
|
||
foreach ($creditCards as $creditCard) { | ||
$stringifiedCard = $this->translator->trans( | ||
'commerce_weavers_sylius_tpay.shop.credit_card.card_selection_one_liner', | ||
[ | ||
'%brand%' => $creditCard->getBrand(), | ||
'%tail%' => $creditCard->getTail(), | ||
'%expires%' => $creditCard->getExpirationDate()->format('m-Y'), | ||
], | ||
'messages', | ||
); | ||
|
||
$choices[$stringifiedCard] = $creditCard->getId(); | ||
} | ||
|
||
return $choices; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ Sylius\Component\Core\Model\ShopUser: | |
username: '[email protected]' | ||
usernameCanonical: '[email protected]' | ||
|
||
|
||
Sylius\Component\Core\Model\Product: | ||
product_mug: | ||
code: 'MUG' | ||
|
Oops, something went wrong.