DatabaseMigrationLegacyYML   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 37
c 3
b 0
f 0
dl 0
loc 65
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A getTitle() 0 3 1
A hasCommitAndPush() 0 3 1
A runActualTask() 0 41 3
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Api\FileSystemFixes;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
7
8
/**
9
 * This task adds a legacy branch to the git repo of the original to act as a backup/legacy version for
10
 * holding a version of the module before it was changed
11
 */
12
class DatabaseMigrationLegacyYML extends Task
13
{
14
    protected $taskStep = 's50';
15
16
    public function getTitle()
17
    {
18
        return 'Copy legacy data to database migration file';
19
    }
20
21
    public function getDescription()
22
    {
23
        return '
24
            Take the data from .upgrade.yml and move it to _config/legay.yml with a header.';
25
    }
26
27
    /**
28
     * [runActualTask description]
29
     * @param  array  $params not currently used for this task
30
     */
31
    public function runActualTask($params = []): ?string
32
    {
33
        foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) {
0 ignored issues
show
Bug introduced by
It seems like getExistingModuleDirLocations() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

33
        foreach ($this->mu()->/** @scrutinizer ignore-call */ getExistingModuleDirLocations() as $moduleDir) {
Loading history...
34
            $oldFile = $moduleDir . '/.upgrade.yml';
35
            $newFile = $moduleDir . '/_config/database.legacy.yml';
36
            $tmpFile = $moduleDir . '/_config/database.legacy.yml.tmp';
37
            $mvStatement = $newFile . ' > ' . $tmpFile . ' && mv ' . $tmpFile . ' ' . $newFile;
38
            if (! file_exists($oldFile)) {
39
                return $oldFile . ' NOT FOUND!!!';
40
            }
41
            $dir = dirname($newFile);
42
            FileSystemFixes::inst($this->mu())
43
                ->mkDir($dir)
44
                ->copyFolderOrFile($oldFile, $newFile);
45
46
            $this->mu()->execMe(
0 ignored issues
show
Bug introduced by
It seems like execMe() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
            $this->mu()->/** @scrutinizer ignore-call */ execMe(
Loading history...
47
                $moduleDir,
48
                'sed \'1d\' ' . $mvStatement,
49
                'removing the first line and placing into temp file',
50
                false
51
            );
52
            $this->mu()->execMe(
53
                $moduleDir,
54
                "sed -i -e 's/^/  /' " . $newFile,
55
                'adding two additional spaces to the start of each line',
56
                false
57
            );
58
            $this->mu()->execMe(
59
                $moduleDir,
60
                'echo \'  classname_value_remapping:\' | cat - ' . $mvStatement,
61
                'adding `  classname_value_remapping:` to the start of ' . $newFile,
62
                false
63
            );
64
            $this->mu()->execMe(
65
                $moduleDir,
66
                'echo \'SilverStripe\ORM\DatabaseAdmin:\' | cat - ' . $mvStatement,
67
                'adding `SilverStripe\ORM\DatabaseAdmin:` to the start of ' . $newFile,
68
                false
69
            );
70
        }
71
        return null;
72
    }
73
74
    protected function hasCommitAndPush()
75
    {
76
        return true;
77
    }
78
}
79