Completed
Pull Request — master (#19)
by Moshe
02:10
created

DockerComposeTransport::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Consolidation\SiteProcess\Transport;
4
5
use Drush\Drush;
6
use Psr\Log\LoggerInterface;
7
use Robo\Common\IO;
8
use Symfony\Component\Console\Style\OutputStyle;
9
use Symfony\Component\Process\Process;
10
use Consolidation\SiteProcess\Util\RealtimeOutputHandler;
11
use Consolidation\SiteProcess\Util\Escape;
12
use Symfony\Component\Console\Output\ConsoleOutputInterface;
13
use Consolidation\SiteAlias\AliasRecord;
14
use Consolidation\SiteProcess\Util\Shell;
15
16
/**
17
 * DockerComposeTransport knows how to wrap a command such that it executes on a Docker Compose service.
18
 */
19
class DockerComposeTransport implements TransportInterface
20
{
21
    protected $tty;
22
    protected $siteAlias;
23
    protected $cd;
24
25
    public function __construct(AliasRecord $siteAlias)
26
    {
27
        $this->siteAlias = $siteAlias;
28
    }
29
30
    /**
31
     * inheritdoc
32
     */
33
    public function configure(Process $process)
34
    {
35
        $this->tty = $process->isTty();
36
    }
37
38
    /**
39
     * inheritdoc
40
     */
41 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...
42
    {
43
        $transport = ['docker-compose', 'exec'];
44
        $transportOptions = $this->getTransportOptions();
45
        $commandToExecute = $this->getCommandToExecute($args);
46
47
        return array_merge(
48
            $transport,
49
            $transportOptions,
50
            $commandToExecute
51
        );
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function addChdir($cd, $args)
58
    {
59
        $this->cd = $cd;
60
    }
61
62
    /**
63
     * getTransportOptions returns the transport options for the tranport
64
     * mechanism itself
65
     */
66
    protected function getTransportOptions()
67
    {
68
        $transportOptions[] = $this->siteAlias->get('docker.service');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$transportOptions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $transportOptions = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
69
        if ($options = $this->siteAlias->get('docker.exec.options')) {
70
            array_unshift($transportOptions, Shell::preEscaped($options));
71
        }
72
        if (!$this->tty) {
73
            array_unshift($transportOptions, '-T');
74
        }
75
        if ($this->cd) {
76
            array_unshift($transportOptions, ['--workdir', $this->cd]);
77
        }
78
        return array_filter($transportOptions);
79
    }
80
81
    /**
82
     * getCommandToExecute processes the arguments for the command to
83
     * be executed such that they are appropriate for the transport mechanism.
84
     *
85
     * Nothing to do for this transport.
86
     */
87
    protected function getCommandToExecute($args)
88
    {
89
        return $args;
90
    }
91
}
92