Passed
Push — 4.9 ( 140402...8b65ee )
by Mikhail
01:39
created

AddonXml::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
c 0
b 0
f 0
rs 9.9666
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\MultipleFileGenerator;
8
use terminal\Terminal;
9
use filesystem\Filesystem;
10
use autocomplete\Autocomplete;
11
use mediators\GeneratorMediator;
12
use \Config;
13
14
class AddonXml extends AbstractController
15
{
16
    private $mfGenerator;
17
    private static $allowedMethods = [
18
        'help',
19
        'update'
20
    ];
21
22
    use HelpTrait;
23
24
    function __construct(
25
        Config              $config,
26
        Terminal            $terminal,
27
        Filesystem          $filesystem,
28
        Autocomplete        $autocomplete
29
    )
30
    {
31
        $this->config               = $config;
32
        $this->terminal             = $terminal;
33
        $this->filesystem           = $filesystem;
34
        $this->autocomplete         = $autocomplete;
35
36
        $addonXmlGenerator      = new AddonXmlGenerator($this->config);
37
        $languageGenerator      = new LanguageGenerator($this->config);
38
        $generatorMediator      = new GeneratorMediator();
39
40
        $generatorMediator
41
            ->addGenerator($addonXmlGenerator)
42
            ->addGenerator($languageGenerator);
43
44
        $this->mfGenerator = new MultipleFileGenerator($this->filesystem);
45
        $this->mfGenerator
46
            ->addGenerator($addonXmlGenerator)
47
            ->addGenerator($languageGenerator);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public static function getAllowedMethods(): array
54
    {
55
        return self::$allowedMethods;
56
    }
57
58
    /**
59
     * help:
60
     * addon-xml/update --addon.id <addon_id> --set <item> [...args]
61
     * Sets additional field to addon xml file
62
     * addon.id - id of the addon
63
     * ---
64
     *      --set
65
     *          settings-item - <item id="date">...</item>
66
     *              args: --section <section_id> --type <type> --id <id> [--default_value <default_value>] [--variants "<variant1,variant2>"]
67
     *                  section         - id for the settings section
68
     *                  type            - type of the item id: input, textarea, password, checkbox, selectbox, multiple select, multiple checkboxes, countries list, states list, file, info, header, template
69
     *                  id              - id of the setting item
70
     *                  default_value   - default value for setting item
71
     *                  variants        - list of item value variants comma separated and quote wrapped
72
     * ---
73
     * 
74
     * see more @link [https://www.cs-cart.ru/docs/4.9.x/developer_guide/addons/scheme/scheme3.0_structure.html]
75
     * @throws Exception if file doesn't exists
76
     */
77
    public function update()
78
    {
79
        $this->mfGenerator
80
            ->throwIfNotExists('Some addon file not found.')
81
            ->read();
82
83
        $old_content = [];
84
        $this->mfGenerator->each(function($generator) use (&$old_content) {
85
            $old_content[get_class($generator->extract())] = $generator->extract()->toString();
86
        });
87
        
88
        $addonXmlGenerator = $this->mfGenerator
89
            ->find(AddonXmlGenerator::class)
90
                ->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

90
                ->/** @scrutinizer ignore-call */ extract();
Loading history...
91
92
        $method     = $this->getMethodName();
93
        $class_name = get_class($this);
94
        if (method_exists($class_name, $method)) {
95
            $this->{$method}($addonXmlGenerator);
96
        } else {
97
            throw new \BadMethodCallException('There is no such command');
98
        }
99
100
        $this->mfGenerator
101
            ->write()
102
            ->throwIfNotExists();
103
        
104
        $this->mfGenerator->each(function($generator) use ($old_content) {
105
            $this->terminal->diff(
106
                \Diff::toString(\Diff::compare($old_content[get_class($generator->extract())], $generator->extract()->toString()))
107
            );
108
        });
109
    }
110
111
    /**
112
     * 
113
     */
114
    public function setSettingsItem($addonXmlGenerator)
115
    {
116
        $addonXmlGenerator->setSetting(
117
            $this->config->get('section'),
118
            $this->config->get('type'),
119
            $this->config->get('id'),
120
            $this->config->get('default_value') ?: '',
121
            (function($config) {
122
                $variants = $config->get('variants');
123
124
                return $variants ? explode(',', $variants) : [];
125
            })($this->config)
126
        );
127
    }
128
129
    public function removeSettingsItem($addonXmlGenerator)
130
    {
131
        $addonXmlGenerator->removeSetting(
132
            $this->config->get('id')
133
        );
134
    }
135
136
    public function updateAutocomplete()
137
    {
138
        $autocomplete   = $this->autocomplete;
139
        $arguments      = $this->terminal->getArguments();
140
        
141
        if (!empty($arguments['set']) && $arguments['set'] === 'settings-item') {
142
            return $this->setSettingsItemAutocomplete();
143
        }
144
145
        return $this->autocomplete->combineQueueParam(
146
            $this->autocomplete->queueArgument('addon.id', function() use ($autocomplete) {
147
                return $autocomplete->getAddonsList();
148
            }),
149
            $this->autocomplete->queueArgument('set', ['settings-item'])
150
        );
151
    }
152
153
    public function setSettingsItemAutocomplete()
154
    {
155
        $generator      = $this->mfGenerator
156
            ->find(AddonXmlGenerator::class)
157
                ->extract();
158
159
        return $this->autocomplete->combineQueueParam(
160
            $this->autocomplete->queueArgument('type', function() use ($generator) {
161
                return $generator->getVariants('item');
162
            }),
163
            $this->autocomplete->queueArgument('section'),
164
            $this->autocomplete->queueArgument('id')
165
        );
166
    }
167
}
168