Completed
Push — feature/pixie-port ( a7bad2...84efcb )
by Vladimir
03:38
created

ServerFormCreator::fill()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 4
nop 2
1
<?php
2
/**
3
 * This file contains a form creator for Servers
4
 *
5
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
6
 */
7
8
namespace BZIon\Form\Creator;
9
10
use BZIon\Form\Type\AdvancedModelType;
11
use BZIon\Form\Type\ModelType;
12
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
13
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
14
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
15
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
16
use Symfony\Component\Form\Extension\Core\Type\TextType;
17
use Symfony\Component\Validator\Constraints\Length;
18
use Symfony\Component\Validator\Constraints\NotBlank;
19
20
/**
21
 * Form creator for servers
22
 */
23
class ServerFormCreator extends ModelFormCreator
24
{
25
    const OFFICIAL_MATCH_SERVER = 'oms';
26
    const OFFICIAL_REPLAY_SERVER = 'ors';
27
    const PUBLIC_SERVER = 'ps';
28
    const PUBLIC_REPLAY_SERVER = 'prs';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function build($builder)
34
    {
35
        return $builder
36
            ->add('domain', TextType::class, [
37
                'constraints' => [
38
                    new NotBlank(),
39
                    new Length([
40
                        'max' => 50,
41
                    ]),
42
                ],
43
            ])
44
            ->add(
45
                $builder->create('port', IntegerType::class, array(
0 ignored issues
show
Documentation introduced by
$builder->create('port',...)->setDataLocked(false) is of type object<Symfony\Component\Form\FormConfigBuilder>, but the function expects a string|integer|object<Sy...m\FormBuilderInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
                    'constraints' => new NotBlank(),
47
                    'data'        => 5154
48
                ))->setDataLocked(false) // Don't lock the data so we can change the default value later if needed
49
            )
50
            ->add('name', TextType::class, [
51
                'constraints' => [
52
                    new NotBlank(),
53
                    new Length([
54
                        'max' => 100,
55
                    ]),
56
                ],
57
            ])
58
            ->add('country', new ModelType('Country'), [
59
                'constraints' => [
60
                    new NotBlank(),
61
                ],
62
            ])
63
            ->add('owner', new AdvancedModelType('Player'), [
64
                'constraints' => [
65
                    new NotBlank(),
66
                ],
67
            ])
68
            ->add('server_type', ChoiceType::class, [
69
                'choices' => [
70
                    self::OFFICIAL_MATCH_SERVER  => 'Official Match Server',
71
                    self::OFFICIAL_REPLAY_SERVER => 'Official Replay Server',
72
                    self::PUBLIC_SERVER          => 'Public Server',
73
                    self::PUBLIC_REPLAY_SERVER   => 'Public Replay Server',
74
                ],
75
                'required' => true,
76
                'label' => 'Server Type',
77
            ])
78
            ->add('inactive', CheckboxType::class, [
79
                'label' => 'Server Inactive',
80
                'required' => false,
81
                'attr' => [
82
                    'data-help-message' => 'When checked, that means this server is no longer active in hosting',
83
                ],
84
            ])
85
            ->add('enter', SubmitType::class, [
86
                'attr' => [
87
                    'class' => 'c-button--blue pattern pattern--downward-stripes',
88
                ],
89
            ])
90
        ;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     *
96
     * @param \Server $server
97
     */
98
    public function fill($form, $server)
99
    {
100
        $form->get('name')->setData($server->getName());
101
        $form->get('domain')->setData($server->getDomain());
102
        $form->get('port')->setData($server->getPort());
103
        $form->get('country')->setData($server->getCountry());
104
        $form->get('owner')->setData($server->getOwner());
105
        $form->get('inactive')->setData($server->isInactive());
106
107
        $serverType = $form->get('server_type');
108
109
        if ($server->isOfficialServer()) {
110
            if ($server->isReplayServer()) {
111
                $serverType->setData(self::OFFICIAL_REPLAY_SERVER);
112
            }
113
            else {
114
                $serverType->setData(self::OFFICIAL_MATCH_SERVER);
115
            }
116
        }
117
        else {
118
            if ($server->isReplayServer()) {
119
                $serverType->setData(self::PUBLIC_REPLAY_SERVER);
120
            }
121
            else {
122
                $serverType->setData(self::PUBLIC_SERVER);
123
            }
124
        }
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     *
130
     * @param \Server $server
131
     */
132
    public function update($form, $server)
133
    {
134
        $server
135
            ->setName($form->get('name')->getData())
136
            ->setDomain($form->get('domain')->getData())
137
            ->setPort($form->get('port')->getData())
138
            ->setCountry($form->get('country')->getData()->getId())
139
            ->setOwner($form->get('owner')->getData()->getId())
140
            ->setInactive($form->get('inactive')->getData())
141
        ;
142
143
        $this->updateServerType($server, $form->get('server_type')->getData());
144
145
        $server->forceUpdate();
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function enter($form)
152
    {
153
        $server = \Server::addServer(
154
            $form->get('name')->getData(),
155
            $form->get('domain')->getData(),
156
            $form->get('port')->getData(),
157
            $form->get('country')->getData()->getId(),
158
            $form->get('owner')->getData()->getId()
159
        );
160
161
        $server->setInactive($form->get('inactive')->getData());
162
163
        $this->updateServerType($server, $form->get('server_type')->getData());
164
165
        return $server;
166
    }
167
168
    private function updateServerType(\Server $server, $serverType)
169
    {
170
        switch ($serverType) {
171
            case self::OFFICIAL_MATCH_SERVER:
172
                $server->setOfficialServer(true);
173
                $server->setReplayServer(false);
174
                break;
175
176
            case self::OFFICIAL_REPLAY_SERVER:
177
                $server->setOfficialServer(true);
178
                $server->setReplayServer(true);
179
                break;
180
181
            case self::PUBLIC_SERVER:
182
                $server->setOfficialServer(false);
183
                $server->setReplayServer(false);
184
                break;
185
186
            case self::PUBLIC_REPLAY_SERVER:
187
                $server->setOfficialServer(false);
188
                $server->setReplayServer(true);
189
                break;
190
        }
191
    }
192
}
193