Completed
Pusheb283a...ad5b4c
passed — Build
created

BaseCommand::parameterToCamelCase()   A

↳ Parent: BaseCommand

Complexity

Conditions 1
Paths 1

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 4
Code Lines 2

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Onigoetz\Deployer\Command;
4
5
use Net_SFTP;
6
use Onigoetz\Deployer\Configuration\ConfigurationManager;
7
use Onigoetz\Deployer\Configuration\Environment;
8
use Onigoetz\Deployer\MethodCaller;
9
use Onigoetz\Deployer\RemoteActionRunner;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\Question;
14
15
class BaseCommand extends Command
16
{
17
    protected $manager;
18
19
    public function __construct(ConfigurationManager $manager)
20
    {
21
        parent::__construct();
22
        $this->manager = $manager;
23
    }
24
25
    protected function prefixPath($path, array $directories)
26
    {
27
        if (strpos($path, '/') === 0) {
28
            return $path;
29
        }
30
31
        return $directories['{{root}}'] . '/' . $path;
32
    }
33
34
    protected function replaceVars($dir, array $directories)
35
    {
36
        return strtr($dir, $directories);
37
    }
38
39
    protected function parameterToCamelCase($key)
40
    {
41
        return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $key))));
42
    }
43
44
    protected function runActions(RemoteActionRunner $runner, $actions, OutputInterface $output, $directories)
45
    {
46
        foreach ($actions as $description => $action) {
47
            $this->runAction(
48
                $description,
49
                $output,
50
                function () use ($runner, $action, $directories) {
51
                    $method = $action['action'];
52
                    unset($action['action']);
53
54
                    $parameters = [];
55
                    foreach ($action as $key => $value) {
56
                        $key = $this->parameterToCamelCase($key);
57
                        $parameters[$key] = $this->replaceVars($value, $directories);
58
59
                        if ($method != 'exec') {
60
                            $parameters[$key] = $this->prefixPath($parameters[$key], $directories);
61
                        }
62
                    }
63
64
                    return (new MethodCaller)->call($runner, $method, $parameters);
65
                }
66
            );
67
        }
68
    }
69
70
    protected function runAction($title, OutputInterface $output, \Closure $closure)
71
    {
72
        $output->write($title);
73
        // 8 is the length of the label + 2 let it breathe
74
        $padding = $this->getApplication()->getTerminalDimensions()[0] - strlen($title) - 10;
75
76
        try {
77
            $response = $closure();
78
        } catch (\Exception $e) {
79
            $output->writeln(str_pad(' ', $padding) . '[ <fg=red>FAIL</fg=red> ]');
80
            throw $e;
81
        }
82
83
        $output->writeln(str_pad(' ', $padding) . '[  <fg=green>OK</fg=green>  ]');
84
        if (!empty($response)) {
85
            $output->writeln('<fg=blue>' . $response . '</fg=blue>');
86
        }
87
    }
88
89
    protected function allServers(
90
        Environment $environment,
91
        InputInterface $input,
92
        OutputInterface $output,
93
        \Closure $action
94
    ) {
95
        //Loop on the servers
96
        /**
97
         * @var \Onigoetz\Deployer\Configuration\Server
98
         */
99
        foreach ($environment->getServers() as $server) {
100
            $output->writeln("Deploying on <info>{$server->getHost()}</info>");
101
            $output->writeln('-------------------------------------------------');
102
103
            //Ask server password if needed ?
104
            if (!$password = $server->getPassword()) {
105
                $text = "Password for <info>{$server->getUsername()}@{$server->getHost()}</info>:";
106
                $question = new Question($text, false);
107
                $question->setHidden(true)->setHiddenFallback(false);
108
                $password = $this->getHelper('question')->ask($input, $output, $question);
109
            }
110
111
            //Login to server
112
            $ssh = new Net_SFTP($server->getHost());
113
            if (!$ssh->login($server->getUsername(), $password)) {
114
                throw new \Exception("Login failed on host '{$server->getHost()}'");
115
            }
116
117
            $action($ssh);
118
119
            $ssh->disconnect();
120
        }
121
    }
122
123
    /**
124
     * Get a valid environment form string
125
     *
126
     * @param $env
127
     * @param OutputInterface $output
128
     * @throws \Exception
129
     * @return Environment
130
     */
131
    protected function getEnvironment($env, OutputInterface $output)
132
    {
133
        try {
134
            /*
135
             * @var Environment
136
             */
137
            $environment = $this->manager->get('environment', $env);
138
        } catch (\LogicException $e) {
139
            throw new \Exception("Environment '$env' doesn't exist");
140
        }
141
142
        if (!$environment->isValid()) {
143
            foreach ($this->manager->getLogs() as $line) {
144
                $output->writeln("<error>$line</error>");
145
            }
146
            throw new \Exception("Invalid configuration for '$env'");
147
        }
148
149
        return $environment;
150
    }
151
}
152