Completed
Pull Request — master (#192)
by
unknown
01:36
created

SetSupportedAddonsTask::run()   C

Complexity

Conditions 10
Paths 9

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 5.1578
c 0
b 0
f 0
cc 10
eloc 27
nc 9
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
            } else {
69
                $this->output($addon . ' did not match a known addon');
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param string $output
76
     */
77
    protected function output($output)
78
    {
79
        echo $output . PHP_EOL;
80
    }
81
82
    protected function outputHelp()
83
    {
84
        $className = __CLASS__;
85
86
        $this->output(<<<HELP
87
{$this->description}
88
89
Usage: framework/sake dev/tasks/$className [-fr] addons=foo/bar,bin/baz
90
91
 -r Replace existing supported addons with the new given list
92
 -f Used with -r and an empty set of addons to drop support for all addons
93
 
94
HELP
95
        );
96
    }
97
98
    /**
99
     * @param array $args
100
     * @return array
101
     */
102
    protected function processArgs(array $args)
103
    {
104
        $outputArgs = [];
105
106
        foreach ($args as $arg) {
107
            $outputArgs = array_merge($outputArgs, str_split(substr($arg, 1)));
108
        }
109
110
        return $outputArgs;
111
    }
112
}
113