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 (#58)
by joseph
18:31
created

PathHelper::getProjectRootDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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