Passed
Push — master ( 5fa504...e8027e )
by Nicolaas
02:03
created

MoveMysiteToApp::normalisedDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
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 MoveMysiteToApp extends Task
13
{
14
    protected $taskStep = 's30';
15
16
    public function getTitle()
17
    {
18
        return 'Move mysite to app folder for projects';
19
    }
20
21
    public function getDescription()
22
    {
23
        return '
24
            Move the mysite folder to the app folder to match Silverstripe best practice.';
25
    }
26
27
    /**
28
     * [runActualTask description]
29
     * @param  array  $params not currently used for this task
30
     */
31
    public function runActualTask($params = [])
32
    {
33
        if ($this->mu()->getIsProjectUpgrade()) {
0 ignored issues
show
Bug introduced by
It seems like getIsProjectUpgrade() 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
        if ($this->mu()->/** @scrutinizer ignore-call */ getIsProjectUpgrade()) {
Loading history...
34
            $rootDir = $this->mu()->getWebRootDirLocation();
0 ignored issues
show
Bug introduced by
The method getWebRootDirLocation() does not exist on Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

34
            $rootDir = $this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation();
Loading history...
Bug introduced by
It seems like getWebRootDirLocation() 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

34
            $rootDir = $this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation();
Loading history...
35
            $old = '/mysite/';
36
            $new = '/app/';
37
            $oldPath = $this->normalisedDir($rootDir . $old);
38
            if (file_exists($oldPath)) {
39
                $newPath = $this->normalisedDir($rootDir . $new);
40
                if (! file_exists($newPath)) {
41
                    $fixer = FileSystemFixes::inst($this->mu());
42
                    $fixer->moveFolderOrFile($oldPath, $newPath);
43
                } else {
44
                    $this->mu()->colourPrint($newPath . ' already exists', 'red');
0 ignored issues
show
Bug introduced by
It seems like colourPrint() 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

44
                    $this->mu()->/** @scrutinizer ignore-call */ colourPrint($newPath . ' already exists', 'red');
Loading history...
45
                }
46
            } else {
47
                $this->mu()->colourPrint(
48
                    'Can not find: ' . $rootDir . '/' . $old,
49
                    'red'
50
                );
51
            }
52
        }
53
    }
54
55
    protected function hasCommitAndPush()
56
    {
57
        return true;
58
    }
59
60
    protected function normalisedDir(string $path): string
61
    {
62
        $normalizedPath = str_replace(['//', '/', '\\\\', '\\'], DIRECTORY_SEPARATOR, $path);
63
        return realpath($normalizedPath);
64
    }
65
}
66