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

SteamServerType::buildForm()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 67
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 67
rs 8.5896
cc 6
eloc 43
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 DP\Core\GameBundle\Entity\GameRepository;
15
use Symfony\Component\Form\AbstractType;
16
use Symfony\Component\Form\FormBuilderInterface;
17
use DP\GameServer\SteamServerBundle\Entity\SteamServer;
18
use Symfony\Component\Form\FormEvents;
19
use Symfony\Component\Form\FormEvent;
20
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
21
22
class SteamServerType extends AbstractType
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function buildForm(FormBuilderInterface $builder, array $options)
28
    {
29
        $steam = $builder->getData();
30
31
        $builder
32
            ->add('name', 'text', ['label' => 'game.name'])
33
            ->add('machine', 'dedipanel_machine_entity')
34
            ->add('dir', 'text', ['label' => 'game.dir'])
35
            ->add('port', 'integer', ['label' => 'game.port'])
36
            ->add('game', 'entity', [
37
                'label' => 'game.selectGame',
38
                'class' => 'DPGameBundle:Game',
39
                'query_builder' => function(GameRepository $repo) {
40
                    return $repo->getQBAvailableSteamGames();
41
                },
42
            ])
43
            ->add('mode', 'choice', [ // @TODO: Create a GameModeTypEextension
44
                'choices'     => SteamServer::getModeList(), // @TODO: use KnpDictionaryBundle
45
                'empty_value' => 'steam.chooseGameMode',
46
                'label'       => 'steam.gameMode',
47
                'required'    => false,
48
            ])
49
            ->add('maxplayers', 'integer', ['label' => 'game.maxplayers'])
50
            ->add('rconPassword', 'text', [
51
                'label'    => 'game.rcon.password',
52
            ])
53
            ->add('svPassword', 'text', ['label' => 'steam.svPassword', 'required' => false])
54
            ->add('core', 'dedipanel_core_assignment', ['machine' => $steam->getMachine()])
55
            ->add('alreadyInstalled', 'dictionary', [
56
                'name'     => 'yes_no',
57
                'label'    => 'game.isAlreadyInstalled',
58
                'expanded' => true,
59
            ])
60
        ;
61
62
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
63
            /** @var \Symfony\Component\Form\FormInterface $form */
64
            $form  = $event->getForm();
65
            /** @var \DP\GameServer\SteamServerBundle\Entity\SteamServer $steam */
66
            $steam = $event->getData();
67
68
            if ($steam->getId() != null && $steam->getInstallationStatus() > 100) {
69
                $form->add('rebootAt', 'time', [
70
                    'label' => 'steam.rebootAt',
71
                    'required' => false
72
                ]);
73
            }
74
        });
75
76
        $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) {
77
            /** @var \Symfony\Component\Form\FormInterface $form */
78
            $form  = $event->getForm();
79
            /** @var \DP\GameServer\SteamServerBundle\Entity\SteamServer $steam */
80
            $steam = $event->getData();
81
82
            if ($steam->getGame() !== null && $steam->getGame()->getAppId() == 740) { // == csgo
83
                $form->add('mode', 'choice', [
84
                    'choices'     => SteamServer::getModeList(),
85
                    'label'       => 'steam.gameMode',
86
                    'empty_value' => 'steam.chooseGameMode',
87
                ]);
88
            }
89
            elseif ($steam->getGame() !== null) {
90
                $form->remove('mode');
91
            }
92
        });
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 View Code Duplication
    public function setDefaultOptions(OptionsResolverInterface $resolver)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
99
    {
100
        $resolver
101
            ->setDefaults([
102
                'remove_on_create'  => ['core'],
103
                'remove_on_update'  => ['alreadyInstalled'],
104
                'disable_on_update' => ['machine', 'game', 'dir'],
105
            ])
106
        ;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getName()
113
    {
114
        return 'dedipanel_steam';
115
    }
116
}
117