AddVendorExposeDataToComposer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 37
c 1
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 runActualTask() 0 30 6
A hasCommitAndPush() 0 3 1
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\ComposerJsonFixes;
6
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
7
8
/**
9
 * Updates the composer requirements to reflect the new version and package names
10
 * in the composer file of your module
11
 */
12
class AddVendorExposeDataToComposer extends Task
13
{
14
    protected $taskStep = 's50';
15
16
    protected $toExpose = [
17
        'javascript',
18
        'images',
19
        'img',
20
        'css',
21
        'fonts',
22
        'js',
23
        'client/javascript',
24
        'client/images',
25
        'client/img',
26
        'client/css',
27
        'client/fonts',
28
        'client/js',
29
    ];
30
31
    public function getTitle()
32
    {
33
        return 'Adds vendor expose data to composer';
34
    }
35
36
    public function getDescription()
37
    {
38
        return '
39
            By default we expose all the client related files (images, css and javascript)';
40
    }
41
42
    public function runActualTask($params = []): ?string
43
    {
44
        $expose = [];
45
        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

45
        foreach ($this->mu()->/** @scrutinizer ignore-call */ getExistingModuleDirLocations() as $moduleDir) {
Loading history...
46
            foreach ($this->toExpose as $folder) {
47
                if (file_exists($moduleDir . '/' . $folder)) {
48
                    if ($this->mu()->getIsModuleUpgrade()) {
0 ignored issues
show
Bug introduced by
It seems like getIsModuleUpgrade() 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

48
                    if ($this->mu()->/** @scrutinizer ignore-call */ getIsModuleUpgrade()) {
Loading history...
Bug introduced by
The method getIsModuleUpgrade() 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
                    if ($this->mu()->/** @scrutinizer ignore-call */ getIsModuleUpgrade()) {
Loading history...
49
                        //expose "javascript"
50
                        $expose[] = $folder;
51
                    } else {
52
                        //expose "app/javascript"
53
                        $expose[] = basename($moduleDir) . '/' . $folder;
54
                    }
55
                }
56
            }
57
        }
58
        if (count($expose)) {
59
            $command =
60
            'if(!isset($data["extra"]["expose"])) { '
61
                . '    $data["extra"]["expose"] = ["' . implode('", "', $expose) . '"]; '
62
                . '}';
63
            ComposerJsonFixes::inst($this->mu())->UpdateJSONViaCommandLine(
64
                $this->mu()->getGitRootDir(),
0 ignored issues
show
Bug introduced by
It seems like getGitRootDir() 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

64
                $this->mu()->/** @scrutinizer ignore-call */ getGitRootDir(),
Loading history...
65
                $command,
66
                'exposing: ' . implode(', ', $expose)
67
            );
68
69
            $this->setCommitMessage('API:  exposing folders' . implode(',', $expose));
70
        }
71
        return null;
72
    }
73
74
    protected function hasCommitAndPush()
75
    {
76
        return true;
77
    }
78
}
79