SubscriptionType   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 65
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 51 3
A getBlockPrefix() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PH\Bundle\SubscriptionBundle\Form\Type;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use PH\Component\Subscription\Model\Metadata;
9
use PH\Component\Subscription\Model\SubscriptionInterface;
10
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
11
use Symfony\Component\Form\CallbackTransformer;
12
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
13
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
14
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
15
use Symfony\Component\Form\Extension\Core\Type\TextType;
16
use Symfony\Component\Form\FormBuilderInterface;
17
18
final class SubscriptionType extends AbstractResourceType
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function buildForm(FormBuilderInterface $builder, array $options)
24
    {
25
        $builder
26
            ->add('amount', MoneyType::class, [
27
                'currency' => false,
28
            ])
29
            ->add('currencyCode', CurrencyType::class)
30
            ->add('interval', IntervalChoiceType::class)
31
            ->add('type', ChoiceType::class, [
32
                'choices' => [
33
                    'Non-recurring' => SubscriptionInterface::TYPE_NON_RECURRING,
34
                    'Recurring' => SubscriptionInterface::TYPE_RECURRING,
35
                ],
36
            ])
37
            ->add('startDate', StartDateChoiceType::class)
38
        ;
39
40
        $builder->get('startDate')
41
            ->addModelTransformer(new CallbackTransformer(
42
                function ($value) {
43
                    return $value;
44
                },
45
                function ($value) {
46
                    if (is_string($value)) {
47
                        return new \DateTime($value);
48
                    }
49
                }
50
            ))
51
        ;
52
53
        $builder
54
            ->add('metadata', TextType::class);
55
56
        $builder->get('metadata')->addModelTransformer(new CallbackTransformer(
57
            function ($value) {
58
                return $value;
59
            },
60
            function ($value) {
61
                $collection = new ArrayCollection();
62
63
                foreach ((array) $value as $key => $item) {
64
                    $metadata = new Metadata();
65
                    $metadata->setKey($key);
66
                    $metadata->setValue($item);
67
                    $collection->add($metadata);
68
                }
69
70
                return $collection;
71
            }
72
        ));
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getBlockPrefix()
79
    {
80
        return 'ph_subscription';
81
    }
82
}
83