TemplateSettingsPatcher::patch()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * Copyright MediaCT. All rights reserved.
5
 * https://www.mediact.nl
6
 */
7
8
declare(strict_types=1);
9
10
namespace Mediact\CodingStandard\PhpStorm\Patcher;
11
12
use Mediact\CodingStandard\PhpStorm\EnvironmentInterface;
13
use Mediact\CodingStandard\PhpStorm\FilesystemInterface;
14
use Mediact\CodingStandard\PhpStorm\XmlAccessorInterface;
15
16
class TemplateSettingsPatcher implements ConfigPatcherInterface
17
{
18
    use CopyFilesTrait;
19
20
    /**
21
     * @var XmlAccessorInterface
22
     */
23
    private $xmlAccessor;
24
25
    /**
26
     * @var array
27
     */
28
    private $includePaths = [];
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param XmlAccessorInterface $xmlAccessor
34
     */
35
    public function __construct(XmlAccessorInterface $xmlAccessor)
36
    {
37
        $this->xmlAccessor  = $xmlAccessor;
38
        $this->includePaths = [
39
            'M2-PHP-File-Header.php',
40
            'M2-Settings.php',
41
            'M2-XML-File-Header.xml'
42
        ];
43
    }
44
45
    /**
46
     * Patch the config.
47
     *
48
     * @param EnvironmentInterface $environment
49
     *
50
     * @return void
51
     */
52
    public function patch(
53
        EnvironmentInterface $environment
54
    ): void {
55
        $this->patchFileTemplateSettings(
56
            $environment
57
        );
58
        $this->patchIncludes(
59
            $environment
60
        );
61
    }
62
63
    /**
64
     * Patch file template settings if exists otherwise create one.
65
     *
66
     * @param EnvironmentInterface $environment
67
     *
68
     * @return void
69
     */
70
    public function patchFileTemplateSettings(
71
        EnvironmentInterface $environment
72
    ): void {
73
        if (!$environment->getIdeConfigFilesystem()->has('file.template.settings.xml')) {
74
            $this->copyFile(
75
                $environment->getDefaultsFilesystem(),
76
                $environment->getIdeConfigFilesystem(),
77
                'file.template.settings.xml'
78
            );
79
        } else {
80
            $xml = simplexml_load_string(
81
                $environment->getIdeConfigFilesystem()->read('file.template.settings.xml')
82
            );
83
84
            foreach ($this->getFileTemplates() as $xmlTag => $fileTemplateNames) {
85
                foreach ($fileTemplateNames as $fileTemplateName) {
86
                    $node = $this->xmlAccessor->getDescendant(
87
                        $xml,
88
                        [
89
                            [
90
                                'component',
91
                                ['name' => 'ExportableFileTemplateSettings']
92
                            ],
93
                            [$xmlTag],
94
                            ['template', ['name' => $fileTemplateName]]
95
                        ]
96
                    );
97
                    $this->xmlAccessor->setAttributes(
98
                        $node,
99
                        [
100
                            'reformat' => 'false',
101
                            'live-template-enabled' => 'true'
102
                        ]
103
                    );
104
                    $environment->getIdeConfigFilesystem()->put('file.template.settings.xml', $xml->asXML());
0 ignored issues
show
Bug introduced by
It seems like $xml->asXML() can also be of type true; however, parameter $contents of Mediact\CodingStandard\P...esystemInterface::put() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

104
                    $environment->getIdeConfigFilesystem()->put('file.template.settings.xml', /** @scrutinizer ignore-type */ $xml->asXML());
Loading history...
105
                }
106
            }
107
        }
108
    }
109
110
    /**
111
     * Add copyright files to project if they do not exist
112
     *
113
     * @param EnvironmentInterface $environment
114
     *
115
     * @return void
116
     */
117
    public function patchIncludes(EnvironmentInterface $environment): void
118
    {
119
        foreach ($this->includePaths as $fileName) {
120
            if (!$environment->getIdeConfigFilesystem()->has("fileTemplates/includes/$fileName")) {
121
                $environment->getIdeConfigFilesystem()->put(
122
                    "fileTemplates/includes/$fileName",
123
                    $environment->getDefaultsFilesystem()->read("includes/$fileName")
124
                );
125
            }
126
        }
127
    }
128
129
    /**
130
     * Enable file templates
131
     *
132
     * @return array
133
     */
134
    public function getFileTemplates(): array
135
    {
136
        return [
137
            'default_templates' => [
138
                'M2-Acl XML.xml',
139
                'M2-Class.php',
140
                'M2-Class-Backend-Controller',
141
                'M2-Class-Block.php',
142
                'M2-Class-Helper.php',
143
                'M2-Class-Observer.php',
144
                'M2-Class-ViewModel.php',
145
                'M2-Config-XML.xml',
146
                'M2-Db-schema-XML.xml',
147
                'M2-DI.xml',
148
                'M2-Events.xml',
149
                'M2-Extension-Attributes-XML.xml',
150
                'M2-Layout-XML.xml',
151
                'M2-Menu.xml',
152
                'M2-Module-XML.xml',
153
                'M2-Registration.php',
154
                'M2-Routes.xml',
155
                'M2-Sales-XML.xml',
156
                'M2-System-include-XML.xml',
157
                'M2-System-XML.xml'
158
            ],
159
            'includes_templates' => [
160
                'M2-PHP-File-Header.php',
161
                'M2-Settings.php',
162
                'M2-XML-File-Header.xml',
163
            ]
164
        ];
165
    }
166
}
167