AbstractServer::isAlreadyInstalled()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
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\Model;
13
14
use Doctrine\ORM\Mapping as ORM;
15
use Symfony\Component\Validator\Context\ExecutionContextInterface;
16
use Symfony\Component\Validator\Mapping\ClassMetadata;
17
use Symfony\Component\Validator\Constraints as Assert;
18
19
abstract class AbstractServer implements ServerInterface
20
{
21
    /**
22
     * @var integer $installationStatus
23
     *
24
     * @ORM\Column(name="installation_status", type="integer", nullable=true)
25
     */
26
    protected $installationStatus;
27
28
    /**
29
     * @var boolean $alreadyInstalled Used by the creation process
30
     */
31
    protected $alreadyInstalled;
32
33
    /**
34
     * @var array $core
35
     *
36
     * @ORM\Column(name="core", type="simple_array", nullable=true)
37
     */
38
    protected $core = array();
39
40
    /**
41
     * @var string $dir
42
     *
43
     * @ORM\Column(name="dir", type="string", length=64)
44
     */
45
    protected $dir;
46
47
48
    /** {@inheritdoc} */
49
    abstract public function getName();
50
51
    /**
52
     * Set whether is already already installed
53
     *
54
     * @param boolean $alreadyInstalled
55
     *
56
     * @return AbstractServer
57
     */
58
    public function setAlreadyInstalled($alreadyInstalled)
59
    {
60
        $this->alreadyInstalled = $alreadyInstalled;
61
62
        if ($alreadyInstalled) {
63
            $this->installationStatus = 100;
64
        }
65
66
        return $this;
67
    }
68
69
    /**
70
     * Is already installed ? (from form)
71
     *
72
     * @return boolean
73
     */
74
    public function isAlreadyInstalled()
75
    {
76
        return $this->alreadyInstalled || $this->isInstallationEnded();
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function setInstallationStatus($installationStatus)
83
    {
84
        $this->installationStatus = $installationStatus;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getInstallationStatus()
91
    {
92
        return $this->installationStatus;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function isInstallationEnded()
99
    {
100
        return $this->installationStatus >= 101;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getMachine()
107
    {
108
        return $this->machine;
0 ignored issues
show
Bug introduced by
The property machine does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function deleteServer()
115
    {
116
        $this->changeState('stop');
117
118
        return $this->deleteInstallDir();
119
    }
120
121
    public function deleteInstallDir()
122
    {
123
        $conn = $this->getMachine()->getConnection();
124
        $installDir = $this->getAbsoluteDir();
125
126
        if ($conn->dirExists($installDir)) {
127
            return (bool) $conn->getSFTP()->delete($installDir);
128
        }
129
130
        return true;
131
    }
132
133
    /**
134
     * Try to retrieve the most recent percentage in $installLog
135
     *
136
     * @param string $installLog Will seperate log by lines
137
     * @return null|string
138
     */
139
    protected function getPercentFromInstallLog($installLog)
140
    {
141
        // On recherche dans chaque ligne en commencant par la fin
142
        // Un signe "%" afin de connaître le % le plus à jour
143
        $lines = array_reverse(explode("\n", $installLog));
144
145
        foreach ($lines AS $line) {
146
            $percentPos = strpos($line, '%');
147
148
            if ($percentPos !== false) {
149
                $line = substr($line, 0, $percentPos);
150
                $spacePos = strrpos($line, ' ')+1;
151
152
                return substr($line, $spacePos);
153
            }
154
        }
155
156
        return null;
157
    }
158
159
    /** {@inheritdoc} */
160
    public function getFullName()
161
    {
162
        return '[DediPanel] ' . $this->getName();
163
    }
164
165
    /** 
166
     * @var array $core
167
     * @return array
168
     */
169
    public function setCore(array $core = array())
170
    {
171
        $this->core = $core;
172
173
        return $core;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $core; (array) is incompatible with the return type declared by the interface DP\Core\CoreBundle\Model\ServerInterface::setCore of type DP\Core\CoreBundle\Model\ServerInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
174
    }
175
176
    /** {@inheritdoc} */
177
    public function getCore()
178
    {
179
        return $this->core;
180
    }
181
182
    /** {@inheritdoc} */
183
    public function setDir($dir)
184
    {
185
        $this->dir = trim($dir, '/ ');
186
    }
187
188
    /** {@inheritdoc} */
189
    public function getDir()
190
    {
191
        return $this->dir;
192
    }
193
194
    /**
195
     * Get absolute path of server installation directory
196
     *
197
     * @return string
198
     */
199
    public function getAbsoluteDir()
200
    {
201
        return rtrim($this->getMachine()->getHome(), '/') . '/' . $this->getDir() . '/';
202
    }
203
204
    public static function loadValidatorMetadata(ClassMetadata $metadata)
205
    {
206
        $metadata->addConstraint(new Assert\Callback('validateServer'));
207
    }
208
209
    public function validateServer(ExecutionContextInterface $context)
210
    {
211
        if ($this->getMachine() !== null && $this->getMachine()->getConnection() !== null) {
212
            $relDir = $this->getDir();
213
            $absDir = $this->getAbsoluteDir();
214
215
            if (!$this->getMachine()->getConnection()->testSSHConnection()) {
216
                $context->buildViolation('gameServer.assert.machine_unavailable')
217
                    ->atPath('machine')
218
                    ->addViolation();
219
            }
220 View Code Duplication
            elseif (!$this->isAlreadyInstalled() && !empty($relDir)
0 ignored issues
show
Duplication introduced by
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...
221
            && $this->getMachine()->getConnection()->dirExists($absDir)) {
222
                $context->buildViolation('gameServer.assert.directory_exists')
223
                    ->atPath('dir')
224
                    ->addViolation();
225
            }
226 View Code Duplication
            elseif ($this->isAlreadyInstalled() && !empty($relDir)
0 ignored issues
show
Duplication introduced by
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...
227
            && !$this->getMachine()->getConnection()->dirExists($absDir)) {
228
                $context->buildViolation('gameServer.assert.directory_not_exists')
229
                    ->atPath('dir')
230
                    ->addViolation();
231
            }
232
        }
233
    }
234
}
235