DeployPresenter::actionDeployDatabase()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 21
loc 21
ccs 0
cts 16
cp 0
rs 9.3142
cc 2
eloc 12
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * This file is part of the Deploy module for webcms2.
5
 * Copyright (c) @see LICENSE
6
 */
7
8
namespace AdminModule\DeployModule;
9
10
/**
11
 * Deploy presenter takes care about deployment of applications as well as management of application.
12
 *
13
 * @author Tomas Voslar <[email protected]>
14
 */
15
class DeployPresenter extends BasePresenter
16
{
17
    /**
18
     * Application's instance holder.
19
     * 
20
     * @var \WebCMS\DeployModule\Entity\Application
21
     */
22
    private $application;
23
24
    /**
25
     * Application entity repository.
26
     * 
27
     * @var Doctrine\ORM\EntityRepository
28
     */
29
    private $repository;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 6
    protected function startup()
35
    {
36 6
	   parent::startup();
37
38 6
       $this->repository = $this->em->getRepository('\WebCMS\DeployModule\Entity\Application');
39 6
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 3
    protected function beforeRender()
45
    {
46 3
	   parent::beforeRender();
47 3
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function renderDefault($idPage)
53
    {
54 1
    	$this->reloadContent();
55 1
    	$this->template->idPage = $idPage;
0 ignored issues
show
Documentation introduced by
The property $template is declared private in Nette\Application\UI\Control. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
56 1
    }
57
    
58
    /**
59
     * Creates datagrid with applications.
60
     * 
61
     * @param  string $name                   Name of the datagrid.
62
     * 
63
     * @return \Grido\Grid   Datagrid object.
64
     */
65 4
    protected function createComponentApplicationsGrid($name)
66 4
    {
67 1
        $grid = $this->createGrid($this, $name, '\WebCMS\DeployModule\Entity\Application');
68
        
69 1
        $grid->addColumnText('name', 'Name')->setSortable()->setFilterText();
70 1
        $grid->addColumnText('path', 'Path')->setSortable()->setFilterText();
71 1
        $grid->addColumnText('database', 'Database')->setSortable()->setFilterText();
72
        $grid->addColumnText('servers', 'Servers')->setCustomRender(function($item) {
73 1
            $servers = '';
74 1
            foreach ($item->getServers()->getIterator() as $server) {
75 1
                $servers .= $server->getName() . ', ';
76 1
            }
77
78 1
            return substr($servers, 0, -2);
79 1
        });
80 1
        $grid->addColumnText('applications', 'System version')->setCustomRender(function($item) {
81
		
82 1
              $installedPath = $item->getPath() . 'libs/composer/installed.json';
83
84 1
              if (file_exists($installedPath)) {
85
                  $installed = file_get_contents($item->getPath() . 'libs/composer/installed.json');
86
                  $installed = json_decode($installed);
87
                  foreach ($installed as $package) {
88
                      if ($package->name == 'webcms2/webcms2') {
89
                          return $package->version;
90
                      }
91
                 }
92
             }
93 1
             return 'No system detected.';
94 1
        });
95
96 1
        $grid->addActionHref("deployApplication", 'Deploy', 'deployApplication', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => 'btn btn-primary ajax'));
97 1
        $grid->addActionHref("deployDatabase", 'Deploy db', 'deployDatabase', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => 'btn btn-primary ajax'));
98 1
        $grid->addActionHref("addApplication", 'Edit', 'addApplication', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => 'btn btn-primary ajax'));
99 1
        $grid->addActionHref("deleteApplication", 'Delete', 'deleteApplication', array('idPage' => $this->actualPage->getId()))->getElementPrototype()->addAttributes(array('class' => 'btn btn-primary btn-danger'));
100
        
101 1
        return $grid;
102
    }
103
104
    /**
105
     * Display application form action. 
106
     *
107
     * @param  int  $id      Application's id.
108
     * @param  int  $idPage  Id of the page.
109
     * 
110
     * @return void
111
     */
112 4
    public function actionAddApplication($id, $idPage)
0 ignored issues
show
Unused Code introduced by
The parameter $idPage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114 4
        if (is_numeric($id)) {
115 2
            $this->application = $this->repository->find($id);
116 2
        } else {
117 2
            $this->application = new \WebCMS\DeployModule\Entity\Application;
118
        }   
119 4
    }
120
121
    /**
122
     * Render method for application form.
123
     * 
124
     * @param  int $idPage 
125
     * 
126
     * @return void
127
     */
128 2
    public function renderAddApplication($idPage)
129
    {
130 2
        $this->reloadContent();
131 2
        $this->template->idPage = $idPage;
0 ignored issues
show
Documentation introduced by
The property $template is declared private in Nette\Application\UI\Control. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
132 2
    }
133
134
    /**
135
     * Creates application form component.
136
     * 
137
     * @return  \Nette\Application\UI\Form
138
     */
139 4
    public function createComponentApplicationForm()
140
    {
141 4
        $form = $this->createForm();
142
143 4
        $servers = $this->em->getRepository('\WebCMS\DeployModule\Entity\Server')->findAll();
144
145 4
        $serversArray = array();
146 4
        foreach ($servers as $server) {
147 2
            $serversArray[$server->getId()] = $server->getName();
148 4
        }
149
150 4
        $form->addText('name', 'Name')->setRequired()->setAttribute('class', 'form-control');
151 4
        $form->addText('path', 'Path')->setRequired()->setAttribute('class', 'form-control');
152 4
        $form->addText('database', 'Database')->setRequired()->setAttribute('class', 'form-control');
153 4
        $form->addTextArea('apacheConfig', 'Apache config')
154 4
                ->setRequired()
155 4
                ->setAttribute('class', 'form-control')
156 4
                ->setDefaultValue('<VirtualHost *:80>
157
DocumentRoot /var/www/production/appname.cz
158
ServerName www.appname.cz
159
ServerAlias appname.cz
160
ErrorLog /var/log/appname.log
161 1
TransferLog /var/log/appname.log
162
163 1
<IfModule php5_module>
164
    php_value newrelic.appname "appname.cz"
165
</IfModule>
166
167
<Directory />
168
        Options FollowSymLinks
169
        AllowOverride All
170
</Directory>
171 4
</VirtualHost>');
172 4
        $form->addMultiSelect('servers', 'Production server', $serversArray)->setAttribute('class', 'form-control');
173
174 4
        $form->addSubmit('send', 'Save')->setAttribute('class', 'btn btn-success');
175 4
        $form->onSuccess[] = callback($this, 'applicationFormSubmitted');
176
177 4
        $form->setDefaults($this->application->toArray());
178
179 4
        return $form;
180
    }
181
182
    /**
183
     * Method executed after form is submitted.
184
     * 
185
     * @param  Nette\Forms\Form $form Application form.
186
     * 
187
     * @return void
188
     */
189 2
    public function applicationFormSubmitted($form)
190 1
    {
191 2
        $values = $form->getValues();
192
193 2
        $this->application->removeServers();
194 2
        $this->em->flush();
195
196 2
        $this->application->setName($values->name)
197 2
                          ->setPath($values->path)
198 2
                          ->setDatabase($values->database)
199 2
                          ->setApacheConfig($values->apacheConfig);
200
        
201 2
        foreach($values->servers as $s) {
202
            $server = $this->em->getRepository('\WebCMS\DeployModule\Entity\Server')->find($s);
203
            $this->application->addServer($server);
204 2
        }
205
206 2
        $this->em->persist($this->application);
207 2
        $this->em->flush();
208
209 2
        $this->flashMessage('Application has been added.', 'success');
210 2
        $this->forward('default', array(
211 2
                'idPage' => $this->actualPage->getId()
212 2
            ));
213
    }
214
215
    /**
216
     * Method for deleting of application.
217
     * 
218
     * @param  int $id Id of the application to remove.
219
     * 
220
     * @return void
221
     */
222 1 View Code Duplication
    public function actionDeleteApplication($id)
223
    {
224 1
        $this->application = $this->repository->find($id);
225
226 1
        $this->em->remove($this->application);
227 1
        $this->em->flush();
228
229 1
        $this->flashMessage('Application has been removed', 'success');
230 1
        $this->forward('default', array(
231 1
                'idPage' => $this->actualPage->getId()
232 1
            ));
233
    }
234
235
    /**
236
     * 
237
     * 
238
     * @param  [type] $id [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
239
     * 
240
     * @return [type]     [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
241
     */
242 4 View Code Duplication
    public function actionDeployApplication($id, $idPage)
0 ignored issues
show
Unused Code introduced by
The parameter $idPage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
243
    {
244
        $application = $this->repository->find($id);
245
246
        $deployScript = $this->settings->get('Deploy script', 'deployModule')->getValue();
0 ignored issues
show
Bug introduced by
The method getValue cannot be called on $this->settings->get('De...cript', 'deployModule') (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
247
248
        foreach($application->getServers() as $server) {
249
            $commandString = sprintf($deployScript,
250 4
                $application->getPath(),
251
                $server->getPath() . $application->getName(),
252
                $server->getIp()
253
            );
254
255
            $output = shell_exec($commandString);
256
            $this->flashMessage($server->getName() . ' ' . $output, 'info');
257
        }
258
259
        $this->flashMessage('Application has been deployed.', 'success');
260
        $this->forward('default', array(
261
                'idPage' => $this->actualPage->getId()
262
            ));
263
    }
264
265
    /**
266
     * 
267
     * 
268
     * @param  [type] $id [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
269
     * 
270
     * @return [type]     [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
271
     */
272 View Code Duplication
    public function actionDeployDatabase($id, $idPage)
0 ignored issues
show
Unused Code introduced by
The parameter $idPage is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
273
    {
274
        $application = $this->repository->find($id);
275
276
        $deployScript = $this->settings->get('Deploy database script', 'deployModule')->getValue();
0 ignored issues
show
Bug introduced by
The method getValue cannot be called on $this->settings->get('De...cript', 'deployModule') (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
277
278
        foreach($application->getServers() as $server) {
279
            $commandString = sprintf($deployScript, 
280
            	$application->getDatabase(),
281
            	$server->getIp()
282
            );
283
            
284
            $output = shell_exec($commandString);
285
            $this->flashMessage($server->getName() . ' ' . $output, 'info');
286
        }
287
288
        $this->flashMessage('Database has been deployed on all servers.', 'success');
289
        $this->forward('default', array(
290
                'idPage' => $this->actualPage->getId()
291
            ));
292
    }
293
}
294