AbstractFormBuilder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 9
dl 0
loc 154
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A createForm() 0 13 1
A getElement() 0 4 1
A getRule() 0 4 1
A getFilter() 0 4 1
A getDependency() 0 4 1
A getRepositoryTransformer() 0 8 1
A getFormService() 0 4 1
buildForm() 0 1 ?
A initService() 0 9 1
A dispatchFormEvent() 0 6 1
B addMetadataFieldset() 0 28 1
A addShopsFieldset() 0 14 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Bundle\CoreBundle\Form;
14
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use WellCommerce\Bundle\CoreBundle\DependencyInjection\AbstractContainerAware;
17
use WellCommerce\Bundle\CoreBundle\Doctrine\Repository\RepositoryInterface;
18
use WellCommerce\Bundle\CoreBundle\Entity\EntityInterface;
19
use WellCommerce\Bundle\CoreBundle\Form\DataTransformer\DataTransformerFactory;
20
use WellCommerce\Bundle\CoreBundle\Form\DataTransformer\RepositoryAwareDataTransformerInterface;
21
use WellCommerce\Component\Form\Dependencies\DependencyInterface;
22
use WellCommerce\Component\Form\Elements\ElementInterface;
23
use WellCommerce\Component\Form\Elements\FormInterface;
24
use WellCommerce\Component\Form\Event\FormEvent;
25
use WellCommerce\Component\Form\Filters\FilterInterface;
26
use WellCommerce\Component\Form\FormBuilderInterface;
27
use WellCommerce\Component\Form\Handler\FormHandlerInterface;
28
use WellCommerce\Component\Form\Resolver\FormResolverFactoryInterface;
29
use WellCommerce\Component\Form\Rules\RuleInterface;
30
31
/**
32
 * Class AbstractFormBuilder
33
 *
34
 * @author Adam Piotrowski <[email protected]>
35
 */
36
abstract class AbstractFormBuilder extends AbstractContainerAware implements FormBuilderInterface
37
{
38
    /**
39
     * @var FormResolverFactoryInterface
40
     */
41
    protected $resolverFactory;
42
    
43
    /**
44
     * @var FormHandlerInterface
45
     */
46
    protected $formHandler;
47
    
48
    /**
49
     * @var DataTransformerFactory
50
     */
51
    protected $dataTransformerFactory;
52
    
53
    /**
54
     * @var EventDispatcherInterface
55
     */
56
    protected $eventDispatcher;
57
    
58
    public function __construct(
59
        FormResolverFactoryInterface $resolverFactory,
60
        FormHandlerInterface $formHandler,
61
        DataTransformerFactory $dataTransformerFactory,
62
        EventDispatcherInterface $eventDispatcher
63
    ) {
64
        $this->resolverFactory        = $resolverFactory;
65
        $this->formHandler            = $formHandler;
66
        $this->dataTransformerFactory = $dataTransformerFactory;
67
        $this->eventDispatcher        = $eventDispatcher;
68
    }
69
    
70
    public function createForm($defaultData = null, array $options = []): FormInterface
71
    {
72
        $defaultOptions = ['name' => $this->getAlias()];
73
        $options        = array_merge($defaultOptions, $options);
74
        $form           = $this->getFormService($options);
75
        
76
        $this->buildForm($form);
77
        $this->dispatchFormEvent($form, $defaultData, FormEvent::FORM_PRE_INIT_EVENT);
78
        $this->formHandler->initForm($form, $defaultData);
79
        $this->dispatchFormEvent($form, $defaultData, FormEvent::FORM_POST_INIT_EVENT);
80
        
81
        return $form;
82
    }
83
    
84
    public function getElement(string $alias, array $options = []): ElementInterface
85
    {
86
        return $this->initService('element', $alias, $options);
87
    }
88
    
89
    public function getRule(string $alias, array $options = []): RuleInterface
90
    {
91
        return $this->initService('rule', $alias, $options);
92
    }
93
    
94
    public function getFilter(string $alias, array $options = []): FilterInterface
95
    {
96
        return $this->initService('filter', $alias, $options);
97
    }
98
    
99
    public function getDependency(string $alias, array $options = []): DependencyInterface
100
    {
101
        return $this->initService('dependency', $alias, $options);
102
    }
103
    
104
    public function getRepositoryTransformer(string $alias, RepositoryInterface $repository): RepositoryAwareDataTransformerInterface
105
    {
106
        /** @var RepositoryAwareDataTransformerInterface $transformer */
107
        $transformer = $this->dataTransformerFactory->createRepositoryTransformer($alias);
108
        $transformer->setRepository($repository);
109
        
110
        return $transformer;
111
    }
112
    
113
    protected function getFormService(array $options): FormInterface
114
    {
115
        return $this->getElement('form', $options);
116
    }
117
    
118
    abstract protected function buildForm(FormInterface $form);
119
    
120
    /**
121
     * Initializes a service by its type
122
     *
123
     * @param string $type
124
     * @param string $alias
125
     * @param array  $options
126
     *
127
     * @return object
128
     */
129
    protected function initService(string $type, string $alias, array $options)
130
    {
131
        $id      = $this->resolverFactory->resolve($type, $alias);
132
        $service = $this->get($id);
133
        
134
        $service->setOptions($options);
135
        
136
        return $service;
137
    }
138
    
139
    protected function dispatchFormEvent(FormInterface $form, EntityInterface $entity = null, string $name)
140
    {
141
        $eventName = sprintf('%s.%s', $this->getAlias(), $name);
142
        
143
        $this->eventDispatcher->dispatch($eventName, new FormEvent($this, $form, $entity));
0 ignored issues
show
Bug introduced by Adam Piotrowski
It seems like $entity defined by parameter $entity on line 139 can also be of type object<WellCommerce\Bund...Entity\EntityInterface>; however, WellCommerce\Component\F...ormEvent::__construct() does only seem to accept null, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
144
    }
145
    
146
    protected function addMetadataFieldset(FormInterface $form, RepositoryInterface $repository)
147
    {
148
        $metadata = $form->addChild($this->getElement('nested_fieldset', [
149
            'name'  => 'metadata',
150
            'label' => 'common.fieldset.meta',
151
        ]));
152
        
153
        $languageData = $metadata->addChild($this->getElement('language_fieldset', [
154
            'name'        => 'translations',
155
            'label'       => 'common.fieldset.translations',
156
            'transformer' => $this->getRepositoryTransformer('translation', $repository),
157
        ]));
158
        
159
        $languageData->addChild($this->getElement('text_field', [
160
            'name'  => 'meta.title',
161
            'label' => 'common.label.meta.title',
162
        ]));
163
        
164
        $languageData->addChild($this->getElement('text_field', [
165
            'name'  => 'meta.keywords',
166
            'label' => 'common.label.meta.keywords',
167
        ]));
168
        
169
        $languageData->addChild($this->getElement('text_area', [
170
            'name'  => 'meta.description',
171
            'label' => 'common.label.meta.description',
172
        ]));
173
    }
174
    
175
    protected function addShopsFieldset(FormInterface $form)
176
    {
177
        $shopsData = $form->addChild($this->getElement('nested_fieldset', [
178
            'name'  => 'shops_data',
179
            'label' => 'common.fieldset.shops',
180
        ]));
181
        
182
        $shopsData->addChild($this->getElement('multi_select', [
183
            'name'        => 'shops',
184
            'label'       => 'common.label.shops',
185
            'options'     => $this->get('shop.dataset.admin')->getResult('select'),
186
            'transformer' => $this->getRepositoryTransformer('collection', $this->get('shop.repository')),
187
        ]));
188
    }
189
}
190