PrepareTask   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 50
ccs 25
cts 25
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 26 4
A getDescription() 0 3 1
A getName() 0 3 1
A getExcludes() 0 10 2
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 - Create temporal Tar
20
 *
21
 * @author Andrés Montañez <[email protected]>
22
 */
23
class PrepareTask extends AbstractTask
24
{
25 48
    public function getName(): string
26
    {
27 48
        return 'deploy/tar/prepare';
28
    }
29
30 18
    public function getDescription(): string
31
    {
32 18
        return '[Deploy] Preparing Tar file';
33
    }
34
35 18
    public function execute(): bool
36
    {
37 18
        if (!$this->runtime->getEnvOption('releases', false)) {
38 1
            throw new ErrorException('This task is only available with releases enabled', 40);
39
        }
40
41 17
        $tarLocal = $this->runtime->getTempFile();
42 17
        $this->runtime->setVar('tar_local', $tarLocal);
43
44 17
        $excludes = $this->getExcludes();
45 17
        $tarPath = $this->runtime->getEnvOption('tar_create_path', 'tar');
46 17
        $flags = $this->runtime->getEnvOption(
47
            'tar_create',
48 17
            $this->runtime->isWindows() ? '--force-local -c -z -p -f' : 'cfzp'
49
        );
50 17
        $from = $this->runtime->getEnvOption('from', './');
51
52 17
        if ($this->runtime->getEnvOption('copyDirectory', false)) {
53 1
            $from = sprintf('-C %s ./', $from);
54
        }
55
56 17
        $cmdTar = sprintf('%s %s %s %s %s', $tarPath, $flags, $tarLocal, $excludes, $from);
57
58
        /** @var Process $process */
59 17
        $process = $this->runtime->runLocalCommand($cmdTar, 300);
60 17
        return $process->isSuccessful();
61
    }
62
63 17
    protected function getExcludes(): string
64
    {
65 17
        $excludes = $this->runtime->getMergedOption('exclude', []);
66 17
        $excludes = array_merge(['.git'], array_filter($excludes));
67
68 17
        foreach ($excludes as &$exclude) {
69 17
            $exclude = '--exclude="' . $exclude . '"';
70
        }
71
72 17
        return implode(' ', $excludes);
73
    }
74
}
75