AbstractWriter::writeFile()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 3
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Generator\Writer;
4
5
use Janisbiz\LightOrm\Generator\Dms\DmsDatabaseInterface;
6
use Janisbiz\LightOrm\Generator\Dms\DmsTableInterface;
7
8
abstract class AbstractWriter implements WriterInterface
9
{
10
    /**
11
     * @var WriterConfigInterface
12
     */
13
    protected $writerConfig;
14
15
    /**
16
     * @param DmsDatabaseInterface $dmsDatabase
17
     *
18
     * @return string
19
     */
20
    public function generateNamespace(DmsDatabaseInterface $dmsDatabase)
21
    {
22
        return \rtrim(
23
            \sprintf(
24
                '%s\\%s\\%s%s',
25
                $this->getWriterConfig()->getNamespace(),
26
                $dmsDatabase->getPhpName(),
27
                $this->getWriterConfig()->getClassPrefix(),
28
                $this->getWriterConfig()->getClassSuffix()
29
            ),
30
            '\\'
31
        );
32
    }
33
34
    /**
35
     * @param DmsTableInterface $dmsTable
36
     *
37
     * @return string
38
     */
39
    public function generateClassName(DmsTableInterface $dmsTable): string
40
    {
41
        return \implode(
42
            '',
43
            [
44
                $this->getWriterConfig()->getClassPrefix(),
45
                $dmsTable->getPhpName(),
46
                $this->getWriterConfig()->getClassSuffix()
47
            ]
48
        );
49
    }
50
51
    /**
52
     * @param DmsDatabaseInterface $dmsDatabase
53
     * @param DmsTableInterface $dmsTable
54
     *
55
     * @return string
56
     */
57
    public function generateFQDN(DmsDatabaseInterface $dmsDatabase, DmsTableInterface $dmsTable): string
58
    {
59
        return \sprintf('%s\\%s', $this->generateNamespace($dmsDatabase), $this->generateClassName($dmsTable));
60
    }
61
62
    /**
63
     * @param DmsDatabaseInterface $dmsDatabase
64
     *
65
     * @return string[]
66
     */
67
    public function read(DmsDatabaseInterface $dmsDatabase): array
68
    {
69
        $fileDirectory = $this->generateFileDirectory($dmsDatabase);
70
        $handle = @\opendir($fileDirectory);
71
72
        $files = [];
73
74
        if (false === $handle) {
75
            return $files;
76
        }
77
78
        while (false !== ($file = \readdir($handle))) {
79
            $fileName = \implode(
80
                '',
81
                [
82
                    $fileDirectory,
83
                    DIRECTORY_SEPARATOR,
84
                    $file,
85
                ]
86
            );
87
88
            if (\is_dir($fileName)) {
89
                continue;
90
            }
91
92
            $files[$fileName] = $file;
93
        }
94
        \closedir($handle);
95
        \asort($files);
96
97
        return $files;
98
    }
99
100
    /**
101
     * @return WriterConfigInterface
102
     */
103
    abstract protected function getWriterConfig(): WriterConfigInterface;
104
105
    /**
106
     * @param DmsDatabaseInterface $dmsDatabase
107
     *
108
     * @return string
109
     */
110
    protected function generateFileDirectory(DmsDatabaseInterface $dmsDatabase): string
111
    {
112
        return \rtrim(
113
            \implode(
114
                '',
115
                [
116
                    $this->getWriterConfig()->getDirectory(),
117
                    DIRECTORY_SEPARATOR,
118
                    $dmsDatabase->getPhpName(),
119
                    DIRECTORY_SEPARATOR,
120
                    $this->getWriterConfig()->getClassPrefix(),
121
                    $this->getWriterConfig()->getClassSuffix()
122
                ]
123
            ),
124
            DIRECTORY_SEPARATOR
125
        );
126
    }
127
128
    /**
129
     * @param DmsDatabaseInterface $dmsDatabase
130
     * @param DmsTableInterface $dmsTable
131
     *
132
     * @return string
133
     */
134
    protected function generateFileName(DmsDatabaseInterface $dmsDatabase, DmsTableInterface $dmsTable): string
135
    {
136
        return \sprintf(
137
            '%s.php',
138
            \implode(
139
                '',
140
                [
141
                    $this->generateFileDirectory($dmsDatabase),
142
                    DIRECTORY_SEPARATOR,
143
                    $this->getWriterConfig()->getClassPrefix(),
144
                    $dmsTable->getPhpName(),
145
                    $this->getWriterConfig()->getClassSuffix(),
146
                ]
147
            )
148
        );
149
    }
150
151
    /**
152
     * @param string $fileName
153
     * @param string $fileContent
154
     * @param bool $skipIfExists
155
     *
156
     * @return $this
157
     */
158
    protected function writeFile(string $fileName, string $fileContent, $skipIfExists = false): AbstractWriter
159
    {
160
        if (true === $skipIfExists && \file_exists($fileName)) {
161
            return $this;
162
        }
163
164
        $fileDirectory = \dirname($fileName);
165
        if (!\file_exists($fileDirectory)) {
166
            \mkdir($fileDirectory, 0777, true);
167
        }
168
169
        $file = \fopen($fileName, 'w');
170
        \fwrite($file, $fileContent);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

170
        \fwrite(/** @scrutinizer ignore-type */ $file, $fileContent);
Loading history...
171
        \fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

171
        \fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
172
173
        return $this;
174
    }
175
176
    /**
177
     * @param string $fileName
178
     * @param string[] $existingFiles
179
     *
180
     * @return $this
181
     */
182
    protected function removeFileFromExistingFiles(string $fileName, array &$existingFiles): AbstractWriter
183
    {
184
        foreach ($existingFiles as $i => $existingFile) {
185
            if (\basename($fileName) === \basename($existingFile)) {
186
                unset($existingFiles[$i]);
187
            }
188
        }
189
190
        return $this;
191
    }
192
}
193