Completed
Push — master ( 265a2d...f9e310 )
by jerome
05:38 queued 02:58
created

MinecraftServerBundle/Form/MinecraftServerType.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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