DockerMachineCli   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 113
Duplicated Lines 37.17 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 16
c 3
b 0
f 0
lcom 1
cbo 3
dl 42
loc 113
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isRunning() 9 9 2
A start() 8 8 2
A stop() 8 8 2
A getIp() 0 15 3
A create() 8 8 2
A getEnvironmentDeclaration() 0 4 1
A isCreated() 9 9 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Dock\Docker\Machine;
4
5
use Dock\IO\ProcessRunner;
6
use Symfony\Component\Process\Exception\ProcessFailedException;
7
8
class DockerMachineCli implements Machine
9
{
10
    const DEFAULT_MACHINE_NAME = 'inviqa-dock-cli';
11
12
    /**
13
     * @var ProcessRunner
14
     */
15
    private $processRunner;
16
17
    /**
18
     * @var string
19
     */
20
    private $name;
21
22
    /**
23
     * @param ProcessRunner $processRunner
24
     * @param string        $name
25
     */
26
    public function __construct(ProcessRunner $processRunner, $name)
27
    {
28
        $this->processRunner = $processRunner;
29
        $this->name = $name;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 View Code Duplication
    public function isRunning()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
36
    {
37
        $process = $this->processRunner->run('docker-machine status '.$this->name, false);
38
        if (!$process->isSuccessful()) {
39
            return false;
40
        }
41
42
        return strpos($process->getOutput(), 'Running') === 0;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 View Code Duplication
    public function start()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
49
    {
50
        try {
51
            $this->processRunner->run('docker-machine start '.$this->name);
52
        } catch (ProcessFailedException $e) {
53
            throw new MachineException($e->getMessage(), $e->getCode(), $e);
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 View Code Duplication
    public function stop()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
    {
62
        try {
63
            $this->processRunner->run('docker-machine stop '.$this->name);
64
        } catch (ProcessFailedException $e) {
65
            throw new MachineException($e->getMessage(), $e->getCode(), $e);
66
        }
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getIp()
73
    {
74
        try {
75
            $ip = $this->processRunner->run('docker-machine ip '.$this->name)->getOutput();
76
        } catch (ProcessFailedException $e) {
77
            throw new MachineException($e->getMessage(), $e->getCode(), $e);
78
        }
79
80
        $ip = trim($ip);
81
        if (strpos($ip, 'not running') !== false) {
82
            throw new MachineException($ip);
83
        }
84
85
        return $ip;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 View Code Duplication
    public function isCreated()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
92
    {
93
        $process = $this->processRunner->run('docker-machine status '.$this->name, false);
94
        if(!$process->isSuccessful() || strpos($process->getOutput(), 'Error') === 0) {
95
            return false;
96
        }
97
98
        return true;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 View Code Duplication
    public function create()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
105
    {
106
        try {
107
            $this->processRunner->run('docker-machine create --driver virtualbox '.$this->name);
108
        } catch (ProcessFailedException $e) {
109
            throw new MachineException($e->getMessage(), $e->getCode(), $e);
110
        }
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getEnvironmentDeclaration()
117
    {
118
        return sprintf('eval "$(docker-machine env %s)"', $this->name).PHP_EOL;
119
    }
120
}
121