Passed
Branch master (2f7647)
by Nicolaas
04:56 queued 03:20
created

CheckThatFoldersAreReady::runActualTask()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 14
c 2
b 1
f 0
dl 0
loc 27
rs 9.2222
cc 6
nc 6
nop 1
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
6
7
/**
8
 * Checks that all the directories needed to run this tool exist and are writable.
9
 */
10
class CheckThatFoldersAreReady extends Task
11
{
12
    protected $taskStep = 's10';
13
14
    public function getTitle()
15
    {
16
        return 'Check Folders Are Ready';
17
    }
18
19
    public function getDescription()
20
    {
21
        return '
22
            Checks that all the directories needed to run this tool exist and are writable.
23
            ';
24
    }
25
26
    /**
27
     * Checks that the required folder variables link to folders that file_exists
28
     * and which are writable.
29
     */
30
    public function runActualTask($params = [])
31
    {
32
        $abovewebdir = $this->mu()->getAboveWebRootDirLocation();
0 ignored issues
show
Bug introduced by
The method getAboveWebRootDirLocation() 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

32
        $abovewebdir = $this->mu()->/** @scrutinizer ignore-call */ getAboveWebRootDirLocation();
Loading history...
33
        //check dir above web dir exists
34
        if (! file_exists($abovewebdir)) {
35
            $this->mu()->colourPrint('Above web dir does not exists: ' . $abovewebdir, 'red');
36
            return 'No point in running tool with directory not ready';
37
        }
38
        //Directory exists, now check if writable.
39
        if (! is_writable($abovewebdir)) {
40
            //Not writable send warning
41
            $this->mu()->colourPrint('Above web dir is not writable: ' . $abovewebdir, 'red');
42
            return 'No point in running tool with directory not ready';
43
        }
44
        //It has been found and is writable; Success!
45
        $this->mu()->colourPrint('Found and checked above web dir ✔', 'green');
46
47
        //LogFileLocation
48
        $logDir = $this->mu()->getLogFolderDirLocation();
0 ignored issues
show
Bug introduced by
The method getLogFolderDirLocation() 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

48
        $logDir = $this->mu()->/** @scrutinizer ignore-call */ getLogFolderDirLocation();
Loading history...
49
        if ($logDir) {
50
            //check that log dir is exists
51
            if (! file_exists($logDir)) {
52
                return 'Log dir not exists: ' . $logDir . ' set your log dir to an empty string if you prefer to continue without a log.';
53
            }
54
            //Directory exists, now check if writable.
55
            if (! is_writable($logDir)) {
56
                return $logDir . ' is not writable' . '. Set the log dir to an empty string or provide a writable directory. ';
57
            }
58
            //all ok
59
        }
60
    }
61
62
    protected function hasCommitAndPush()
63
    {
64
        return false;
65
    }
66
}
67