LoanType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 83
ccs 0
cts 32
cp 0
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A mapFormsToData() 0 9 1
A __construct() 0 3 1
A configureOptions() 0 5 1
A mapDataToForms() 0 7 1
A buildForm() 0 27 1
1
<?php
2
namespace App\Form\Type;
3
4
use App\DTO\LoanDTO;
5
use Symfony\Component\Form\AbstractType;
6
use Symfony\Component\Form\Extension\Core\Type\DateType;
7
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
8
use Symfony\Component\Form\Extension\Core\Type\TextType;
9
use Symfony\Component\Form\FormBuilderInterface;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
use Symfony\Component\Form\DataMapperInterface;
12
use Symfony\Component\Form\FormInterface;
13
use App\Form\DataTransformer\UuidToItemTransformer;
14
use Tetranz\Select2EntityBundle\Form\Type\Select2EntityType;
15
16
class LoanType extends AbstractType  implements DataMapperInterface
17
{
18
    private $transformer;
19
20
    public function __construct(UuidToItemTransformer $transformer)
21
    {
22
        $this->transformer = $transformer;
23
    }
24
25
    /**
26
     * @param FormBuilderInterface $builder
27
     * @param array $options
28
     */
29
    public function buildForm(FormBuilderInterface $builder, array $options): void
30
    {
31
        $builder
32
            ->add('item', Select2EntityType::class, [
33
                'multiple' => false,
34
                'required' => true,
35
                'remote_route' => 'admin_items_autocomplete',
36
                'remote_params' => [], // static route parameters for request->query
37
                'class' => '\App\Entity\Item',
38
                'primary_key' => 'id',
39
                'text_property' => 'name',
40
                'minimum_input_length' => 3,
41
                'page_limit' => 10,
42
                'allow_clear' => true,
43
                'delay' => 250,
44
                'cache' => true,
45
                'cache_timeout' => 60000, // if 'cache' is true
46
                'language' => 'en',
47
                'placeholder' => 'Select an item'
48
            ])
49
            ->add('loaner', TextType::class, ['required' => false])
50
            ->add('loanDate', DateType::class, ['required' => false, 'widget' => 'single_text', 'html5' => true, 'attr' => ['placeholder' => 'yyyy-mm-dd'], 'placeholder' => 'Y-m-d','input'  => 'datetime', 'format' => 'yyyy-MM-dd', 'input_format' => 'Y-m-d'])
51
            ->add('returnDate', DateType::class, ['required' => false, 'widget' => 'single_text', 'html5' => true, 'attr' => ['placeholder' => 'yyyy-mm-dd'], 'input'  => 'datetime', 'input_format' => 'Y-m-d'])
52
            ->add('submit', SubmitType::class)
53
            ->setDataMapper($this);
54
55
            $builder->get('item')->addModelTransformer($this->transformer);
56
    }
57
    
58
    /**
59
     * @param OptionsResolver $resolver
60
     */
61
    public function configureOptions(OptionsResolver $resolver): void
62
    {
63
        $resolver->setDefaults([
64
            'data_class' => LoanDTO::class,
65
            'empty_data' => null,
66
        ]);
67
    }
68
69
    /**
70
     * Maps properties of some data to a list of forms.
71
     *
72
     * @param LoanDTO $loanDTO Structured data
73
     * @param FormInterface[]|\Traversable $forms A list of {@link FormInterface} instances
74
     */
75
    public function mapDataToForms($loanDTO, $forms): void
76
    {
77
        $forms = iterator_to_array($forms);
78
        $forms['item']->setData($loanDTO->getItemId());
79
        $forms['loaner']->setData($loanDTO->getLoaner());
80
        $forms['loanDate']->setData($loanDTO->getLoanDate());
81
        $forms['returnDate']->setData($loanDTO->getReturnDate());
82
    }
83
84
    /**
85
     * Maps the data of a list of forms into the properties of some data.
86
     *
87
     * @param FormInterface[]|\Traversable $forms A list of {@link FormInterface} instances
88
     * @param LoanDTO $loanDTO Structured data
89
     */
90
    public function mapFormsToData($forms, &$loanDTO): void
91
    {
92
        $forms = iterator_to_array($forms);
93
        $loanDTO = new LoanDTO(
94
            null, 
95
            $forms['item']->getData(), 
96
            $forms['loaner']->getData(), 
97
            $forms['loanDate']->getData(), 
98
            $forms['returnDate']->getData()
99
        );
100
    }
101
}