CopyTask::getDescription()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Magallanes package.
5
 *
6
 * (c) Andrés Montañez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mage\Task\BuiltIn\Deploy\Tar;
13
14
use Mage\Task\Exception\ErrorException;
15
use Symfony\Component\Process\Process;
16
use Mage\Task\AbstractTask;
17
18
/**
19
 * Tar Task - Copy Tar
20
 *
21
 * @author Andrés Montañez <[email protected]>
22
 */
23
class CopyTask extends AbstractTask
24
{
25 48
    public function getName(): string
26
    {
27 48
        return 'deploy/tar/copy';
28
    }
29
30 17
    public function getDescription(): string
31
    {
32 17
        return '[Deploy] Copying files with Tar';
33
    }
34
35 17
    public function execute(): bool
36
    {
37 17
        if (!$this->runtime->getEnvOption('releases', false)) {
38 1
            throw new ErrorException('This task is only available with releases enabled', 40);
39
        }
40
41 16
        $user = $this->runtime->getEnvOption('user', $this->runtime->getCurrentUser());
42 16
        $host = $this->runtime->getHostName();
43 16
        $sshConfig = $sshConfig = $this->runtime->getSSHConfig();
0 ignored issues
show
Unused Code introduced by
The assignment to $sshConfig is dead and can be removed.
Loading history...
44 16
        $hostPath = rtrim($this->runtime->getEnvOption('host_path'), '/');
45 16
        $currentReleaseId = $this->runtime->getReleaseId();
46
47 16
        $tarPath = $this->runtime->getEnvOption('tar_extract_path', 'tar');
48 16
        $flags = $this->runtime->getEnvOption('tar_extract', 'xfzop');
49 16
        $targetDir = sprintf('%s/releases/%s', $hostPath, $currentReleaseId);
50
51 16
        $tarLocal = $this->runtime->getVar('tar_local');
52 16
        $tarRemote = basename($tarLocal);
0 ignored issues
show
Bug introduced by
It seems like $tarLocal can also be of type null; however, parameter $path of basename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $tarRemote = basename(/** @scrutinizer ignore-type */ $tarLocal);
Loading history...
53
54 16
        $cmdCopy = sprintf(
55
            'scp -P %d %s %s %s@%s:%s/%s',
56 16
            $sshConfig['port'],
57 16
            isset($sshConfig['scp_flags']) ? $sshConfig['scp_flags'] : $sshConfig['flags'],
58
            $tarLocal,
59
            $user,
60
            $host,
61
            $targetDir,
62
            $tarRemote
63
        );
64
65
        /** @var Process $process */
66 16
        $process = $this->runtime->runLocalCommand($cmdCopy, intval($sshConfig['timeout']));
67 16
        if ($process->isSuccessful()) {
68 14
            $cmdUnTar = sprintf('cd %s && %s %s %s', $targetDir, $tarPath, $flags, $tarRemote);
69 14
            $process = $this->runtime->runRemoteCommand($cmdUnTar, false, 600);
70 14
            if ($process->isSuccessful()) {
71 13
                $cmdDelete = sprintf('rm %s/%s', $targetDir, $tarRemote);
72 13
                $process = $this->runtime->runRemoteCommand($cmdDelete, false);
73 13
                return $process->isSuccessful();
74
            }
75
        }
76
77 3
        return false;
78
    }
79
}
80