CollectionField   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 60
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getViewType() 0 4 1
A getFormType() 0 4 1
A getStorageType() 0 4 1
B configureOptions() 0 35 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Component\ContentType\Standard\Field;
6
7
use Psi\Component\ContentType\FieldInterface;
8
use Psi\Component\ContentType\FieldOptions;
9
use Psi\Component\ContentType\FieldRegistry;
10
use Psi\Component\ContentType\OptionsResolver\FieldOptionsResolver;
11
use Psi\Component\ContentType\Standard\Storage\CollectionType;
12
use Psi\Component\ContentType\Standard\View as View;
13
use Symfony\Component\Form\Extension\Core\Type as Form;
14
15
class CollectionField implements FieldInterface
16
{
17
    private $registry;
18
19
    public function __construct(FieldRegistry $registry)
20
    {
21
        $this->registry = $registry;
22
    }
23
24
    public function getViewType(): string
25
    {
26
        return View\CollectionType::class;
27
    }
28
29
    public function getFormType(): string
30
    {
31
        return Form\CollectionType::class;
32
    }
33
34
    public function getStorageType(): string
35
    {
36
        return CollectionType::class;
37
    }
38
39
    public function configureOptions(FieldOptionsResolver $options)
40
    {
41
        $options->setRequired([
42
            'field_type',
43
        ]);
44
        $options->setDefault('field_options', []);
45
        $options->setFormMapper(function (array $options, array $shared) {
46
47
            // default to allowing add / delete (contrary to the form types
48
            // default behavior).
49
            $options = array_merge([
50
                'allow_add' => true,
51
                'allow_delete' => true,
52
            ], $options);
53
54
            // resolve the form options for the colletion entry.
55
            $field = $this->registry->get($shared['field_type']);
56
            $resolver = new FieldOptionsResolver();
57
            $field->configureOptions($resolver);
58
            $entryOptions = $resolver->resolveFormOptions(FieldOptions::create($shared['field_options']));
59
60
            // do not allow entry_type or entry_options to be overridden.
61
            $options['entry_type'] = $field->getFormType();
62
            $options['entry_options'] = $entryOptions;
63
64
            return $options;
65
        });
66
67
        $options->setViewMapper(function ($options, $shared) {
68
            return array_merge($options, [
69
                'field_type' => $shared['field_type'],
70
                'field_options' => $shared['field_options'],
71
            ]);
72
        });
73
    }
74
}
75