DockerComposeTransport   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 12.77 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 12
loc 94
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 4 1
A wrap() 12 12 1
A addChdir() 0 5 1
A getTransport() 0 13 5
A getTransportOptions() 0 16 4
A getCommandToExecute() 0 4 1

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 Consolidation\SiteProcess\Transport;
4
5
use Consolidation\SiteProcess\SiteProcess;
6
use Consolidation\SiteAlias\SiteAliasInterface;
7
use Consolidation\SiteProcess\Util\Shell;
8
use Consolidation\Config\ConfigInterface;
9
10
/**
11
 * DockerComposeTransport knows how to wrap a command such that it executes
12
 * on a Docker Compose service.
13
 */
14
class DockerComposeTransport implements TransportInterface
15
{
16
    protected $tty;
17
    protected $siteAlias;
18
    protected $cd_remote;
19
20
    public function __construct(SiteAliasInterface $siteAlias)
21
    {
22
        $this->siteAlias = $siteAlias;
23
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function configure(SiteProcess $process)
29
    {
30
        $this->tty = $process->isTty();
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36 View Code Duplication
    public function wrap($args)
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...
37
    {
38
        $transport = $this->getTransport();
39
        $transportOptions = $this->getTransportOptions();
40
        $commandToExecute = $this->getCommandToExecute($args);
41
42
        return array_merge(
43
            $transport,
44
            $transportOptions,
45
            $commandToExecute
46
        );
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function addChdir($cd, $args)
53
    {
54
        $this->cd_remote = $cd;
55
        return $args;
56
    }
57
58
    /**
59
     * getTransport returns the transport along with the docker-compose
60
     * project in case it is defined.
61
     */
62
    protected function getTransport()
63
    {
64
        $transport = ['docker-compose'];
65
        $project = $this->siteAlias->get('docker.project', '');
66
        $options = $this->siteAlias->get('docker.compose.options', '');
67
        if ($project && (strpos($options, '-p') === false || strpos($options, '--project') === false)) {
68
            $transport = array_merge($transport, ['-p', $project]);
69
        }
70
        if ($options) {
71
            $transport[] = Shell::preEscaped($options);
72
        }
73
        return array_merge($transport, ['exec']);
74
    }
75
76
    /**
77
     * getTransportOptions returns the transport options for the tranport
78
     * mechanism itself
79
     */
80
    protected function getTransportOptions()
81
    {
82
        $transportOptions = [
83
            $this->siteAlias->get('docker.service', ''),
84
        ];
85
        if ($options = $this->siteAlias->get('docker.exec.options', '')) {
86
            array_unshift($transportOptions, Shell::preEscaped($options));
87
        }
88
        if (!$this->tty) {
89
            array_unshift($transportOptions, '-T');
90
        }
91
        if ($this->cd_remote) {
92
            $transportOptions = array_merge(['--workdir', $this->cd_remote], $transportOptions);
93
        }
94
        return array_filter($transportOptions);
95
    }
96
97
    /**
98
     * getCommandToExecute processes the arguments for the command to
99
     * be executed such that they are appropriate for the transport mechanism.
100
     *
101
     * Nothing to do for this transport.
102
     */
103
    protected function getCommandToExecute($args)
104
    {
105
        return $args;
106
    }
107
}
108