GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#77)
by joseph
15:21
created

PathHelper::createSubDirectoriesAndGetPath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.1576

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
ccs 7
cts 12
cp 0.5833
cc 4
nc 4
nop 2
crap 5.1576
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FileCreationTransaction;
7
use EdmondsCommerce\DoctrineStaticMeta\Config;
8
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
9
use Symfony\Component\Filesystem\Filesystem;
10
11
class PathHelper
12
{
13
    /**
14
     * @var Filesystem
15
     */
16
    protected $filesystem;
17
    /**
18
     * @var FileCreationTransaction
19
     */
20
    protected $fileCreationTransaction;
21
22 138
    public function __construct(Filesystem $filesystem, FileCreationTransaction $fileCreationTransaction)
23
    {
24 138
        $this->filesystem              = $filesystem;
25 138
        $this->fileCreationTransaction = $fileCreationTransaction;
26 138
    }
27
28
    /**
29
     * @return string
30
     * @throws DoctrineStaticMetaException
31
     * @SuppressWarnings(PHPMD.StaticAccess)
32
     */
33
    public function getProjectRootDirectory(): string
34
    {
35
        return Config::getProjectRootDirectory();
36
    }
37
38
    /**
39
     * @param string $path
40
     */
41 37
    public function ensurePathExists(string $path): void
42
    {
43 37
        if ($this->filesystem->exists($path)) {
44 24
            return;
45
        }
46 37
        $this->filesystem->mkdir($path);
47 37
    }
48
49
    /**
50
     * @param string $pathToProjectRoot
51
     * @param string $templatePath
52
     * @param string $destinationFileName
53
     * @param array  $subDirectories
54
     *
55
     * @return string
56
     * @throws DoctrineStaticMetaException
57
     */
58 135
    public function copyTemplateAndGetPath(
59
        string $pathToProjectRoot,
60
        string $templatePath,
61
        string $destinationFileName,
62
        array $subDirectories
63
    ): string {
64
        try {
65 135
            $path = $this->createSubDirectoriesAndGetPath($pathToProjectRoot, $subDirectories);
66 135
            if (false === \ts\stringContains($destinationFileName, '.php')) {
67 135
                $destinationFileName = "$destinationFileName.php";
68
            }
69 135
            $filePath = "$path/$destinationFileName";
70 135
            $this->filesystem->copy($templatePath, $filePath, true);
71 135
            $this->fileCreationTransaction::setPathCreated($filePath);
72
73 135
            return $filePath;
74
        } catch (\Exception $e) {
75
            throw new DoctrineStaticMetaException(
76
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
77
                $e->getCode(),
78
                $e
79
            );
80
        }
81
    }
82
83
    /**
84
     * @param string $pathToProjectRoot
85
     * @param array  $subDirectories
86
     *
87
     * @return string
88
     * @throws DoctrineStaticMetaException
89
     */
90 135
    public function createSubDirectoriesAndGetPath(string $pathToProjectRoot, array $subDirectories): string
91
    {
92 135
        if (!$this->filesystem->exists($pathToProjectRoot)) {
93
            throw new DoctrineStaticMetaException("path to project root $pathToProjectRoot does not exist");
94
        }
95 135
        foreach ($subDirectories as $sd) {
96 135
            $pathToProjectRoot .= "/$sd";
97
            try {
98 135
                $this->filesystem->mkdir($pathToProjectRoot);
99
            } catch (\Exception $e) {
100
                throw new DoctrineStaticMetaException(
101
                    'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
102
                    $e->getCode(),
103 135
                    $e
104
                );
105
            }
106
        }
107
108 135
        return \realpath($pathToProjectRoot);
109
    }
110
111
    /**
112
     * @param string $pathToProjectRoot
113
     * @param string $templatePath
114
     * @param string $destPath
115
     *
116
     * @return string
117
     * @throws DoctrineStaticMetaException
118
     */
119 28
    public function copyTemplateDirectoryAndGetPath(
120
        string $pathToProjectRoot,
121
        string $templatePath,
122
        string $destPath
123
    ): string {
124 28
        $realTemplatePath = realpath($templatePath);
125 28
        if (false === $realTemplatePath) {
126
            throw new DoctrineStaticMetaException('path ' . $templatePath . ' does not exist');
127
        }
128
129 28
        $relativeDestPath = $this->filesystem->makePathRelative($destPath, $pathToProjectRoot);
130 28
        $subDirectories   = explode('/', $relativeDestPath);
131 28
        $path             = $this->createSubDirectoriesAndGetPath($pathToProjectRoot, $subDirectories);
132 28
        $this->filesystem->mirror($realTemplatePath, $path);
133 28
        $this->fileCreationTransaction::setPathCreated($path);
134
135 28
        return $path;
136
    }
137
138
    /**
139
     * @param string $path
140
     * @param string $singular
141
     * @param string $plural
142
     *
143
     * @return string
144
     * @throws DoctrineStaticMetaException
145
     */
146 28
    public function renamePathBasenameSingularOrPlural(
147
        string $path,
148
        string $singular,
149
        string $plural
150
    ): string {
151 28
        $find     = AbstractGenerator::FIND_ENTITY_NAME;
152 28
        $replace  = $singular;
153 28
        $basename = \basename($path);
154 28
        if (\ts\stringContains($basename, AbstractGenerator::FIND_ENTITY_NAME_PLURAL)) {
155 28
            $find    = AbstractGenerator::FIND_ENTITY_NAME_PLURAL;
156 28
            $replace = $plural;
157
        }
158
159 28
        return $this->renamePathBasename($find, $replace, $path);
160
    }
161
162
    /**
163
     * Move the basename of a path to the find/replaced version
164
     *
165
     * Then return the updated path
166
     *
167
     * @param string $find
168
     * @param string $replace
169
     * @param string $path
170
     *
171
     * @return string
172
     * @throws DoctrineStaticMetaException
173
     */
174 28
    public function renamePathBasename(string $find, string $replace, string $path): string
175
    {
176 28
        $basename    = basename($path);
177 28
        $newBasename = str_replace($find, $replace, $basename);
178 28
        $moveTo      = \dirname($path) . '/' . $newBasename;
179 28
        if ($moveTo === $path) {
180 28
            return $path;
181
        }
182 28
        if (is_dir($moveTo) || file_exists($moveTo)) {
183
            throw new DoctrineStaticMetaException(
184
                "Error trying to move:\n[$path]\n to \n[$moveTo]\ndestination already exists"
185
            );
186
        }
187
        try {
188 28
            $this->filesystem->rename($path, $moveTo);
189
        } catch (\Exception $e) {
190
            throw new DoctrineStaticMetaException(
191
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
192
                $e->getCode(),
193
                $e
194
            );
195
        }
196
197 28
        return $moveTo;
198
    }
199
200 24
    public function getPathFromNameAndSubDirs(string $pathToProjectRoot, string $name, array $subDirectories): string
201
    {
202 24
        $path = realpath($pathToProjectRoot) . '/' . implode('/', $subDirectories) . '/' . $name . '.php';
203
204 24
        return $path;
205
    }
206
207
    /**
208
     * Take a potentially non existent path and resolve the relativeness into a normal path
209
     *
210
     * @param string $relativePath
211
     *
212
     * @return string
213
     * @throws \RuntimeException
214
     */
215 63
    public function resolvePath(string $relativePath): string
216
    {
217 63
        $path     = [];
218 63
        $absolute = ($relativePath[0] === '/');
219 63
        foreach (explode('/', $relativePath) as $part) {
220
            // ignore parts that have no value
221 63
            if (empty($part) || $part === '.') {
222 63
                continue;
223
            }
224
225 63
            if ($part !== '..') {
226 63
                $path[] = $part;
227 63
                continue;
228
            }
229 36
            if (count($path) > 0) {
230
                // going back up? sure
231 36
                array_pop($path);
232 36
                continue;
233
            }
234
            throw new \RuntimeException('Relative path resolves above root path.');
235
        }
236
237 63
        $return = implode('/', $path);
238 63
        if ($absolute) {
239 63
            $return = "/$return";
240
        }
241
242 63
        return $return;
243
    }
244
}
245