PhpstormProjectConfigure   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 123
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A canExecute() 0 4 2
A execute() 0 6 2
B markSourceDirectories() 0 31 3
A getIdeaDirectory() 0 4 1
A getProjectFile() 0 4 1
A getIdeaConfigFile() 0 4 1
A getNamespace() 0 4 1
A fixNamespaceVariable() 0 4 1
A getTestsNamespace() 0 4 1
A getPriority() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of the skeleton package.
7
 *
8
 * (c) Gennady Knyazkin <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Gennadyx\Skeleton\Action;
15
16
use Gennadyx\Skeleton\Action\Traits\EventAwareTrait;
17
use Gennadyx\Skeleton\Action\Traits\FilesystemAwareTrait;
18
use Gennadyx\Skeleton\Action\Traits\VarAwareTrait;
19
use Gennadyx\Skeleton\EventAwareInterface;
20
use Gennadyx\Skeleton\Exception\RuntimeException;
21
use Gennadyx\Skeleton\VarAwareInterface;
22
use Symfony\Component\Serializer\Encoder\XmlEncoder;
23
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
24
25
/**
26
 * Class PhpstormProjectConfigure
27
 *
28
 * @author Gennady Knyazkin <[email protected]>
29
 */
30
final class PhpstormProjectConfigure implements ActionInterface, VarAwareInterface, EventAwareInterface
31
{
32
    use VarAwareTrait,
33
        FilesystemAwareTrait,
34
        EventAwareTrait;
35
36
    const IDEA_PATH             = '.idea';
37
    const COMPOSER_PROJECT_PATH = 'composer';
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 2
    public function execute()
43
    {
44 2
        if ($this->canExecute()) {
45 1
            $this->markSourceDirectories();
46
        }
47 2
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 2
    public function canExecute(): bool
53
    {
54 2
        return is_dir($this->getIdeaDirectory()) && file_exists($this->getProjectFile());
55
    }
56
57
    /**
58
     * @return void
59
     * @throws RuntimeException
60
     */
61 1
    private function markSourceDirectories()
62
    {
63 1
        $encoder = new XmlEncoder('module');
64
65
        try {
66 1
            $data = $encoder->decode(file_get_contents($this->getProjectFile()), 'xml');
67
68 1
            if (isset($data['component']['content']['#'])) {
69 1
                unset($data['component']['content']['#']);
70
            }
71
72 1
            $data['component']['content']['sourceFolder'] = [
73
                [
74 1
                    '@url'           => 'file://$MODULE_DIR$/src',
75 1
                    '@isTestSource'  => 'false',
76 1
                    '@packagePrefix' => $this->getNamespace(),
77 1
                    '#'              => '',
78
                ],
79
                [
80 1
                    '@url'           => 'file://$MODULE_DIR$/tests',
81 1
                    '@isTestSource'  => 'true',
82 1
                    '@packagePrefix' => $this->getTestsNamespace(),
83 1
                    '#'              => '',
84
                ],
85
            ];
86
87 1
            file_put_contents($this->getProjectFile(), $encoder->encode($data, 'xml'));
88
        } catch (UnexpectedValueException $e) {
89
            throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
90
        }
91 1
    }
92
93
    /**
94
     * @return string
95
     */
96 2
    private function getIdeaDirectory(): string
97
    {
98 2
        return str_replace('/'.static::COMPOSER_PROJECT_PATH, '', $this->vars['root']).'/'.self::IDEA_PATH;
99
    }
100
101
    /**
102
     * @return string
103
     */
104 1
    private function getProjectFile(): string
105
    {
106 1
        return $this->getIdeaConfigFile($this->vars['name']);
107
    }
108
109
    /**
110
     * @param string $name Config name
111
     *
112
     * @return string
113
     */
114 1
    private function getIdeaConfigFile(string $name): string
115
    {
116 1
        return $this->getIdeaDirectory().'/'.$name.'.iml';
117
    }
118
119
    /**
120
     * @return string
121
     */
122 1
    private function getNamespace(): string
123
    {
124 1
        return $this->fixNamespaceVariable('namespace');
125
    }
126
127
    /**
128
     * @param string $variable
129
     *
130
     * @return string
131
     */
132 1
    private function fixNamespaceVariable(string $variable): string
133
    {
134 1
        return str_replace('\\\\', '\\', $this->vars[$variable]);
135
    }
136
137
    /**
138
     * @return string
139
     */
140 1
    private function getTestsNamespace(): string
141
    {
142 1
        return $this->fixNamespaceVariable('tests_namespace');
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 4
    public function getPriority(): int
149
    {
150 4
        return PHP_INT_MAX - 1;
151
    }
152
}
153