ServerDomainManager::changeState()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 18
loc 18
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
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\Core\CoreBundle\Controller\Server;
13
14
use DP\Core\CoreBundle\Exception\MaxSlotsLimitReachedException;
15
use DP\Core\CoreBundle\Exception\PortAlreadyInUseException;
16
use Sylius\Bundle\ResourceBundle\Controller\DomainManager;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use DP\Core\CoreBundle\Controller\FlashHelper;
20
use Sylius\Bundle\ResourceBundle\Controller\Configuration;
21
use DP\Core\CoreBundle\Model\ServerInterface;
22
use Sylius\Bundle\ResourceBundle\Event\ResourceEvent;
23
use Dedipanel\PHPSeclibWrapperBundle\Connection\Exception\ConnectionErrorException;
24
use DP\Core\CoreBundle\Exception\InstallAlreadyStartedException;
25
use DP\Core\CoreBundle\Exception\MissingPacketException;
26
use DP\Core\CoreBundle\Exception\DirectoryAlreadyExistsException;
27
use DP\Core\CoreBundle\Exception\MaxServerException;
28
use DP\VoipServer\VoipServerBundle\Exception\OfflineServerException;
29
use DP\Core\CoreBundle\Exception\IPBannedException;
30
31
class ServerDomainManager extends DomainManager
32
{
33
    protected $templating;
34
35
    /**
36
     * @{inheritdoc}
37
     */
38
    public function __construct(
39
        ObjectManager $manager,
40
        EventDispatcherInterface $eventDispatcher,
41
        FlashHelper $flashHelper,
42
        Configuration $config,
43
        $templating
44
    ) {
45
        parent::__construct($manager, $eventDispatcher, $flashHelper, $config);
46
47
        $this->templating = $templating;
48
    }
49
50
    /**
51
     * Finalize the installation if the server is already installed
52
     *
53
     * @param ServerInterface $resource
54
     */
55
    public function create($resource)
56
    {
57
        /** @var ResourceEvent $event */
58
        $event = $this->dispatchEvent('pre_create', new ResourceEvent($resource));
59
        $message = $event->getMessage();
60
61
        if (!empty($message)) {
62
            $this->flashHelper->setFlash(
63
                $event->getMessageType(),
64
                $event->getMessage(),
65
                $event->getMessageParameters()
66
            );
67
        }
68
69
        $this->manager->persist($resource);
70
        $this->flashHelper->setFlash(ResourceEvent::TYPE_SUCCESS, 'create');
71
72
        /** @var ResourceEvent $event */
73
        $this->dispatchEvent('post_create', new ResourceEvent($resource));
74
75
        if (!$resource->isInstallationEnded()) {
76
            $this->installationProcess($resource);
77
            $this->manager->persist($resource);
78
        }
79
80
        $this->manager->flush();
81
82
        return $resource;
83
    }
84
85
    /**
86
     * @param object $resource
87
     *
88
     * @return object|null
89
     */
90
    public function delete($resource, $fromMachine = false)
91
    {
92
        /** @var ResourceEvent $event */
93
        $event = $this->dispatchEvent('pre_delete', new ResourceEvent($resource));
94
95
        if ($event->isStopped()) {
96
            $this->flashHelper->setFlash(
97
                $event->getMessageType(),
98
                $event->getMessage(),
99
                $event->getMessageParameters()
100
            );
101
102
            return null;
103
        }
104
105
        $flash = 'delete';
106
107
        if ($fromMachine && $this->deleteFromMachine($resource)) {
108
            $flash = 'full_delete';
109
        }
110
111
        $this->manager->remove($resource);
112
        $this->manager->flush();
113
        $this->flashHelper->setFlash(ResourceEvent::TYPE_SUCCESS, $flash);
114
115
        $this->dispatchEvent('post_delete', new ResourceEvent($resource));
116
117
        return $resource;
118
    }
119
120
    private function deleteFromMachine(ServerInterface $resource)
121
    {
122
        try {
123
            if (!$resource->deleteServer()) {
124
                $this->flashHelper->setFlash('error', 'dedipanel.machine.delete_failed');
125
126
                return false;
127
            }
128
        }
129
        catch (ConnectionErrorException $e) {
130
            $this->flashHelper->setFlash('error', 'dedipanel.machine.connection_failed');
131
132
            return false;
133
        }
134
135
        return true;
136
    }
137
138
    /**
139
     * Start/stop/restart a server
140
     *
141
     * @param ServerInterface $server
142
     * @param $state
143
     * @return ServerInterface|null
144
     */
145 View Code Duplication
    public function changeState(ServerInterface $server, $state)
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...
146
    {
147
        try {
148
            $server->changeState($state);
149
        }
150
        catch (ConnectionErrorException $e) {
151
            $this->flashHelper->setFlash(
152
                ResourceEvent::TYPE_ERROR,
153
                'dedipanel.machine.connection_failed'
154
            );
155
156
            return null;
157
        }
158
159
        $this->flashHelper->setFlash(ResourceEvent::TYPE_SUCCESS, 'dedipanel.flashes.state_changed.' . $state);
160
161
        return $server;
162
    }
163
164
    protected function installationProcess(ServerInterface $server)
165
    {
166
        $progress = $server->getInstallationStatus();
167
168
        if ($progress === null && !$this->install($server)) {
169
            $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.core.install_failed');
170
171
            return false;
172
        }
173
174
        if ($progress != 100) {
175
            $progress = $server->getInstallationProgress();
176
            $server->setInstallationStatus($progress);
177
        }
178
179
        if ($progress == 100) {
180
            if (!$this->finalizeInstall($server)) {
181
                $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.core.post_install_failed');
182
183
                return false;
184
            }
185
186
            $this->flashHelper->setFlash(ResourceEvent::TYPE_SUCCESS, 'dedipanel.flashes.finalize_install_server');
187
        }
188
189
        return true;
190
    }
191
192
    /**
193
     * Launch a server installation
194
     *
195
     * @param ServerInterface $server
196
     */
197
    protected function install(ServerInterface $server)
198
    {
199
        $installed = false;
200
201
        try {
202
            $installed = $server->installServer($this->templating);
203
        }
204
        catch (InstallAlreadyStartedException $e) {
205
            $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.core.installAlreadyStarted');
206
        }
207
        catch (MissingPacketException $e) {
208
            $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.core.missingPacket');
209
        }
210
        catch (ConnectionErrorException $e) {
211
            $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.machine.connection_failed');
212
        }
213
        catch (MaxServerException $e) {
214
            $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.core.max_server_limit');
215
        }
216
        catch (PortAlreadyInUseException $e) {
217
            $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.core.port_in_use');
218
        }
219
        catch (MaxSlotsLimitReachedException $e) {
220
            $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.voip.max_slots');
221
        }
222
        catch (OfflineServerException $e) {
223
            $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.voip.offline_server');
224
        }
225
        catch (IPBannedException $e) {
226
            $params = [];
227
            $duration = $e->getDuration();
228
229
            if (!empty($duration)) {
230
                $params['%duration%'] = $duration;
231
            }
232
233
            $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.voip.banned_from_server');
234
        }
235
236
        return $installed;
237
    }
238
239
    /**
240
     * Finalize a server installation
241
     *
242
     * @param ServerInterface $server
243
     *
244
     * @return boolean
245
     */
246
    protected function finalizeInstall(ServerInterface $server)
247
    {
248
        $finalized = false;
249
250
        try {
251
            $finalized = $server->finalizeInstallation($this->templating);
252
        }
253
        catch (ConnectionErrorException $e) {
254
            $this->flashHelper->setFlash(ResourceEvent::TYPE_ERROR, 'dedipanel.machine.connection_failed');
255
        }
256
257
        return $finalized;
258
    }
259
}
260