Passed
Push — 4.9 ( 8f0f3a...f6adb4 )
by Mikhail
01:57
created

Addon::removeAutocomplete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 27
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
3
namespace controllers;
4
5
use generators\AddonXml\AddonXmlGenerator;
6
use generators\Language\LanguageGenerator;
7
use generators\Readme\ReadmeGenerator;
8
use generators\MultipleFileGenerator;
9
use terminal\Terminal;
10
use filesystem\Filesystem;
11
use mediators\GeneratorMediator;
12
use \Config;
13
14
class Addon extends AbstractController
15
{
16
    private $config;
17
    private $terminal;
18
    private $filesystem;
19
    private $mfGenerator;
20
    private static $allowedMethods = [
21
        'help',
22
        'create',
23
        'remove'
24
    ];
25
26
    use HelpTrait;
27
28
    function __construct(
29
        Config              $config,
30
        Terminal            $terminal,
31
        Filesystem          $filesystem
32
    )
33
    {
34
        $this->config               = $config;
35
        $this->terminal             = $terminal;
36
        $this->filesystem           = $filesystem;
37
38
        $addonXmlGenerator  = new AddonXmlGenerator($this->config);
39
        $languageGenerator  = new LanguageGenerator($this->config);
40
        $readmeGenerator    = new ReadmeGenerator($this->config);
41
42
        $generatorMediator  = new GeneratorMediator();
43
44
        $generatorMediator
45
            ->addGenerator($addonXmlGenerator)
46
            ->addGenerator($languageGenerator)
47
            ->addGenerator($readmeGenerator);
48
49
        $this->mfGenerator = new MultipleFileGenerator($this->filesystem);
50
        $this->mfGenerator
51
            ->addGenerator($addonXmlGenerator)
52
            ->addGenerator($languageGenerator)
53
            ->addGenerator($readmeGenerator);
54
    }
55
    
56
    /**
57
     * @inheritdoc
58
     */
59
    public static function getAllowedMethods(): array
60
    {
61
        return self::$allowedMethods;
62
    }
63
64
    /**
65
     * help:
66
     * addon create
67
     * creates addon.xml, language file, readme and other
68
     * @throws Exception if file already exists
69
     */
70
    public function create()
71
    {
72
        $this->mfGenerator
73
            ->throwIfExists('File already exists. Remove it first if you want to replace it.')
74
            ->find(AddonXmlGenerator::class)
75
                ->extract()
0 ignored issues
show
Bug introduced by
The method extract() does not exist on generators\AbstractFileGenerator. Since it exists in all sub-types, consider adding an abstract or default implementation to generators\AbstractFileGenerator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
                ->/** @scrutinizer ignore-call */ extract()
Loading history...
76
                    ->create();
77
78
        $this->mfGenerator
79
            ->excluding(ReadmeGenerator::class)
80
                ->write()
81
                ->throwIfNotExists('File cannot be created.');
82
83
        $this->mfGenerator
84
            ->find(ReadmeGenerator::class)
85
                ->readFromTemplate()
86
                ->write();
87
88
        $self = $this;
89
        $this->mfGenerator->each(function($generator) use ($self) {
90
            $self->terminal->success($generator->extract()->getPath() . ' was created');
91
            $self->terminal->diff(
92
                \Diff::toString(\Diff::compare('', $generator->extract()->toString()))
93
            );
94
        });
95
    }
96
    
97
    /**
98
     * help:
99
     * addon remove
100
     * removes entire path of the addon
101
     */
102
    public function remove()
103
    {
104
        $self = $this;
105
        $this->terminal->confirm(
106
            function() use ($self) {
107
                $addonPath = $self->config->get('path')
108
                    . $self->config->get('filesystem.output_path_relative');
109
110
                $self->filesystem->delete($addonPath);
111
112
                if ($self->filesystem->exists($addonPath)) {
113
                    throw new \Exception($addonPath . ' cannot be removed');
114
                }
115
116
                $this->terminal->warning('Addon was removed');
117
            }
118
        );        
119
    }
120
121
    /**
122
     * Autocomplete addon param
123
     */
124
    public function createAutocomplete($prev = null, $cur = null, $arguments = [])
0 ignored issues
show
Unused Code introduced by
The parameter $cur is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

124
    public function createAutocomplete($prev = null, /** @scrutinizer ignore-unused */ $cur = null, $arguments = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $prev is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

124
    public function createAutocomplete(/** @scrutinizer ignore-unused */ $prev = null, $cur = null, $arguments = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
125
    {
126
        if (empty($arguments['addon.id'])) {
127
            return ['--addon.id'];
128
        }
129
130
        return [];
131
    }
132
    /**
133
     * Autocomplete addon name to be removed
134
     */
135
    public function removeAutocomplete($prev = null, $cur = null, $arguments = [])
0 ignored issues
show
Unused Code introduced by
The parameter $cur is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

135
    public function removeAutocomplete($prev = null, /** @scrutinizer ignore-unused */ $cur = null, $arguments = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
136
    {
137
        switch ($prev)
138
        {
139
            case '--addon.id':
140
           
141
                $addonsPath = sanitize_filename(
142
                    $this->config->get('path')
143
                    . $this->config->get('filesystem.output_path_relative') . '../'
144
                );
145
146
                $dirs = $this->filesystem->listDirs($addonsPath);
147
        
148
                return array_map(function($dir) {
149
                    $paths = explode('/', $dir);
150
                    return end($paths);
151
                }, $dirs);
152
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
153
            default:
154
            /**
155
             * @todo do it better
156
             */
157
                if (empty($arguments['addon.id'])) {
158
                    return ['--addon.id'];
159
                }
160
161
                return [];
162
        }
163
    }
164
}
165