CheckDockerConfigurationBeforeStarting::reset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Dock\Project\Decorator;
4
5
use Dock\Cli\IO\ConsoleUserInteraction;
6
use Dock\Docker\Compose\Project;
7
use Dock\Doctor\CommandFailedException;
8
use Dock\Doctor\Doctor;
9
use Dock\IO\UserInteraction;
10
use Dock\Project\ProjectManager;
11
use Symfony\Component\Console\Output\NullOutput;
12
use Symfony\Component\Console\Question\Question;
13
14
class CheckDockerConfigurationBeforeStarting implements ProjectManager
15
{
16
    /**
17
     * @var ProjectManager
18
     */
19
    private $projectManager;
20
21
    /**
22
     * @var Doctor
23
     */
24
    private $doctor;
25
26
    /**
27
     * @var UserInteraction
28
     */
29
    private $userInteraction;
30
31
    /**
32
     * @param ProjectManager  $projectManager
33
     * @param Doctor          $doctor
34
     * @param UserInteraction $userInteraction
35
     */
36
    public function __construct(ProjectManager $projectManager, Doctor $doctor, UserInteraction $userInteraction)
37
    {
38
        $this->projectManager = $projectManager;
39
        $this->doctor = $doctor;
40
        $this->userInteraction = $userInteraction;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function start(Project $project)
47
    {
48
        try {
49
            $this->doctor->examine(new NullOutput(), true);
50
        } catch (CommandFailedException $e) {
51
            if (!$this->userInteraction instanceof ConsoleUserInteraction) {
52
                throw $e;
53
            }
54
55
            $answer = $this->userInteraction->ask(new Question(
56
                'It looks like there\'s something wrong with your installation. Would you like to run the `doctor` command ? [Yn]',
57
                'y'
58
            ));
59
60
            if ('y' == strtolower($answer)) {
61
                $this->doctor->examine($this->userInteraction->getOutput(), false);
62
            }
63
        }
64
65
        return $this->projectManager->start($project);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function stop(Project $project)
72
    {
73
        return $this->projectManager->stop($project);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function reset(Project $project, array $containers = [])
80
    {
81
        $this->projectManager->reset($project, $containers);
82
    }
83
}
84