PartialShipType   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptions() 0 6 1
A buildForm() 0 27 2
A getBlockPrefix() 0 3 1
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Form\Type\PartialShip;
13
14
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
15
use Sylius\Component\Core\Model\ShipmentInterface;
16
use Symfony\Component\Form\Extension\Core\Type\TextType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\Form\FormEvent;
19
use Symfony\Component\Form\FormEvents;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
use Symfony\Component\Validator\Constraints\Count;
22
23
final class PartialShipType extends AbstractResourceType
24
{
25
    public function buildForm(FormBuilderInterface $builder, array $options): void
26
    {
27
        $builder
28
            ->add('tracking', TextType::class, [
29
                'required' => false,
30
                'label' => 'bitbag_sylius_mollie_plugin.form.shipment.tracking_code',
31
                'attr' => ['placeholder' => 'bitbag_sylius_mollie_plugin.form.shipment.tracking_code'],
32
            ])
33
            ->add('units', ShippingUnitsChoiceType::class, [
34
                'choices' => $options['shipment']->getUnits(),
35
                'label' => 'bitbag_sylius_mollie_plugin.form.shipment.units',
36
                'multiple' => true,
37
                'constraints' => [
38
                    new Count(['min' => 1, 'groups' => ['sylius']]),
39
                ],
40
            ])
41
        ;
42
43
        $builder->addEventListener(FormEvents::POST_SUBMIT, static function (FormEvent $formEvent): void {
44
            /** @var ShipmentInterface $shipment */
45
            $shipment = $formEvent->getData();
46
47
            foreach ($shipment->getUnits() as $unit) {
48
                /** @var ShipmentInterface $oldShipment */
49
                $oldShipment = $unit->getShipment();
50
51
                $oldShipment->removeUnit($unit);
52
            }
53
        });
54
    }
55
56
    public function configureOptions(OptionsResolver $resolver): void
57
    {
58
        $resolver
59
            ->setRequired('shipment')
60
            ->setDefaults(['validation_groups' => ['sylius']])
61
            ->setAllowedTypes('shipment', [ShipmentInterface::class])
62
        ;
63
    }
64
65
    public function getBlockPrefix(): string
66
    {
67
        return 'bitbag_partial_shipment_ship';
68
    }
69
}
70