Completed
Push — master ( fdbeef...265a2d )
by jerome
25:01 queued 21:35
created

MinecraftServerType::buildForm()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 62
Code Lines 41

Duplication

Lines 18
Ratio 29.03 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 18
loc 62
rs 9.4743
cc 3
eloc 41
nc 1
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of Dedipanel project
5
 *
6
 * (c) 2010-2015 Dedipanel <http://www.dedicated-panel.net>
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 DP\GameServer\MinecraftServerBundle\Form;
13
14
use DP\Core\GameBundle\Entity\GameRepository;
15
use Symfony\Component\Form\FormBuilderInterface;
16
use Symfony\Component\Form\AbstractType;
17
use Symfony\Component\Form\FormEvents;
18
use Symfony\Component\Form\FormEvent;
19
20
class MinecraftServerType extends AbstractType
21
{
22
    public function buildForm(FormBuilderInterface $builder, array $options)
23
    {
24
        $builder
25
            ->add('name', 'text', array('label' => 'game.name'))
26
            ->add('port', 'integer', array('label' => 'game.port'))
27
            ->add('queryPort', 'integer', array('label' => 'minecraft.queryPort'))
28
            ->add('rconPort', 'integer', array('label' => 'minecraft.rcon.port'))
29
            ->add('rconPassword', 'text', array('label' => 'game.rcon.password'))
30
            ->add('maxplayers', 'integer', array('label' => 'game.maxplayers'))
31
            ->add('minHeap', 'integer', array('label' => 'minecraft.minHeap'))
32
            ->add('maxHeap', 'integer', array('label' => 'minecraft.maxHeap'))->add('alreadyInstalled', 'choice', array(
33
                'choices'  => array(1 => 'game.yes', 0 => 'game.no'),
34
                'label'    => 'game.isAlreadyInstalled',
35
                'expanded' => true,
36
            ))
37
        ;
38
39
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
40
            $form      = $event->getForm();
41
            /** @var DP\GameServer\MinecraftServerBundle\Entity\MinecraftServer $minecraft */
42
            $minecraft = $event->getData();
43
44
            $isUpdateForm = ($minecraft->getId() != null);
45
46
            $form
47
                ->add('machine', 'dedipanel_machine_entity', array(
48
                    'disabled' => $isUpdateForm,
49
                ))
50
                ->add('game', 'entity', array(
51
                    'label' => 'game.selectGame',
52
                    'class' => 'DPGameBundle:Game',
53
                    'query_builder' => function(GameRepository $repo) {
54
                            return $repo->getQBAvailableMinecraftGames();
55
                        },
56
                    'disabled' => $isUpdateForm,
57
                ))
58
                ->add('dir', 'text', array(
59
                    'label' => 'game.dir',
60
                    'disabled' => $isUpdateForm,
61
                ))
62
            ;
63
64 View Code Duplication
            if ($minecraft->getId() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
                $form->remove('alreadyInstalled');
66
67
                if ($minecraft->getMachine()->getNbCore() != null) {
68
                    $choices = array_combine(
69
                        range(0, $minecraft->getMachine()->getNbCore()-1),
70
                        range(1, $minecraft->getMachine()->getNbCore())
71
                    );
72
73
                    $form->add('core', 'choice', array(
74
                        'label'    => 'game.core',
75
                        'choices'  => $choices,
76
                        'multiple' => true,
77
                        'required' => false,
78
                        'expanded' => true,
79
                    ));
80
                }
81
            }
82
        });
83
    }
84
85
    public function getName()
86
    {
87
        return 'dedipanel_minecraft';
88
    }
89
}
90