These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 | if ($project = $this->siteAlias->get('docker.project', '')) { |
||
66 | $transport = array_merge($transport, ['-p', $project]); |
||
67 | } |
||
68 | return array_merge($transport, ['exec']); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * getTransportOptions returns the transport options for the tranport |
||
73 | * mechanism itself |
||
74 | */ |
||
75 | protected function getTransportOptions() |
||
76 | { |
||
77 | $transportOptions = [ |
||
78 | $this->siteAlias->get('docker.service', ''), |
||
79 | ]; |
||
80 | if ($options = $this->siteAlias->get('docker.exec.options', '')) { |
||
81 | array_unshift($transportOptions, Shell::preEscaped($options)); |
||
82 | } |
||
83 | if (!$this->tty) { |
||
84 | array_unshift($transportOptions, '-T'); |
||
85 | } |
||
86 | if ($this->cd_remote) { |
||
87 | $transportOptions = array_merge(['--workdir', $this->cd_remote], $transportOptions); |
||
88 | } |
||
89 | return array_filter($transportOptions); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * getCommandToExecute processes the arguments for the command to |
||
94 | * be executed such that they are appropriate for the transport mechanism. |
||
95 | * |
||
96 | * Nothing to do for this transport. |
||
97 | */ |
||
98 | protected function getCommandToExecute($args) |
||
99 | { |
||
100 | return $args; |
||
101 | } |
||
102 | } |
||
103 |
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.