AddDotEnvFile   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
eloc 33
c 2
b 1
f 0
dl 0
loc 60
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 3 1
A getDescription() 0 3 1
A runActualTask() 0 19 3
A getEnvtContentAddValues() 0 6 1
A hasCommitAndPush() 0 3 1
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Api\FileFixes;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
7
8
/**
9
 * Run a dev/build as a smoke test to see if all is well
10
 */
11
class AddDotEnvFile extends Task
12
{
13
    protected $taskStep = 's60';
14
15
    protected $envContent = [
16
        'SS_DATABASE_CLASS="MySQLDatabase"',
17
        'SS_DATABASE_NAME="--DB-NAME--HERE--"',
18
        'SS_DATABASE_PASSWORD="x"',
19
        'SS_DATABASE_SERVER="localhost"',
20
        'SS_DATABASE_USERNAME="root"',
21
        '',
22
        'SS_DEFAULT_ADMIN_PASSWORD="x"',
23
        'SS_DEFAULT_ADMIN_USERNAME="x"',
24
        '',
25
        'SS_ENVIRONMENT_TYPE="dev"',
26
    ];
27
28
    public function getTitle()
29
    {
30
        return 'Adds a .env file';
31
    }
32
33
    public function getDescription()
34
    {
35
        return 'Adds a basic .env file in case that is needed.';
36
    }
37
38
    public function runActualTask($params = []): ?string
39
    {
40
        if (! file_exists($this->mu()->getWebRootDirLocation() . '/.env')) {
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

40
        if (! file_exists($this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation() . '/.env')) {
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

40
        if (! file_exists($this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation() . '/.env')) {
Loading history...
41
            foreach ($this->envContent as $line) {
42
                $line = $this->getEnvtContentAddValues($line);
43
                $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

43
                $this->mu()->/** @scrutinizer ignore-call */ execMe(
Loading history...
44
                    $this->mu()->getWebRootDirLocation(),
0 ignored issues
show
Bug introduced by
It seems like $this->mu()->getWebRootDirLocation() can also be of type Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader and Sunnysideup\UpgradeToSilverstripe4\Traits\Creator and null; however, parameter $newDir of Sunnysideup\UpgradeToSil...oduleUpgrader::execMe() 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

44
                    /** @scrutinizer ignore-type */ $this->mu()->getWebRootDirLocation(),
Loading history...
45
                    'echo \'' . \addcslashes($line, '\'') . '\' >> .env',
46
                    'adding a line to .env: ' . $line,
47
                    false
48
                );
49
            }
50
            FileFixes::inst($this->mu())
51
                ->addLineToFileIfItDoesNotExist(
52
                    '.gitignore',
53
                    '.env'
54
                );
55
        }
56
        return null;
57
    }
58
59
    protected function getEnvtContentAddValues(string $string)
60
    {
61
        return str_replace(
62
            '--DB-NAME--HERE--',
63
            'upgrader' . $this->mu()->getVendorNamespace() . $this->mu()->getPackageNamespace(),
0 ignored issues
show
Bug introduced by
It seems like getVendorNamespace() 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

63
            'upgrader' . $this->mu()->/** @scrutinizer ignore-call */ getVendorNamespace() . $this->mu()->getPackageNamespace(),
Loading history...
Bug introduced by
The method getVendorNamespace() 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

63
            'upgrader' . $this->mu()->/** @scrutinizer ignore-call */ getVendorNamespace() . $this->mu()->getPackageNamespace(),
Loading history...
Bug introduced by
The method getPackageNamespace() 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

63
            'upgrader' . $this->mu()->getVendorNamespace() . $this->mu()->/** @scrutinizer ignore-call */ getPackageNamespace(),
Loading history...
Bug introduced by
It seems like getPackageNamespace() 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

63
            'upgrader' . $this->mu()->getVendorNamespace() . $this->mu()->/** @scrutinizer ignore-call */ getPackageNamespace(),
Loading history...
64
            $string
65
        );
66
    }
67
68
    protected function hasCommitAndPush()
69
    {
70
        return false;
71
    }
72
}
73