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 (#51)
by joseph
06:58
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
dl 0
loc 19
ccs 7
cts 12
cp 0.5833
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
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\Exception\DoctrineStaticMetaException;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class PathHelper
11
{
12
    /**
13
     * @var Filesystem
14
     */
15
    protected $filesystem;
16
    /**
17
     * @var FileCreationTransaction
18
     */
19
    protected $fileCreationTransaction;
20
21 60
    public function __construct(Filesystem $filesystem, FileCreationTransaction $fileCreationTransaction)
22
    {
23 60
        $this->filesystem              = $filesystem;
24 60
        $this->fileCreationTransaction = $fileCreationTransaction;
25 60
    }
26
27
    /**
28
     * @param string $pathToProjectRoot
29
     * @param array  $subDirectories
30
     *
31
     * @return string
32
     * @throws DoctrineStaticMetaException
33
     */
34 58
    public function createSubDirectoriesAndGetPath(string $pathToProjectRoot, array $subDirectories): string
35
    {
36 58
        if (!$this->filesystem->exists($pathToProjectRoot)) {
37
            throw new DoctrineStaticMetaException("path to project root $pathToProjectRoot does not exist");
38
        }
39 58
        foreach ($subDirectories as $sd) {
40 58
            $pathToProjectRoot .= "/$sd";
41
            try {
42 58
                $this->filesystem->mkdir($pathToProjectRoot);
43
            } catch (\Exception $e) {
44
                throw new DoctrineStaticMetaException(
45
                    'Exception in '.__METHOD__.': '.$e->getMessage(),
46
                    $e->getCode(),
47 58
                    $e
48
                );
49
            }
50
        }
51
52 58
        return \realpath($pathToProjectRoot);
53
    }
54
55
    /**
56
     * @param string $pathToProjectRoot
57
     * @param string $templatePath
58
     * @param string $destinationFileName
59
     * @param array  $subDirectories
60
     *
61
     * @return string
62
     * @throws DoctrineStaticMetaException
63
     */
64 58
    public function copyTemplateAndGetPath(
65
        string $pathToProjectRoot,
66
        string $templatePath,
67
        string $destinationFileName,
68
        array $subDirectories
69
    ): string {
70
        try {
71 58
            $path = $this->createSubDirectoriesAndGetPath($pathToProjectRoot, $subDirectories);
72 58
            if (false === strpos($destinationFileName, '.php')) {
73 58
                $destinationFileName = "$destinationFileName.php";
74
            }
75 58
            $filePath = "$path/$destinationFileName";
76 58
            $this->filesystem->copy($templatePath, $filePath, true);
77 58
            $this->fileCreationTransaction::setPathCreated($filePath);
78
79 58
            return $filePath;
80
        } catch (\Exception $e) {
81
            throw new DoctrineStaticMetaException('Exception in '.__METHOD__.': '.$e->getMessage(), $e->getCode(), $e);
82
        }
83
    }
84
85
86
    /**
87
     * @param string $pathToProjectRoot
88
     * @param string $templatePath
89
     * @param string $destPath
90
     *
91
     * @return string
92
     * @throws DoctrineStaticMetaException
93
     */
94 24
    public function copyTemplateDirectoryAndGetPath(
95
        string $pathToProjectRoot,
96
        string $templatePath,
97
        string $destPath
98
    ): string {
99 24
        $realTemplatePath = realpath($templatePath);
100 24
        if (false === $realTemplatePath) {
101
            throw new DoctrineStaticMetaException('path '.$templatePath.' does not exist');
102
        }
103
104 24
        $relativeDestPath = $this->filesystem->makePathRelative($destPath, $pathToProjectRoot);
105 24
        $subDirectories   = explode('/', $relativeDestPath);
106 24
        $path             = $this->createSubDirectoriesAndGetPath($pathToProjectRoot, $subDirectories);
107 24
        $this->filesystem->mirror($realTemplatePath, $path);
108 24
        $this->fileCreationTransaction::setPathCreated($path);
109
110 24
        return $path;
111
    }
112
113
    /**
114
     * Move the basename of a path to the find/replaced version
115
     *
116
     * Then return the updated path
117
     *
118
     * @param string $find
119
     * @param string $replace
120
     * @param string $path
121
     *
122
     * @return string
123
     * @throws DoctrineStaticMetaException
124
     */
125 24
    public function renamePathBasename(string $find, string $replace, string $path): string
126
    {
127 24
        $basename    = basename($path);
128 24
        $newBasename = str_replace($find, $replace, $basename);
129 24
        $moveTo      = \dirname($path).'/'.$newBasename;
130 24
        if ($moveTo === $path) {
131 24
            return $path;
132
        }
133 24
        if (is_dir($moveTo) || file_exists($moveTo)) {
134
            throw new DoctrineStaticMetaException(
135
                "Error trying to move:\n[$path]\n to \n[$moveTo]\ndestination already exists"
136
            );
137
        }
138
        try {
139 24
            $this->filesystem->rename($path, $moveTo);
140
        } catch (\Exception $e) {
141
            throw new DoctrineStaticMetaException('Exception in '.__METHOD__.': '.$e->getMessage(), $e->getCode(), $e);
142
        }
143
144 24
        return $moveTo;
145
    }
146
147
    /**
148
     * @param string $path
149
     * @param string $singular
150
     * @param string $plural
151
     *
152
     * @return string
153
     * @throws DoctrineStaticMetaException
154
     */
155 24
    public function renamePathBasenameSingularOrPlural(
156
        string $path,
157
        string $singular,
158
        string $plural
159
    ): string {
160 24
        $find     = AbstractGenerator::FIND_ENTITY_NAME;
161 24
        $replace  = $singular;
162 24
        $basename = \basename($path);
163 24
        if (false !== \strpos($basename, AbstractGenerator::FIND_ENTITY_NAME_PLURAL)) {
164 24
            $find    = AbstractGenerator::FIND_ENTITY_NAME_PLURAL;
165 24
            $replace = $plural;
166
        }
167
168 24
        return $this->renamePathBasename($find, $replace, $path);
169
    }
170
171 21
    public function getPathFromNameAndSubDirs(string $pathToProjectRoot, string $name, array $subDirectories): string
172
    {
173 21
        $path = realpath($pathToProjectRoot).'/'.implode('/', $subDirectories).'/'.$name.'.php';
174
175 21
        return $path;
176
    }
177
}
178