Completed
Push — master ( b6a150...3c3902 )
by Gennady
02:57
created

PhpstormProjectConfigure::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
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\FilesystemAwareTrait;
17
use Gennadyx\Skeleton\Exception\RuntimeException;
18
use Gennadyx\Skeleton\VarAwareInterface;
19
use Gennadyx\Skeleton\VarAwareTrait;
20
use Symfony\Component\Serializer\Encoder\XmlEncoder;
21
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
22
23
/**
24
 * Class PhpstormProjectConfigure
25
 *
26
 * @author Gennady Knyazkin <[email protected]>
27
 */
28
final class PhpstormProjectConfigure implements ActionInterface, VarAwareInterface
29
{
30
    use VarAwareTrait,
31
        FilesystemAwareTrait;
32
33
    const IDEA_PATH             = '.idea';
34
    const COMPOSER_PROJECT_PATH = 'composer';
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 2
    public function execute()
40
    {
41 2
        if (!$this->canExecute()) {
42 1
            return;
43
        }
44
45 1
        $this->removeComposerPluginDir();
46 1
        $this->markSourceDirectories();
47 1
    }
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 \Symfony\Component\Filesystem\Exception\IOException
60
     */
61 1
    private function removeComposerPluginDir()
62
    {
63 1
        $directory = $this->vars['root'].'/composer';
64
65 1
        if (is_dir($directory)) {
66 1
            $this->fs->remove($directory);
67
        }
68 1
    }
69
70
    /**
71
     * @return void
72
     * @throws RuntimeException
73
     */
74 1
    private function markSourceDirectories()
75
    {
76 1
        $encoder = new XmlEncoder('module');
77
78
        try {
79 1
            $data = $encoder->decode(file_get_contents($this->getProjectFile()), 'xml');
80
81 1
            if (isset($data['component']['content']['#'])) {
82 1
                unset($data['component']['content']['#']);
83
            }
84
85 1
            $data['component']['content']['sourceFolder'] = [
86
                [
87 1
                    '@url'           => 'file://$MODULE_DIR$/src',
88 1
                    '@isTestSource'  => 'false',
89 1
                    '@packagePrefix' => $this->getNamespace(),
90 1
                    '#'              => '',
91
                ],
92
                [
93 1
                    '@url'           => 'file://$MODULE_DIR$/tests',
94 1
                    '@isTestSource'  => 'true',
95 1
                    '@packagePrefix' => $this->getTestsNamespace(),
96 1
                    '#'              => '',
97
                ],
98
            ];
99
100 1
            file_put_contents($this->getProjectFile(), $encoder->encode($data, 'xml'));
101
        } catch (UnexpectedValueException $e) {
102
            throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
103
        }
104 1
    }
105
106
    /**
107
     * @return string
108
     */
109 2
    private function getIdeaDirectory(): string
110
    {
111 2
        return $this->vars['root'].'/'.self::IDEA_PATH;
112
    }
113
114
    /**
115
     * @return string
116
     */
117 1
    private function getProjectFile(): string
118
    {
119 1
        return $this->getIdeaConfigFile($this->vars['name']);
120
    }
121
122
    /**
123
     * @param string $name Config name
124
     *
125
     * @return string
126
     */
127 1
    private function getIdeaConfigFile(string $name): string
128
    {
129 1
        return $this->getIdeaDirectory().'/'.$name.'.iml';
130
    }
131
132
    /**
133
     * @return string
134
     */
135 1
    private function getNamespace(): string
136
    {
137 1
        return $this->fixNamespaceVariable('namespace');
138
    }
139
140
    /**
141
     * @param string $variable
142
     *
143
     * @return string
144
     */
145 1
    private function fixNamespaceVariable(string $variable): string
146
    {
147 1
        return str_replace('\\\\', '\\', $this->vars[$variable]);
148
    }
149
150
    /**
151
     * @return string
152
     */
153 1
    private function getTestsNamespace(): string
154
    {
155 1
        return $this->fixNamespaceVariable('tests_namespace');
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 4
    public function getPriority(): int
162
    {
163 4
        return PHP_INT_MAX;
164
    }
165
}
166