Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active July 15, 2019 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/9717a5dddf46650aeb9de5fafd3457a2 to your computer and use it in GitHub Desktop.
Save kurozumi/9717a5dddf46650aeb9de5fafd3457a2 to your computer and use it in GitHub Desktop.
<?php
/*
* Copyright (C) 2019 Akira Kurozumi <info@a-zumi.net>.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
namespace Customize\Form\Extension;
use Doctrine\Common\Collections\ArrayCollection;
use Eccube\Entity\Delivery;
use Eccube\Entity\Order;
use Eccube\Entity\Payment;
use Eccube\Form\Type\Shopping\OrderType;
use Eccube\Repository\PaymentRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Customize\Service\Payment\Method\CreditCard;
class OrderTypeExtension extends AbstractTypeExtension
{
/**
* @var PaymentRepository
*/
private $paymentRepository;
public function __construct(PaymentRepository $paymentRepository)
{
$this->paymentRepository = $paymentRepository;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
if($options['skip_add_form']) {
return;
}
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
/** @var Order $Order */
$Order = $event->getData();
if (null === $Order || !$Order->getId()) {
return;
}
$Deliveries = $this->getDeliveries($Order);
$Payments = $this->getPayments($Deliveries);
$Payments = $this->filterPayments($Payments, $Order->getPaymentTotal());
foreach ($Payments as $Payment) {
//MethodClass書き換え
$Payment->setMethodClass(CreditCard::class);
}
$form = $event->getForm();
$this->addPaymentForm($form, $Payments, $Order->getPayment());
});
}
/**
* Returns the name of the type being extended.
*
* @return string The name of the type being extended
*/
public function getExtendedType()
{
return OrderType::class;
}
private function addPaymentForm(FormInterface $form, array $choices, Payment $data = null)
{
$message = trans('front.shopping.payment_method_unselected');
if (empty($choices)) {
$message = trans('front.shopping.payment_method_not_fount');
}
$form->add('Payment', EntityType::class, [
'class' => Payment::class,
'choice_label' => 'method',
'expanded' => true,
'multiple' => false,
'placeholder' => false,
'constraints' => [
new NotBlank(['message' => $message]),
],
'choices' => $choices,
'data' => $data,
'invalid_message' => $message,
]);
}
/**
* 出荷に紐づく配送方法を取得する.
*
* @param Order $Order
*
* @return Delivery[]
*/
private function getDeliveries(Order $Order)
{
$Deliveries = [];
foreach ($Order->getShippings() as $Shipping) {
$Delivery = $Shipping->getDelivery();
if ($Delivery->isVisible()) {
$Deliveries[] = $Shipping->getDelivery();
}
}
return array_unique($Deliveries);
}
/**
* 配送方法に紐づく支払い方法を取得する
* 各配送方法に共通する支払い方法のみ返す.
*
* @param Delivery[] $Deliveries
*
* @return ArrayCollection
*/
private function getPayments($Deliveries)
{
$PaymentsByDeliveries = [];
foreach ($Deliveries as $Delivery) {
$PaymentOptions = $Delivery->getPaymentOptions();
foreach ($PaymentOptions as $PaymentOption) {
/** @var Payment $Payment */
$Payment = $PaymentOption->getPayment();
if ($Payment->isVisible()) {
$PaymentsByDeliveries[$Delivery->getId()][] = $Payment;
}
}
}
if (empty($PaymentsByDeliveries)) {
return new ArrayCollection();
}
$i = 0;
$PaymentsIntersected = [];
foreach ($PaymentsByDeliveries as $Payments) {
if ($i === 0) {
$PaymentsIntersected = $Payments;
} else {
$PaymentsIntersected = array_intersect($PaymentsIntersected, $Payments);
}
$i++;
}
return new ArrayCollection($PaymentsIntersected);
}
/**
* 支払い方法の利用条件でフィルタをかける.
*
* @param ArrayCollection $Payments
* @param $total
*
* @return Payment[]
*/
private function filterPayments(ArrayCollection $Payments, $total)
{
$PaymentArrays = $Payments->filter(function (Payment $Payment) use ($total) {
$min = $Payment->getRuleMin();
$max = $Payment->getRuleMax();
if (null !== $min && $total < $min) {
return false;
}
if (null !== $max && $total > $max) {
return false;
}
return true;
})->toArray();
usort($PaymentArrays, function (Payment $a, Payment $b) {
return $a->getSortNo() < $b->getSortNo() ? 1 : -1;
});
return $PaymentArrays;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment