Completed
Push — master ( 269470...f117d4 )
by Paweł
08:47
created

ArchivableType   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 19 2
A getBlockPrefix() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ResourceBundle\Form\Type;
13
14
use Sylius\Component\Resource\Model\ArchivableInterface;
15
use Symfony\Component\Form\AbstractType;
16
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\Form\FormEvent;
19
use Symfony\Component\Form\FormEvents;
20
21
/**
22
 * @author Jan Góralski <[email protected]>
23
 */
24
final class ArchivableType extends AbstractType
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function buildForm(FormBuilderInterface $builder, array $options)
30
    {
31
        $builder
32
            ->add('archivedAt', DateTimeType::class)
33
            ->addEventListener(FormEvents::SUBMIT, function (FormEvent $event) {
34
                /** @var ArchivableInterface $archivable */
35
                $archivable = $event->getData();
36
37
                $archivedAt = null;
38
                if (null === $archivable->getArchivedAt()) {
39
                    $archivedAt = new \DateTime();
40
                }
41
42
                $archivable->setArchivedAt($archivedAt);
43
44
                $event->setData($archivable);
45
            })
46
        ;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getBlockPrefix()
53
    {
54
        return 'sylius_archivable';
55
    }
56
}
57