Failed Conditions
Pull Request — master (#85)
by Keoghan
06:13
created

DockerSync::adjustDockerComposeSetup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 36
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
c 2
b 0
f 0
dl 0
loc 36
ccs 0
cts 16
cp 0
rs 9.7
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace App\Support\DockerSync;
4
5
use App\Models\PhpVersion;
6
use App\PorterLibrary;
7
use App\Support\Contracts\Cli;
8
use App\Support\Mechanics\MacOs;
9
use App\Support\Mechanics\Mechanic;
10
use Illuminate\Filesystem\Filesystem;
11
use Illuminate\Support\Str;
12
use Symfony\Component\Yaml\Yaml;
13
14
class DockerSync
15
{
16
    /** @var Mechanic */
17
    protected $mechanic;
18
    /** @var Cli */
19
    protected $cli;
20
    /** @var FileSystem */
21
    protected $files;
22
    /** @var PorterLibrary */
23
    private $library;
24
25 181
    public function __construct(Mechanic $mechanic, Cli $cli, Filesystem $files, PorterLibrary $library)
26
    {
27 181
        $this->mechanic = $mechanic;
28 181
        $this->cli = $cli;
29 181
        $this->files = $files;
30 181
        $this->library = $library;
31 181
    }
32
33 11
    public function isActive()
34
    {
35 11
        return setting('use_docker-sync') === 'on';
36
    }
37
38
    public function install()
39
    {
40
        $this->checkForMacOs();
41
42
        $this->cli->passthru('gem install --user-install docker-sync');
43
    }
44
45
    protected function checkForMacOs(): void
46
    {
47
        if (get_class($this->mechanic) !== MacOs::class) {
48
            throw new CannotInstallDockerSync('The OS must be MacOs');
49
        }
50
    }
51
52
    public function adjustDockerComposeSetup(string $composeFile)
53
    {
54
        if (!$this->isActive()) {
55
            return;
56
        }
57
58
        $composeYaml = $this->getYaml($composeFile);
59
        $syncYamlFile = dirname($composeFile).'/docker-sync.yml';
60
        $syncYaml = [
61
            'version' => 2,
62
            'syncs'   => $this->getSyncs(),
63
        ];
64
65
//        if (setting('use_mysql') === 'on') {
66
//            $composeYaml['services']['mysql']['volumes'][0] = $this->replaceSync($composeYaml['services']['mysql']['volumes'][0], 'mysql-data');
67
//        }
68
//
69
//        if (setting('use_redis') === 'on') {
70
//            $composeYaml['services']['redis']['volumes'][0] = $this->replaceSync($composeYaml['services']['redis']['volumes'][0], 'redis-data');
71
//        }
72
73
        foreach (PhpVersion::active()->get() as $version) {
74
            $composeYaml['services'][$version->cli_name]['volumes'][0] = $this->replaceSync($composeYaml['services'][$version->cli_name]['volumes'][0]);
75
            $composeYaml['services'][$version->fpm_name]['volumes'][0] = $this->replaceSync($composeYaml['services'][$version->fpm_name]['volumes'][0]);
76
        }
77
78
        $composeYaml['services']['node']['volumes'][0] = $this->replaceSync($composeYaml['services']['node']['volumes'][0]);
79
        $composeYaml['services']['nginx']['volumes'][0] = $this->replaceSync($composeYaml['services']['nginx']['volumes'][0]);
80
81
        $composeYaml['volumes'] = array_map(function () {
82
            return ['external' => true];
83
        }, $this->getSyncs());
84
85
        $this->putYaml($composeFile, $composeYaml);
86
//        $this->putYaml(str_replace('docker-compose', 'docker-compose-dev', $composeFile), $composeYaml);
87
        $this->putYaml($syncYamlFile, $syncYaml);
88
    }
89
90
    protected function getSyncs()
91
    {
92
        return [
93
            'home' => [
94
                'src'           => setting('home'),
95
                'watch_excludes'=> ['.*/.git', '.*/node_modules'],
96
            ],
97
//            'mysql-data' => $this->library->path().'/data/mysql',
98
//            'redis-data' => $this->library->path().'/data/redis',
99
        ];
100
    }
101
102
    protected function replaceSync($string, $sync = 'home')
103
    {
104
        $pathParts = explode(':', $string);
105
106
        $source = $pathParts[0];
107
        $target = $pathParts[1];
108
        // $settings = $pathParts[2] ?? '';
109
110
        $syncPath = $this->getSyncs()[$sync]['src'];
111
112
        if ($source !== $syncPath) {
0 ignored issues
show
introduced by
The condition $source !== $syncPath is always true.
Loading history...
113
            return $string;
114
        }
115
116
        return implode(':', [$sync, $target, 'nocopy']);
117
//        return [
118
//            'type' => 'volume',
119
//            'source' => $sync,
120
//            'target' => $target,
121
//            'volume' => ['nocopy' => true],
122
//        ];
123
    }
124
125
    public function getYaml(string $file)
126
    {
127
        return Yaml::parseFile($file);
128
    }
129
130
    public function putYaml(string $file, array $yaml)
131
    {
132
        $this->files->put($file, Yaml::dump($yaml, 5, 2));
133
    }
134
135 4
    public function startDaemon()
136
    {
137 4
        if (!$this->isActive()) {
138 4
            return;
139
        }
140
141
        $this->cli->execRealTime($this->getPath().'docker-sync start --config="'.$this->library->path().'/docker-sync.yml"');
142
    }
143
144 4
    public function stopDaemon()
145
    {
146 4
        if (!$this->isActive()) {
147 4
            return;
148
        }
149
150
        $this->cli->execRealTime($this->getPath().'docker-sync stop --config="'.$this->library->path().'/docker-sync.yml"');
151
    }
152
153
    public function getPath()
154
    {
155
        return str_replace("\n", '', $this->cli->exec("ruby -r rubygems -e 'puts Gem.user_dir'")).'/bin/';
156
    }
157
}
158