Completed
Pull Request — master (#104)
by Damian
12:02 queued 07:44
created

DockerComposeProjectManager::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 7
Ratio 50 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 14
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
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 View Code Duplication
        $this->processRunner->run(implode(' && ', array_map(function ($action) use ($composePath, $containers) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        $this->processRunner->run(implode(' && ', array_map(function ($action) use ($composePath, $containers) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
            return implode(' ', [
104
                $composePath,
105
                $action,
106
                implode(' ', $containers),
107
            ]);
108
        }, ['build'])));
109
110
        $this->reset($project, $containers);
111
    }
112
}
113