DeployFile::renderHostOptions()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 2
nop 1
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
namespace Reallyli\LaravelDeployer;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Reallyli\LaravelDeployer\Concerns\DeployBuilder;
7
use Reallyli\LaravelDeployer\Concerns\RendersCode;
8
9
class DeployFile
10
{
11
    use RendersCode;
12
    use DeployBuilder;
13
14
    const REPLACEMENT_KEYS = [
15
        'include',
16
        'default',
17
        'options',
18
        'hosts',
19
        'localhost',
20
        'strategies',
21
        'hooks',
22
    ];
23
24
    const SPECIAL_HOST_KEYS = [
25
        'become',
26
        'hostname',
27
        'roles',
28
        'stage',
29
        'user',
30
        'port',
31
        'configFile',
32
        'identityFile',
33
        'forwardAgent',
34
        'multiplexing',
35
    ];
36
37
    protected $data;
38
    protected $filesystem;
39
40
    public function __construct($data = [])
41
    {
42
        $this->data = collect($data);
43
        $this->filesystem = app(Filesystem::class);
44
    }
45
46
    public function __toString()
47
    {
48
        $ds = DIRECTORY_SEPARATOR;
49
        $stub = $this->filesystem->get(__DIR__."{$ds}stubs{$ds}deploy.stub");
50
51
        foreach (static::REPLACEMENT_KEYS as $key) {
52
            $value = call_user_func([$this, 'render'.ucfirst($key)]);
53
            $stub = preg_replace('/{{'.$key.'}}/', $value, $stub);
54
        }
55
56
        // Trim empty lines at the end of file.
57
        $stub = preg_replace('/\n+$/', '', $stub);
58
59
        // Ensure stub has no more than two consecutive empty lines.
60
        $stub = preg_replace('/\n{3,}/', "\n\n", $stub);
61
62
        return $stub;
63
    }
64
65
    public function get($key)
66
    {
67
        return collect($this->data->get($key, []));
68
    }
69
70
    public function updateStrategy($strategy)
71
    {
72
        if (is_string($strategy)) {
73
            $this->data->put('default', $strategy);
74
        }
75
76
        return $this;
77
    }
78
79
    public function store()
80
    {
81
        $path = $this->getDeployFileFullPath();
82
        $dir = dirname($path);
83
84
        if (! is_dir($dir)) {
85
            mkdir($dir, 0777, true);
86
        }
87
88
        $this->filesystem->put($path, (string) $this);
89
90
        return $path;
91
    }
92
93
    protected function renderDefault()
94
    {
95
        $default = $this->data->get('default', 'basic');
96
97
        return "set('strategy', '$default');";
98
    }
99
100
    protected function renderInclude()
101
    {
102
        return $this->get('include')
103
            ->map(function ($include) {
104
                return "require '$include';";
105
            })
106
            ->implode("\n");
107
    }
108
109
    protected function renderStrategies()
110
    {
111
        return $this->get('strategies')
112
            ->map(function ($tasks) {
113
                return collect($tasks)->map(function ($task) {
114
                    return "    '$task',";
115
                })->implode("\n");
116
            })
117
            ->map(function ($tasks, $strategy) {
118
                $title = title_case(str_replace('_', ' ', $strategy)).' Strategy';
119
                $slug = snake_case($strategy);
120
121
                return "desc('$title');\ntask('strategy:$slug', [\n$tasks\n]);";
122
            })
123
            ->implode("\n\n");
124
    }
125
126
    protected function renderOptions()
127
    {
128
        return $this->get('options')
129
            ->map(function ($value, $key) {
130
                $value = $this->render($value, 0, false);
131
132
                return "set('$key', $value);";
133
            })
134
            ->implode("\n");
135
    }
136
137
    protected function renderHosts()
138
    {
139
        return $this->get('hosts')
140
            ->map(function ($options) {
141
                return $this->renderHostOptions($options);
142
            })
143
            ->map(function ($options, $hostname) {
144
                $multipleHostName = explode(',', $hostname);
145
                if (count($multipleHostName) > 1) {
146
                    $hostname = implode("','", $multipleHostName);
147
                }
148
149
                return "host('$hostname')$options;";
150
            })
151
            ->implode("\n\n");
152
    }
153
154
    protected function renderLocalhost()
155
    {
156
        $options = $this->renderHostOptions($this->get('localhost'));
157
158
        return empty($options) ? '' : "localhost()$options;";
159
    }
160
161
    protected function renderHooks()
162
    {
163
        return $this->get('hooks')
164
            ->flatMap(function ($tasks, $hook) {
165
                return collect($tasks)->map(function ($task) use ($hook) {
166
                    switch ($hook) {
167
                        case 'success':
168
                            return "after('success', '$task');";
169
                        case 'fail':
170
                            return "after('deploy:failed', '$task');";
171
                        default:
172
                            return "after('hook:$hook', '$task');";
173
                    }
174
                });
175
            })
176
            ->implode("\n");
177
    }
178
179
    protected function renderHostOptions($options)
180
    {
181
        $options = collect($options)->map(function ($value, $key) {
182
            if ($key === 'sshOptions' && is_array($value)) {
183
                return collect($value)->map(function ($sshValue, $sshKey) {
184
                    $sshValue = $this->render($sshValue, 1, false);
185
186
                    return "    ->addSshOption('$sshKey', $sshValue)";
187
                })->implode("\n");
188
            }
189
190
            $value = $this->render($value, 1, false);
191
192
            return in_array($key, static::SPECIAL_HOST_KEYS)
193
                ? "    ->$key($value)"
194
                : "    ->set('$key', $value)";
195
        })->implode("\n");
196
197
        return empty($options) ? '' : "\n$options";
198
    }
199
}
200