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

AddonXml::getAllowedMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 generators\FileGenerator;
10
use terminal\Terminal;
11
use filesystem\Filesystem;
12
use mediators\GeneratorMediator;
13
use \Config;
14
15
class AddonXml extends AbstractController
16
{
17
    private $config;
18
    private $terminal;
19
    private $filesystem;
20
    private $mfGenerator;
21
    private static $allowedMethods = [
22
        'help',
23
        'create',
24
        'remove',
25
        'update'
26
    ];
27
28
    use HelpTrait;
29
30
    function __construct(
31
        Config              $config,
32
        Terminal            $terminal,
33
        Filesystem          $filesystem
34
    )
35
    {
36
        $this->config               = $config;
37
        $this->terminal             = $terminal;
38
        $this->filesystem           = $filesystem;
39
40
        $addonXmlGenerator      = new AddonXmlGenerator($this->config);
41
        $languageGenerator      = new LanguageGenerator($this->config);
42
        $generatorMediator      = new GeneratorMediator();
43
44
        $generatorMediator
45
            ->addGenerator($addonXmlGenerator)
46
            ->addGenerator($languageGenerator);
47
48
        $this->mfGenerator = new MultipleFileGenerator($this->filesystem);
49
        $this->mfGenerator
50
            ->addGenerator($addonXmlGenerator)
51
            ->addGenerator($languageGenerator);
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public static function getAllowedMethods(): array
58
    {
59
        return self::$allowedMethods;
60
    }
61
62
    /**
63
     * help:
64
     * addon-xml create
65
     * creates addonXml structure and write it to file
66
     * @throws Exception if file already exists
67
     */
68
    public function create()
69
    {
70
        $addonXmlFileGenerator = $this->mfGenerator
71
            ->find(AddonXmlGenerator::class);
72
        
73
        $addonXmlGenerator = $addonXmlFileGenerator
74
                ->throwIfExists('Such addon.xml already exists. Remove it first if you want to replace it.')
75
                ->extract();
76
                
77
        $addonXmlGenerator
78
            ->create();
79
80
        $addonXmlFileGenerator    
81
            ->write()
82
            ->throwIfNotExists($addonXmlGenerator->getPath() . ' cannot be created.');
83
84
        /**
85
         * results
86
         */
87
        $this->terminal->success($addonXmlGenerator->getPath() . ' was created');
88
        $this->terminal->diff(
89
            \Diff::toString(\Diff::compare('', $addonXmlGenerator->toString()))
90
        );
91
    }
92
93
    /**
94
     * help:
95
     * addon-xml remove
96
     * removes file addon.xml
97
     * @throws Exception if file doesn't exists
98
     */
99
    public function remove()
100
    {
101
        $addonXmlGenerator = $this->mfGenerator
102
            ->find(AddonXmlGenerator::class)
103
                ->read()
104
                ->remove()
105
                ->throwIfExists('File cannot be removed.')
106
                ->extract();
107
108
        $this->terminal->success($addonXmlGenerator->getPath() . ' was removed');
109
        $this->terminal->diff(
110
            \Diff::toString(\Diff::compare($addonXmlGenerator->toString(), ''))
111
        );
112
    }
113
114
    /**
115
     * help:
116
     * addon-xml update addon.id=<addon_id> <item> [remove] [...args]
117
     * Sets additional field to addon xml file
118
     * addon.id - id of the addon
119
     * ---
120
     *      settings-item (si) - <item id="date">...</item>
121
     *           args: section=<section_id> type=<type> id=<id> [dv=<default_value>] [v=<variants>]
122
     *              section         - id for the settings section
123
     *              type            - type of the item id: input, textarea, password, checkbox, selectbox, multiple select, multiple checkboxes, countries list, states list, file, info, header, template
124
     *              id              - id of the setting item
125
     *              default_value   - (df) - default value for setting item
126
     *              variants        - (v) - list of item value variants comma separated
127
     * ---
128
     * 
129
     * see more @link [https://www.cs-cart.ru/docs/4.9.x/developer_guide/addons/scheme/scheme3.0_structure.html]
130
     * @throws Exception if file doesn't exists
131
     */
132
    public function update()
133
    {
134
        $this->mfGenerator
135
            ->throwIfNotExists('Some addon file not found.')
136
            ->read();
137
138
        $old_content = [];
139
        $this->mfGenerator->each(function($generator) use (&$old_content) {
140
            $old_content[get_class($generator->extract())] = $generator->extract()->toString();
141
        });
142
        
143
        $addonXmlGenerator = $this->mfGenerator
144
            ->find(AddonXmlGenerator::class)
145
                ->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

145
                ->/** @scrutinizer ignore-call */ extract();
Loading history...
146
147
        switch (true)
148
        {
149
            case $this->config->get('settings-item'):
150
            case $this->config->get('si'):
151
                if (true === $this->config->get('remove')) {
152
                    $addonXmlGenerator->removeSetting(
153
                        $this->config->get('id')
154
                    );
155
                } else {
156
                    $addonXmlGenerator->setSetting(
157
                        $this->config->get('section'),
158
                        $this->config->get('type'),
159
                        $this->config->get('id'),
160
                        $this->config->getOr('default_value', 'dv') ?: '',
161
                        (function($config) {
162
                            $variants = $config->getOr('variants', 'v');
163
164
                            return $variants ? explode(',', $variants) : [];
165
                        })($this->config)
166
                    );
167
                }
168
            break;
169
            default:
170
                throw new \BadMethodCallException('There is no such command');
171
        }
172
173
        $this->mfGenerator
174
            ->write()
175
            ->throwIfNotExists();
176
        
177
        $this->mfGenerator->each(function($generator) use ($old_content) {
178
            $this->terminal->diff(
179
                \Diff::toString(\Diff::compare($old_content[get_class($generator->extract())], $generator->extract()->toString()))
180
            );
181
        });
182
    }
183
}
184