ShippingMethodMappingType   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 50
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B buildForm() 0 39 2
A configureOptions() 0 7 1
1
<?php
2
3
namespace Loevgaard\PakkelabelsBundle\Form;
4
5
use Loevgaard\PakkelabelsBundle\Entity\ShippingMethodMapping;
6
use Symfony\Component\Form\AbstractType;
7
use Symfony\Component\Form\CallbackTransformer;
8
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
9
use Symfony\Component\Form\Extension\Core\Type\TextType;
10
use Symfony\Component\Form\FormBuilderInterface;
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
13
class ShippingMethodMappingType extends AbstractType
14
{
15
    public function buildForm(FormBuilderInterface $builder, array $options)
16
    {
17
        $builder
18
            ->add('source', null, [
19
                'label' => 'shipping_method_mapping.label.source',
20
            ])
21
            ->add('productCode', null, [
22
                'label' => 'shipping_method_mapping.label.product_code',
23
            ])
24
            ->add('serviceCodes', TextType::class, [
25
                'label' => 'shipping_method_mapping.label.service_codes',
26
            ])
27
            ->add('returnProductCode', null, [
28
                'label' => 'shipping_method_mapping.label.return_product_code',
29
            ])
30
            ->add('returnServiceCodes', TextType::class, [
31
                'label' => 'shipping_method_mapping.label.return_service_codes',
32
            ])
33
            ->add('save', SubmitType::class, [
34
                'label' => 'layout.save',
35
            ])
36
        ;
37
38
        $transformer = new CallbackTransformer(
39
            function ($serviceCodesAsArray) {
40
                if (!is_array($serviceCodesAsArray)) {
41
                    return '';
42
                }
43
44
                return join(', ', $serviceCodesAsArray);
45
            },
46
            function ($serviceCodesAsString) {
47
                return preg_split('/[,; ]+/i', $serviceCodesAsString, null, PREG_SPLIT_NO_EMPTY);
48
            }
49
        );
50
51
        $builder->get('serviceCodes')->addModelTransformer($transformer);
52
        $builder->get('returnServiceCodes')->addModelTransformer($transformer);
53
    }
54
55
    public function configureOptions(OptionsResolver $resolver)
56
    {
57
        $resolver->setDefaults([
58
            'data_class' => ShippingMethodMapping::class,
59
            'translation_domain' => 'LoevgaardPakkelabelsBundle',
60
        ]);
61
    }
62
}
63