Completed
Push — master ( a9b2d8...9165ca )
by Marius
03:47
created

setupPhpUnitRunConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 1

Importance

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