Test Failed
Pull Request — master (#3)
by MediaCT
06:30
created

FileTemplatesPatcher   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 72
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
B patchWorkspaceConfig() 0 25 2
A patch() 0 11 1
A __construct() 0 3 1
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\CodingStandard\PhpStorm\Patcher;
8
9
use Mediact\CodingStandard\PhpStorm\EnvironmentInterface;
10
use Mediact\CodingStandard\PhpStorm\FilesystemInterface;
11
use Mediact\CodingStandard\PhpStorm\XmlAccessorInterface;
12
13
class FileTemplatesPatcher implements ConfigPatcherInterface
14
{
15
    use CopyFilesTrait;
16
17
    /**
18
     * @var XmlAccessorInterface
19
     */
20
    private $xmlAccessor;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param XmlAccessorInterface $xmlAccessor
26
     */
27
    public function __construct(XmlAccessorInterface $xmlAccessor)
28
    {
29
        $this->xmlAccessor = $xmlAccessor;
30
    }
31
32
    /**
33
     * Patch the config.
34
     *
35
     * @param EnvironmentInterface $environment
36
     *
37
     * @return void
38
     */
39
    public function patch(
40
        EnvironmentInterface $environment
41
    ) {
42
        $this->copyDirectory(
43
            $environment->getDefaultsFilesystem(),
44
            $environment->getIdeConfigFilesystem(),
45
            'fileTemplates'
46
        );
47
48
        $this->patchWorkspaceConfig(
49
            $environment->getIdeConfigFilesystem()
50
        );
51
    }
52
53
    /**
54
     * Patch the workspace config.
55
     *
56
     * @param FilesystemInterface $ideConfigFs
57
     *
58
     * @return void
59
     */
60
    private function patchWorkspaceConfig(
61
        FilesystemInterface $ideConfigFs
62
    ) {
63
        if (!$ideConfigFs->has('workspace.xml')) {
64
            return;
65
        }
66
67
        $xml = simplexml_load_string(
68
            $ideConfigFs->read('workspace.xml')
69
        );
70
71
        $node = $this->xmlAccessor->getDescendant(
72
            $xml,
73
            [
74
                ['component', ['name' => 'FileTemplateManagerImpl']],
75
                ['option', ['name' => 'SCHEME']]
76
            ]
77
        );
78
79
        $this->xmlAccessor->setAttributes(
80
            $node,
81
            ['value' => 'Project']
82
        );
83
84
        $ideConfigFs->put('workspace.xml', $xml->asXML());
85
    }
86
}
87