src/FMT/Application/FormType/Subscribers/PaymentSubscriber.php line 82

Open in your IDE?
  1. <?php
  2. /**
  3.  * Author: Anton Orlov
  4.  * Date: 14.04.2018
  5.  * Time: 16:36
  6.  */
  7. namespace FMT\Application\FormType\Subscribers;
  8. use FMT\Domain\Service\PaymentManagerInterface;
  9. use FMT\Domain\Type\Payment\Donation;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Form\FormEvent;
  12. use Symfony\Component\Form\FormEvents;
  13. use Symfony\Component\Form\FormInterface;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Symfony\Component\Validator\Constraints\LessThanOrEqual;
  17. use Symfony\Component\Validator\Constraints\NotBlank;
  18. class PaymentSubscriber implements EventSubscriberInterface
  19. {
  20.     /** @var AuthorizationCheckerInterface */
  21.     private $checker;
  22.     /**
  23.      * @var TranslatorInterface
  24.      */
  25.     private $translator;
  26.     /**
  27.      * @var PaymentManagerInterface
  28.      */
  29.     private $paymentManager;
  30.     /**
  31.      * PaymentSubscriber constructor.
  32.      * @param AuthorizationCheckerInterface $authChecker
  33.      * @param TranslatorInterface $translator
  34.      * @param PaymentManagerInterface $paymentManager
  35.      */
  36.     public function __construct(
  37.         AuthorizationCheckerInterface $authChecker,
  38.         TranslatorInterface $translator,
  39.         PaymentManagerInterface $paymentManager
  40.     ) {
  41.         $this->checker $authChecker;
  42.         $this->translator $translator;
  43.         $this->paymentManager $paymentManager;
  44.     }
  45.     /**
  46.      * Returns an array of event names this subscriber wants to listen to.
  47.      *
  48.      * The array keys are event names and the value can be:
  49.      *
  50.      *  * The method name to call (priority defaults to 0)
  51.      *  * An array composed of the method name to call and the priority
  52.      *  * An array of arrays composed of the method names to call and respective
  53.      *    priorities, or 0 if unset
  54.      *
  55.      * For instance:
  56.      *
  57.      *  * array('eventName' => 'methodName')
  58.      *  * array('eventName' => array('methodName', $priority))
  59.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  60.      *
  61.      * @return array The event names to listen to
  62.      */
  63.     public static function getSubscribedEvents()
  64.     {
  65.         return [
  66.             FormEvents::PRE_SET_DATA => "onDataPreSet",
  67.             FormEvents::PRE_SUBMIT => "onDataPreSubmit"
  68.         ];
  69.     }
  70.     /**
  71.      * This event removes unnecessary fields for authenticated user
  72.      *
  73.      * @param FormEvent $event
  74.      */
  75.     public function onDataPreSet(FormEvent $event)
  76.     {
  77.         $form $event->getForm();
  78.         $data $event->getData();
  79.         if ($this->checker->isGranted("IS_AUTHENTICATED_REMEMBERED")) {
  80.             $form->remove("email")->remove("first_name")->remove("last_name");
  81.         }
  82.         $options $form->getConfig()->getOptions();
  83.         $dataCheckout = isset($options["attr"]) && isset($options["attr"]["data-checkout"]) && $options["attr"]["data-checkout"];
  84.         if ($dataCheckout) {
  85.             $form->remove("payment_amount");
  86.         }
  87.         if ($data instanceof Donation && !$dataCheckout) {
  88.             $this->addMaxAllowedDonateConstraint($form$data);
  89.         }
  90.     }
  91.     /**
  92.      * This event removes NotBlank constraint in the case when anonymous button selected
  93.      *
  94.      * @param FormEvent $event
  95.      */
  96.     public function onDataPreSubmit(FormEvent $event)
  97.     {
  98.         $form $event->getForm();
  99.         $data $event->getData();
  100.         $isAnonymous = isset($data["anonymous"]) && in_array($data["anonymous"], [1'true'true]);
  101.         if ($isAnonymous) {
  102.             $this->removeNameConstraints($form);
  103.         }
  104.     }
  105.     /**
  106.      * @param FormInterface $form
  107.      */
  108.     private function removeNameConstraints(FormInterface $form)
  109.     {
  110.         $fields array_filter(["first_name""last_name"], function ($field) use ($form) {
  111.             return $form->has($field);
  112.         });
  113.         foreach ($fields as $field) {
  114.             $fieldConfig $form->get($field)->getConfig();
  115.             $configOptions $fieldConfig->getOptions();
  116.             $configOptions["constraints"] = array_filter($configOptions["constraints"], function ($constraint) {
  117.                 return !$constraint instanceof NotBlank;
  118.             });
  119.             $form->add($fieldget_class($fieldConfig->getType()->getInnerType()), $configOptions);
  120.         }
  121.     }
  122.     /**
  123.      * @param FormInterface $form
  124.      * @param Donation $donation
  125.      */
  126.     private function addMaxAllowedDonateConstraint(FormInterface $formDonation $donation)
  127.     {
  128.         $campaign $donation->getStudent()->getUnfinishedCampaign();
  129.         if (empty($campaign)) {
  130.             return;
  131.         }
  132.         $leftAmount $campaign->getAllowedDonateAmount();
  133.         $fees $this->paymentManager->getDonationFees($leftAmount);
  134.         $limit $leftAmount array_sum($fees);
  135.         $field "payment_amount";
  136.         $fieldConfig $form->get($field)->getConfig();
  137.         $configOptions $fieldConfig->getOptions();
  138.         $configOptions["constraints"][] = new LessThanOrEqual([
  139.             'value' => $limit 100,
  140.             'message' => $this->translator->trans('fmt.campaign.too_much_amount', [
  141.                 '${amount}' => $campaign->getAllowedDonateAmountPrice()
  142.             ])
  143.         ]);
  144.         $form->add($fieldget_class($fieldConfig->getType()->getInnerType()), $configOptions);
  145.     }
  146. }