CommandType::getDefaultOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace HMLB\DDDBundle\Form\Type;
6
7
use HMLB\DDD\Message\Command\Command;
8
use ReflectionClass;
9
use Symfony\Component\Form\AbstractType;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\OptionsResolver\Options;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
14
/**
15
 * CommandType.
16
 *
17
 * @author Hugues Maignol <[email protected]>
18
 */
19
abstract class CommandType extends AbstractType
20
{
21
    /**
22
     * The class name of the Command.
23
     *
24
     * @return string
25
     */
26
    abstract protected function getCommandClass();
27
28
    /**
29
     * Override this to pre-populate the default options of the OptionsResolver.
30
     *
31
     * @return array
32
     */
33
    protected function getDefaultOptions()
34
    {
35
        return [];
36
    }
37
38
    /**
39
     * @param FormInterface $form
40
     * @param Options       $options
41
     *
42
     * @return Command
43
     */
44
    protected function createCommandFromData(FormInterface $form, Options $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        $reflection = new ReflectionClass($this->getCommandClass());
47
        $data = [];
48
        /** @var FormInterface $field */
49
        foreach ($form as $field) {
50
            $data[$field->getName()] = $field->getData();
51
        }
52
53
        return $reflection->newInstanceArgs($data);
54
    }
55
56
    /**
57
     * @param OptionsResolver $resolver
58
     */
59
    public function configureOptions(OptionsResolver $resolver)
60
    {
61
        $setup = [
62
            'data_class' => 'OH\Core\Command\AskQuestion',
63
            'empty_data' => function (Options $options) {
64
                return function (FormInterface $form) use ($options) {
65
                    return $this->createCommandFromData($form, $options);
66
                };
67
            },
68
        ];
69
70
        $merged = array_merge($setup, $this->getDefaultOptions());
71
72
        $resolver->setDefaults($merged);
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getBlockPrefix()
79
    {
80
        return call_user_func($this->getCommandClass().'::messageName');
81
    }
82
}
83