ExternalToolsConfigurationHelper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Paysera\PhpStormHelper\Service;
6
7
use Paysera\PhpStormHelper\Entity\ExternalToolConfiguration;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class ExternalToolsConfigurationHelper
11
{
12
    private $directoryResolver;
13
    private $filesystem;
14
    private $domHelper;
15
16 12
    public function __construct(DirectoryResolver $directoryResolver, Filesystem $filesystem, DomHelper $domHelper)
17
    {
18 12
        $this->directoryResolver = $directoryResolver;
19 12
        $this->filesystem = $filesystem;
20 12
        $this->domHelper = $domHelper;
21 12
    }
22
23
    /**
24
     * @param ExternalToolConfiguration[] $externalToolConfigurations
25
     */
26 6
    public function configureExternalTools(array $externalToolConfigurations)
27
    {
28 6
        $directory = $this->directoryResolver->getConfigurationDirectory();
29 6
        $toolsDirectory = $directory . '/tools';
30 6
        $externalToolsXmlPath = $toolsDirectory . '/External Tools.xml';
31 6
        if (!$this->filesystem->exists($toolsDirectory)) {
32 3
            $this->filesystem->mkdir($toolsDirectory);
33
        }
34
35 6
        $document = $this->domHelper->loadOrCreateDocument($externalToolsXmlPath);
36
37 6
        $toolSet = $this->domHelper->findOrCreateChildNode($document, 'toolSet', ['name' => 'External Tools']);
38
39 6
        foreach ($externalToolConfigurations as $configuration) {
40 5
            $tool = $this->domHelper->findOrCreateChildNode($toolSet, 'tool', [
41 5
                'name' => $configuration->getName(),
42
            ]);
43
44 5
            $attributes = array_map(function (bool $value) {
45 5
                return $value ? 'true' : 'false';
46 5
            }, [
47 5
                'showInMainMenu' => $configuration->isVisibleInMainMenu(),
48 5
                'showInEditor' => $configuration->isVisibleInEditor(),
49 5
                'showInProject' => $configuration->isVisibleInProject(),
50 5
                'showInSearchPopup' => $configuration->isVisibleInSearchPopup(),
51
                'disabled' => false,
52 5
                'useConsole' => $configuration->isConsoleShownAlways(),
53 5
                'showConsoleOnStdOut' => $configuration->isConsoleShownOnStdOut(),
54 5
                'showConsoleOnStdErr' => $configuration->isConsoleShownOnStdErr(),
55 5
                'synchronizeAfterRun' => $configuration->isSynchronizationRequired(),
56
            ]);
57
58 5
            $this->domHelper->applyAttributesToElement($tool, $attributes);
59
60 5
            $exec = $this->domHelper->findOrCreateChildNode($tool, 'exec');
61
62 5
            $commandOption = $this->domHelper->findOrCreateChildNode($exec, 'option', [
63 5
                'name' => 'COMMAND',
64
            ]);
65 5
            $commandOption->setAttribute('value', $configuration->getCommand());
66
67 5
            $parametersOption = $this->domHelper->findOrCreateChildNode($exec, 'option', [
68 5
                'name' => 'PARAMETERS',
69
            ]);
70 5
            $parametersOption->setAttribute('value', $configuration->getParameters());
71
72 5
            $workingDirectoryOption = $this->domHelper->findOrCreateChildNode($exec, 'option', [
73 5
                'name' => 'WORKING_DIRECTORY',
74
            ]);
75 5
            $workingDirectoryOption->setAttribute(
76 5
                'value',
77 5
                $configuration->getWorkingDirectory() ?? '$ProjectFileDir$'
78
            );
79
        }
80
81 6
        $this->domHelper->saveDocument($externalToolsXmlPath, $document);
82 6
    }
83
}
84