IdentifiedSiteType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 31
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 6 2
A getParent() 0 3 1
A configureOptions() 0 14 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FH\Bundle\MultiSiteBundle\Form\Type;
6
7
use FH\Bundle\MultiSiteBundle\Site\IdentifiedSiteInterface;
8
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
9
use Symfony\Bridge\Doctrine\Form\EventListener\MergeDoctrineCollectionListener;
10
use Symfony\Component\Form\AbstractType;
11
use Symfony\Component\Form\FormBuilderInterface;
12
use Symfony\Component\OptionsResolver\Options;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
/**
16
 * @author Joris van de Sande <[email protected]>
17
 */
18
final class IdentifiedSiteType extends AbstractType
19
{
20
    public function buildForm(FormBuilderInterface $builder, array $options): void
21
    {
22
        if ($options['multiple']) {
23
            $builder
24
                ->addEventSubscriber(new MergeDoctrineCollectionListener())
25
                ->addViewTransformer(new CollectionToArrayTransformer(), true);
26
        }
27
    }
28
29
    public function configureOptions(OptionsResolver $resolver): void
30
    {
31
        $resolver->setDefaults([
32
            'exclude_sites' => [],
33
        ]);
34
35
        $resolver->setNormalizer('choices', function (Options $options, $value) {
36
            if ($options['exclude_sites']) {
37
                $value = array_filter($value, function (IdentifiedSiteInterface $site) use ($options): bool {
38
                    return !\in_array($site->getIdentifier(), $options['exclude_sites'], true);
39
                });
40
            }
41
42
            return $value;
43
        });
44
    }
45
46
    public function getParent(): string
47
    {
48
        return SiteType::class;
49
    }
50
}
51