Test Failed
Pull Request — master (#85)
by Keoghan
08:27 queued 05:13
created

DockerSync::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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