Completed
Pull Request — master (#192)
by
unknown
19:56
created

SetSupportedAddonsTask::processArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * Provides a CLI for bulk setting supported addons
5
 *
6
 * @package mysite
7
 */
8
class SetSupportedAddonsTask extends BuildTask
9
{
10
    /**
11
     * {@inheritDoc}
12
     * @var string
13
     */
14
    protected $title = 'Set Supported Addons';
15
16
    /**
17
     * {@inheritDoc}
18
     * @var string
19
     */
20
    protected $description = 'Bulk update addons that should be registered as supported by SilverStripe';
21
22
    /**
23
     * {@inheritDoc}
24
     * @param SS_HTTPRequest $request
25
     */
26
    public function run($request)
27
    {
28
        $vars = $request->getVars();
29
30
        // Check for args
31
        $args = $this->processArgs($request->getVar('args') ?: []);
32
        $isReplace = in_array('r', $args);
33
        $isForce = in_array('f', $args);
34
35
        unset($vars['url']);
36
        if (empty($vars) && !$isReplace) {
37
            $this->outputHelp();
38
            return;
39
        }
40
41
        $addons = $request->getVar('addons');
42
43
        if ($isReplace) {
44
            // Catch a mistake of replacing addons but providing no addons to replace it with
45
            if (empty($addons) && !$isForce) {
46
                $this->output('Providing no addons when using -r will unsupport all addons. Use -f to force.');
47
                return;
48
            }
49
50
            // Mark all addons as unsupported
51
            $this->output('All addons have been marked unsupported');
52
            SQLUpdate::create('Addon', ['Supported' => false])->execute();
53
54
            // If there are no addons to replace with, say as such and return
55
            if (empty($addons)) {
56
                return;
57
            }
58
        }
59
60
        $addons = explode(',', $addons);
61
        $addonList = Addon::get()->filter('Name', $addons);
62
63
        foreach ($addons as $addon) {
64
            if ($addonObject = $addonList->find('Name', $addon)) {
65
                $addonObject->Supported = true;
66
                $addonObject->write();
67
                $this->output($addon . ' marked as supported');
68
            }
69
            else {
70
                $this->output($addon . ' did not match a known addon');
71
            }
72
        }
73
    }
74
75
    /**
76
     * @param string $output
77
     */
78
    protected function output($output)
79
    {
80
        echo $output . PHP_EOL;
81
    }
82
83
    protected function outputHelp()
84
    {
85
        $className = __CLASS__;
86
87
        $this->output(<<<HELP
88
{$this->description}
89
90
Usage: framework/sake dev/tasks/$className [-fr] addons=foo/bar,bin/baz
91
92
 -r Replace existing supported addons with the new given list
93
 -f Used with -r and an empty set of addons to drop support for all addons
94
 
95
HELP
96
        );
97
    }
98
99
    /**
100
     * @param array $args
101
     * @return array
102
     */
103
    protected function processArgs(array $args)
104
    {
105
        $outputArgs = [];
106
107
        foreach ($args as $arg) {
108
            $outputArgs = array_merge($outputArgs, str_split(substr($arg, 1)));
109
        }
110
111
        return $outputArgs;
112
    }
113
}
114