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

SteamServerType::buildForm()   C

Complexity

Conditions 7
Paths 1

Size

Total Lines 94
Code Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 94
rs 6.4883
cc 7
eloc 59
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\SteamServerBundle\Form;
13
14
use Symfony\Component\Form\AbstractType;
15
use Symfony\Component\Form\FormBuilderInterface;
16
use DP\GameServer\SteamServerBundle\Entity\SteamServer;
17
use Symfony\Component\Form\FormEvents;
18
use Symfony\Component\Form\FormEvent;
19
20
class SteamServerType 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('mode', 'choice', array(
28
                'choices'     => SteamServer::getModeList(),
29
                'empty_value' => 'steam.chooseGameMode',
30
                'label'       => 'steam.gameMode',
31
                'required'    => false,
32
            ))
33
            ->add('maxplayers', 'integer', array('label' => 'game.maxplayers'))
34
            ->add('rconPassword', 'text', array(
35
                'label'    => 'game.rcon.password',
36
                // 'required' => empty($options['data']),
37
            ))
38
            ->add('svPassword', 'text', array('label' => 'steam.svPassword', 'required' => false))
39
            ->add('alreadyInstalled', 'choice', array(
40
                'choices'  => array(1 => 'game.yes', 0 => 'game.no'),
41
                'label'    => 'game.isAlreadyInstalled',
42
                'expanded' => true,
43
            ))
44
        ;
45
46
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
47
            /** @var Symfony\Component\Form\FormInterface $form */
48
            $form  = $event->getForm();
49
            /** @var DP\GameServer\SteamServerBundle\Entity\SteamServer $steam */
50
            $steam = $event->getData();
51
52
            $isUpdateForm = ($steam->getId() != null);
53
54
            $form
55
                ->add('machine', 'dedipanel_machine_entity', array(
56
                    'disabled' => $isUpdateForm,
57
                ))
58
                ->add('game', 'entity', array(
59
                    'label' => 'game.selectGame',
60
                    'class' => 'DPGameBundle:Game',
61
                    'query_builder' => function($repo) {
62
                        return $repo->getQBAvailableSteamGames();
63
                    },
64
                    'disabled' => $isUpdateForm,
65
                ))
66
                ->add('dir', 'text', array(
67
                    'label' => 'game.dir',
68
                    'disabled' => $isUpdateForm,
69
                ))
70
            ;
71
72
            if ($steam->getId() != null) {
73
                $form->remove('alreadyInstalled');
74
75
                if ($steam->getInstallationStatus() > 100) {
76
                    $form->add('rebootAt', 'time', array(
77
                        'label' => 'steam.rebootAt',
78
                        'required' => false
79
                    ));
80
                }
81
                if ($steam->getMachine()->getNbCore() != null) {
82
                    $choices = array_combine(
83
                        range(0, $steam->getMachine()->getNbCore()-1),
84
                        range(1, $steam->getMachine()->getNbCore())
85
                    );
86
87
                    $form->add('core', 'choice', array(
88
                        'label'    => 'game.core',
89
                        'choices'  => $choices,
90
                        'multiple' => true,
91
                        'required' => false,
92
                        'expanded' => true,
93
                    ));
94
                };
95
            }
96
        });
97
98
        $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
99
            /** @var Symfony\Component\Form\FormInterface $form */
100
            $form  = $event->getForm();
101
            /** @var DP\GameServer\SteamServerBundle\Entity\SteamServer $steam */
102
            $steam = $event->getData();
103
104
            if ($steam->getGame() !== null && $steam->getGame()->getAppId() == 740) { // == csgo
105
                $form->add('mode', 'choice', array(
106
                    'choices' => SteamServer::getModeList(),
107
                    'empty_value' => 'steam.chooseGameMode',
108
                    'label' => 'steam.gameMode',
109
                ));
110
            }
111
            elseif ($steam->getGame() !== null) {
112
                $form->remove('mode');
113
            }
114
        });
115
    }
116
117
    public function getName()
118
    {
119
        return 'dedipanel_steam';
120
    }
121
}
122