DeployCommand::configure()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 19
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 24
loc 24
ccs 19
cts 19
cp 1
rs 8.9713
cc 1
eloc 19
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Command to deploy an application to a Cloud Foundry.
4
 */
5
6
namespace Graviton\Deployment\Command\CloudFoundry;
7
8
use Graviton\Deployment\Command\AbstractCommand;
9
use Graviton\Deployment\Configuration;
10
use Graviton\Deployment\Deployment;
11
use Graviton\Deployment\Services\CloudFoundry\DeploymentUtils;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
/**
18
 * @author  List of contributors <https://github.com/libgraviton/deploy-scripts/graphs/contributors>
19
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
20
 * @link    http://swisscom.ch
21
 */
22
final class DeployCommand extends AbstractCommand
23
{
24
    /** @var Deployment */
25
    private $deployHandler;
26
27
    /**
28
     * @param Deployment    $deploymentHandler Managing the actual deployment
29
     * @param Configuration $configuration     Set of configuration options to influence the current command.
30
     * @param string|null   $name              Name of the command
31
     */
32 4
    public function __construct(Deployment $deploymentHandler, Configuration $configuration, $name = null)
33
    {
34 4
        parent::__construct($configuration, $name);
35 4
        $this->deployHandler = $deploymentHandler;
36 4
    }
37
38
    /**
39
     * Configures the current command.
40
     *
41
     * @return void
42
     */
43 4 View Code Duplication
    protected function configure()
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...
44
    {
45 4
        $this
46 4
            ->setName('graviton:deployment:cf:deploy')
47 4
            ->setDescription('Deploys an application to a CF instance.')
48 4
            ->addArgument(
49 4
                'applicationName',
50 4
                InputArgument::REQUIRED,
51
                'Which application shall be deployed?'
52 4
            )
53 4
            ->addArgument(
54 4
                'versionName',
55 4
                InputArgument::OPTIONAL,
56 4
                'Which application shall be deployed?',
57
                'unstable'
58 4
            )
59 4
            ->addOption(
60 4
                'no-logout',
61 4
                null,
62 4
                InputOption::VALUE_NONE,
63
                'Will keep the CF session open after deployment. '.
64
                '<fg=yellow;options=bold>Keep in mind to close it by yourself!</fg=yellow;options=bold>'
65 4
            );
66 4
    }
67
68
    /**
69
     * Executes the current command.
70
     *
71
     * @param InputInterface  $input  User input on console
72
     * @param OutputInterface $output Output of the command
73
     *
74
     * @return void
75
     */
76 3
    protected function execute(InputInterface $input, OutputInterface $output)
77
    {
78
        // read options
79 3
        $noLogout = $input->getOption('no-logout');
80
81
        // read arguments
82
        // e.g. graviton-develop
83 3
        $applicationName = $input->getArgument('applicationName').'-'.$input->getArgument('versionName');
84 3
        if ($input->getArgument('versionName') == 'master') {
85 1
            $applicationRoute = $input->getArgument('applicationName');
86 1
        } else {
87 2
            $applicationRoute = $applicationName;
88
        }
89
90 3
        $output->writeln('Deploying application ('.$applicationName.') to a Cloud Foundry instance.');
91
92 3
        DeploymentUtils::login($this->deployHandler, $output, $this->configuration);
93 3
        list($slice, $oldSlice) = DeploymentUtils::determineDeploymentSlice(
94 3
            $this->deployHandler,
95 3
            $output,
96 3
            $this->configuration,
97
            $applicationName
98 3
        );
99 3
        DeploymentUtils::deploy(
100 3
            $this->deployHandler,
101 3
            $output,
102 3
            $this->configuration,
103 3
            $applicationName,
104 3
            $applicationRoute,
105 3
            $slice,
106
            false
107 3
        );
108 3
        DeploymentUtils::createServices($this->deployHandler, $output, $this->configuration, $applicationName, $slice);
109 3
        DeploymentUtils::setEnvironmentVariables(
110 3
            $this->deployHandler,
111 3
            $output,
112 3
            $this->configuration,
113 3
            $applicationName.'-'.$slice
114 3
        );
115
116 3
        DeploymentUtils::startApplication(
117 3
            $this->deployHandler,
118 3
            $output,
119 3
            $this->configuration,
120 3
            $applicationName,
121
            $slice
122 3
        );
123
124 3
        DeploymentUtils::addRoute(
125 3
            $this->deployHandler,
126 3
            $output,
127 3
            $this->configuration,
128 3
            $applicationName,
129 3
            $slice,
130
            $applicationRoute
131 3
        );
132
133 3
        if (!DeploymentUtils::isInitialDeploy()) {
134 3
            DeploymentUtils::cleanUp(
135 3
                $this->deployHandler,
136 3
                $output,
137 3
                $this->configuration,
138 3
                $applicationName,
139 3
                $applicationRoute,
140
                $oldSlice
141 3
            );
142 3
        }
143
144 3 View Code Duplication
        if (true == $noLogout) {
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...
145
            $output->writeln(
146
                '<bg=yellow;fg=black;options=bold>'.
147
                '                                                                           '.PHP_EOL.
148
                '  Cloud Foundry session will not be closed (»no-logout« option was set) !  '.
149
                PHP_EOL.'                                                                           '.
150
                '</bg=yellow;fg=black;options=bold>'
151
            );
152
153
            return;
154
        }
155
156 3
        DeploymentUtils::logout($this->deployHandler, $output, $this->configuration);
157 3
    }
158
}
159