MachineCreator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Machine;
4
5
use Cocotte\Console\Style;
6
use Cocotte\DigitalOcean\ApiToken;
7
use Cocotte\Shell\ProcessRunner;
8
use Symfony\Component\Process\Process;
9
10
final class MachineCreator
11
{
12
13
    /**
14
     * @var ProcessRunner
15
     */
16
    private $processRunner;
17
18
    /**
19
     * @var MachineState
20
     */
21
    private $machineState;
22
23
    /**
24
     * @var MachineName
25
     */
26
    private $machineName;
27
28
    /**
29
     * @var ApiToken
30
     */
31
    private $token;
32
33
    /**
34
     * @var Style
35
     */
36
    private $style;
37
38
    public function __construct(
39
        ProcessRunner $processRunner,
40
        MachineState $machineState,
41
        MachineName $machineName,
42
        ApiToken $token,
43
        Style $style
44
    ) {
45
        $this->processRunner = $processRunner;
46
        $this->machineState = $machineState;
47
        $this->machineName = $machineName;
48
        $this->token = $token;
49
        $this->style = $style;
50
    }
51
52
    public function create()
53
    {
54
        if ($this->machineState->exists()) {
55
            throw new \Exception(
56
                "Error: a machine named {$this->machineName} already exists. Remove it or choose a ".
57
                "different machine name. Run the uninstall command to remove it completely."
58
            );
59
        }
60
61
        $process = $this->createProcess();
62
63
        $process->setTimeout(600);
64
65
        $this->processRunner->mustRun($process, true);
66
    }
67
68
    private function createProcess(): Process
69
    {
70
        return Process::fromShellCommandline(
71
            <<<'TAG'
72
docker-machine create \
73
    --driver digitalocean \
74
    --digitalocean-access-token "${DIGITAL_OCEAN_API_TOKEN}" \
75
    --engine-opt log-driver="json-file" \
76
    --engine-opt log-opt="max-size=1m" \
77
    --engine-opt log-opt="max-file=10" \
78
    --engine-install-url "https://releases.rancher.com/install-docker/19.03.9.sh" \
79
    "${MACHINE_NAME}"
80
TAG
81
        );
82
    }
83
84
}
85