DockerComposeProjectManager::start()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
1
<?php
2
3
namespace Dock\Project;
4
5
use Dock\Docker\Compose\ComposeExecutableFinder;
6
use Dock\Docker\Compose\Project;
7
use Dock\IO\Process\InteractiveProcessBuilder;
8
use Dock\IO\Process\InteractiveProcessManager;
9
use Dock\IO\ProcessRunner;
10
use Dock\IO\UserInteraction;
11
12
class DockerComposeProjectManager implements ProjectManager, ProjectBuildManager
13
{
14
    /**
15
     * @var InteractiveProcessBuilder
16
     */
17
    private $interactiveProcessBuilder;
18
19
    /**
20
     * @var UserInteraction
21
     */
22
    private $userInteraction;
23
24
    /**
25
     * @var ProcessRunner
26
     */
27
    private $processRunner;
28
29
    /**
30
     * @var ComposeExecutableFinder
31
     */
32
    private $composeExecutableFinder;
33
34
    /**
35
     * @param InteractiveProcessBuilder $interactiveProcessBuilder
36
     * @param UserInteraction           $userInteraction
37
     * @param ProcessRunner             $processRunner
38
     * @param ComposeExecutableFinder   $composeExecutableFinder
39
     */
40
    public function __construct(InteractiveProcessBuilder $interactiveProcessBuilder, UserInteraction $userInteraction, ProcessRunner $processRunner, ComposeExecutableFinder $composeExecutableFinder)
41
    {
42
        $this->interactiveProcessBuilder = $interactiveProcessBuilder;
43
        $this->userInteraction = $userInteraction;
44
        $this->processRunner = $processRunner;
45
        $this->composeExecutableFinder = $composeExecutableFinder;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function start(Project $project)
52
    {
53
        $this->userInteraction->writeTitle('Starting application containers');
54
55
        $this->interactiveProcessBuilder
56
            ->forCommand(sprintf(
57
                '%s up -d',
58
                $this->composeExecutableFinder->find()
59
            ))
60
            ->disableOutput()
61
            ->withoutTimeout()
62
            ->ifTakesMoreThan(5000, function (InteractiveProcessManager $processManager) {
63
                $processManager->enableOutput(true);
64
            })
65
            ->getManager()
66
            ->run();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function stop(Project $project)
73
    {
74
        $this->userInteraction->writeTitle('Stopping application containers');
75
76
        $this->processRunner->followsUpWith($this->composeExecutableFinder->find(), ['stop']);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function reset(Project $project, array $containers = [])
83
    {
84
        $composePath = $this->composeExecutableFinder->find();
85
86
        $this->processRunner->run(implode(' && ', array_map(function ($action) use ($composePath, $containers) {
87
            return implode(' ', [
88
                $composePath,
89
                $action,
90
                implode(' ', $containers),
91
            ]);
92
        }, ['kill', 'rm -f', 'up -d'])));
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function build(Project $project, array $containers = [])
99
    {
100
        $composePath = $this->composeExecutableFinder->find();
101
102
        $this->processRunner->run(implode(' ', [
103
            $composePath,
104
            'build',
105
            implode(' ', $containers),
106
        ]));
107
108
        $this->reset($project, $containers);
109
    }
110
}
111