Passed
Branch 4.9 (cb955a)
by Mikhail
01:30
created

AddonXml   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 75
dl 0
loc 151
c 0
b 0
f 0
rs 10

4 Methods

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

131
                ->/** @scrutinizer ignore-call */ extract();
Loading history...
132
133
        switch (true)
134
        {
135
            case $this->config->get('settings-item'):
136
            case $this->config->get('si'):
137
                if (true === $this->config->get('remove')) {
138
                    $addonXmlGenerator->removeSetting(
139
                        $this->config->get('id')
140
                    );
141
                } else {
142
                    $addonXmlGenerator->setSetting(
143
                        $this->config->get('section'),
144
                        $this->config->get('type'),
145
                        $this->config->get('id'),
146
                        $this->config->getOr('default_value', 'dv') ?: '',
147
                        (function($config) {
148
                            $variants = $config->getOr('variants', 'v');
149
150
                            return $variants ? explode(',', $variants) : [];
151
                        })($this->config)
152
                    );
153
                }
154
            break;
155
            default:
156
                throw new \BadMethodCallException('There is no such command');
157
        }
158
159
        $this->mfGenerator
160
            ->write()
161
            ->throwIfNotExists();
162
        
163
        $this->mfGenerator->each(function($generator) use ($old_content) {
164
            $this->terminal->diff(
165
                \Diff::toString(\Diff::compare($old_content[get_class($generator->extract())], $generator->extract()->toString()))
166
            );
167
        });
168
    }
169
}
170