Test Failed
Push — master ( 857c27...82742e )
by Marius
01:45
created

ensureWorkspaceFileExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.9765

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 3
cts 8
cp 0.375
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.9765
1
<?php
2
declare(strict_types=1);
3
4
namespace Paysera\PhpStormHelper\Service;
5
6
use RuntimeException;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
class WorkspaceConfigurationHelper
10
{
11
    private $domHelper;
12
    private $filesystem;
13
14 18
    public function __construct(DomHelper $domHelper, Filesystem $filesystem)
15
    {
16 18
        $this->domHelper = $domHelper;
17 18
        $this->filesystem = $filesystem;
18 18
    }
19
20 11
    public function setupServerMappings(string $pathToWorkspaceXml, array $serverMappings)
21
    {
22 11
        foreach ($serverMappings as $serverMapping) {
23 11
            $parts = explode('@', $serverMapping);
24 11
            if (count($parts) !== 2) {
25
                throw new RuntimeException(sprintf('Invalid server mapping format: %s', $serverMapping));
26
            }
27
28 11
            $this->addServer($pathToWorkspaceXml, $parts[0], $parts[1]);
29
        }
30 11
    }
31
32 11
    private function addServer(string $pathToWorkspaceXml, string $hostWithPort, string $remoteRoot)
33
    {
34 11
        if (strpos($hostWithPort, ':') === false) {
35 8
            $host = $hostWithPort;
36 8
            $name = $host;
37 8
            $port = null;
38
        } else {
39 3
            $parts = explode(':', $hostWithPort, 2);
40 3
            $host = $parts[0];
41 3
            $port = $parts[1];
42 3
            $name = $port === '80' ? $host : $hostWithPort;
43
        }
44
45 11
        $this->ensureWorkspaceFileExists($pathToWorkspaceXml);
46 11
        $document = $this->domHelper->loadDocument($pathToWorkspaceXml);
47
48 11
        $projectElement = $this->domHelper->findNode($document, 'project');
49 11
        $serversComponent = $this->domHelper->findOrCreateChildNode(
50 11
            $projectElement,
51 11
            'component',
52 11
            ['name' => 'PhpServers']
53
        );
54
55 11
        $internalNode = $this->domHelper->findOrCreateChildNode($serversComponent, 'servers');
56
57 11
        $serverNode = $this->domHelper->findOptionalNode($internalNode, 'server', ['host' => $host, 'port' => $port]);
58 11
        if ($serverNode === null) {
59 8
            $serverNode = $document->createElement('server');
60 8
            $serverNode->setAttribute('id', $this->generateId($name));
61 8
            $internalNode->appendChild($serverNode);
62
        }
63
64 11
        $serverNode->setAttribute('host', $host);
65 11
        $serverNode->setAttribute('name', $name);
66 11
        if ($port === null) {
67 8
            $serverNode->removeAttribute('port');
68
        } else {
69 3
            $serverNode->setAttribute('port', $port);
70
        }
71 11
        $serverNode->setAttribute('use_path_mappings', 'true');
72
73 11
        $pathMappings = $this->domHelper->findOrCreateChildNode($serverNode, 'path_mappings');
74
75 11
        $this->domHelper->removeNodes($pathMappings, 'mapping');
76 11
        $mapping = $this->domHelper->findOrCreateChildNode($pathMappings, 'mapping');
77 11
        $mapping->setAttribute('local-root', '$PROJECT_DIR$');
78 11
        $mapping->setAttribute('remote-root', $remoteRoot);
79
80 11
        $this->domHelper->saveDocument($pathToWorkspaceXml, $document);
81 11
    }
82
83 3
    public function configureComposer(string $pathToWorkspaceXml, string $composerExecutable = 'composer')
84
    {
85 3
        $this->ensureWorkspaceFileExists($pathToWorkspaceXml);
86 3
        $document = $this->domHelper->loadDocument($pathToWorkspaceXml);
87
88 3
        $projectElement = $this->domHelper->findNode($document, 'project');
89
90 3
        $component = $this->domHelper->findOrCreateChildNode(
91 3
            $projectElement,
92 3
            'component',
93 3
            ['name' => 'ComposerSettings']
94
        );
95
96 3
        $component->setAttribute('doNotAsk', 'true');
97 3
        $component->setAttribute('synchronizationState', 'SYNCHRONIZE');
98 3
        $component->setAttribute('updatePackages', 'false');
99
100 3
        $pharConfigPath = $this->domHelper->findOrCreateChildNode($component, 'pharConfigPath');
101 3
        $pharConfigPath->nodeValue = '$PROJECT_DIR$/composer.json';
102
103 3
        $executablePath = $this->domHelper->findOrCreateChildNode($component, 'executablePath');
104 3
        $executablePath->nodeValue = $composerExecutable;
105
106 3
        $this->domHelper->saveDocument($pathToWorkspaceXml, $document);
107 3
    }
108
109 3
    public function configureFileTemplateScheme(string $pathToWorkspaceXml)
110
    {
111 3
        $this->ensureWorkspaceFileExists($pathToWorkspaceXml);
112 3
        $document = $this->domHelper->loadDocument($pathToWorkspaceXml);
113
114 3
        $projectElement = $this->domHelper->findNode($document, 'project');
115
116 3
        $component = $this->domHelper->findOrCreateChildNode(
117 3
            $projectElement,
118 3
            'component',
119 3
            ['name' => 'FileTemplateManagerImpl']
120
        );
121 3
        $option = $this->domHelper->findOrCreateChildNode(
122 3
            $component,
123 3
            'option',
124 3
            ['name' => 'SCHEME']
125
        );
126 3
        $option->setAttribute('value', 'Project');
127
128 3
        $this->domHelper->saveDocument($pathToWorkspaceXml, $document);
129 3
    }
130
131 1
    public function setupPhpUnitRunConfiguration(string $pathToWorkspaceXml)
132
    {
133 1
        $this->ensureWorkspaceFileExists($pathToWorkspaceXml);
134 1
        $document = $this->domHelper->loadDocument($pathToWorkspaceXml);
135
136 1
        $projectElement = $this->domHelper->findNode($document, 'project');
137
138 1
        $component = $this->domHelper->findOrCreateChildNode(
139 1
            $projectElement,
140 1
            'component',
141 1
            ['name' => 'RunManager']
142
        );
143 1
        $configuration = $this->domHelper->findOrCreateChildNode(
144 1
            $component,
145 1
            'configuration',
146 1
            ['name' => 'PHPUnit', 'type' => 'PHPUnitRunConfigurationType', 'factoryName' => 'PHPUnit']
147
        );
148
149 1
        $testRunnerNode = $this->domHelper->findOrCreateChildNode(
150 1
            $configuration,
151 1
            'TestRunner'
152
        );
153 1
        $testRunnerNode->setAttribute('scope', 'XML');
154
155 1
        $methodNode = $this->domHelper->findOrCreateChildNode(
156 1
            $configuration,
157 1
            'method'
158
        );
159 1
        $methodNode->setAttribute('v', '2');
160
161 1
        $this->domHelper->saveDocument($pathToWorkspaceXml, $document);
162 1
    }
163
164 8
    private function generateId(string $name)
165
    {
166 8
        $hash = sha1($name);
167 8
        return substr($hash, 0, 8) . '-'
168 8
            . substr($hash, 8, 4) . '-'
169 8
            . substr($hash, 12, 4) . '-'
170 8
            . substr($hash, 16, 4) . '-'
171 8
            . substr($hash, 20, 12)
172
        ;
173
    }
174
175 18
    private function ensureWorkspaceFileExists(string $pathToWorkspaceXml)
176
    {
177 18
        if (file_exists($pathToWorkspaceXml)) {
178 18
            return;
179
        }
180
181
        $emptyWorkspaceFile = <<<'FILE'
182
<?xml version="1.0" encoding="UTF-8"?>
183
<project version="4"/>
184
FILE;
185
186
        $directory = dirname($pathToWorkspaceXml);
187
        $this->filesystem->mkdir($directory);
188
        $this->filesystem->dumpFile($pathToWorkspaceXml, $emptyWorkspaceFile);
189
    }
190
}
191