1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Filesystem; |
||
6 | |||
7 | use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException; |
||
8 | use RuntimeException; |
||
9 | use SplFileInfo; |
||
10 | |||
11 | use function realpath; |
||
12 | |||
13 | abstract class AbstractFilesystemItem |
||
14 | { |
||
15 | /** |
||
16 | * This is the name of the path type. It should be overridden in child classes. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected const PATH_TYPE = 'override me'; |
||
21 | |||
22 | /** |
||
23 | * This is the path as configured for the filesystem item. It in no way means the item exists there, it is simply |
||
24 | * where we expect it to exist if/when it has been created |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $path; |
||
29 | |||
30 | /** |
||
31 | * What permissions should be set to created filesystem items? |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $createModeOctal = 0777; |
||
36 | |||
37 | /** |
||
38 | * A string representation of the octal number |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $createModeOctalString = '0777'; |
||
43 | |||
44 | /** |
||
45 | * Stores an instance fo the SplFileInfo object for the path item. |
||
46 | * |
||
47 | * @var SplFileInfo |
||
48 | */ |
||
49 | protected $splFileInfo; |
||
50 | |||
51 | /** |
||
52 | * @param string $path - the path where we would expect the item to exist if/when it has been created |
||
53 | */ |
||
54 | 25 | public function __construct(string $path = null) |
|
55 | { |
||
56 | 25 | if (static::PATH_TYPE === self::PATH_TYPE) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
57 | throw new RuntimeException('You must override the PATH_TYPE in your concrete class'); |
||
58 | } |
||
59 | 25 | if (null !== $path) { |
|
60 | 19 | $this->setPath($path); |
|
61 | } |
||
62 | 25 | } |
|
63 | |||
64 | /** |
||
65 | * Create the filesystem item, asserting that it does not already exist |
||
66 | * |
||
67 | * @return static |
||
68 | * @throws DoctrineStaticMetaException |
||
69 | */ |
||
70 | 15 | public function create() |
|
71 | { |
||
72 | 15 | $this->assertPathIsSet(); |
|
73 | 11 | $this->assertNotExists(); |
|
74 | 9 | $this->doCreate(); |
|
75 | 6 | $this->setPermissions(); |
|
76 | |||
77 | 6 | return $this; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * @throws DoctrineStaticMetaException |
||
82 | */ |
||
83 | 45 | protected function assertPathIsSet(): void |
|
84 | { |
||
85 | 45 | if (null === $this->path) { |
|
86 | 18 | throw new DoctrineStaticMetaException('$this->path is not set'); |
|
87 | } |
||
88 | 27 | } |
|
89 | |||
90 | /** |
||
91 | * Throw an Exception if there is already something that exists at the path |
||
92 | * |
||
93 | * @throws DoctrineStaticMetaException |
||
94 | */ |
||
95 | 11 | protected function assertNotExists(): void |
|
96 | { |
||
97 | 11 | if (true === $this->exists()) { |
|
98 | 2 | throw new DoctrineStaticMetaException(static::PATH_TYPE . ' already exists at path ' . $this->path); |
|
99 | } |
||
100 | 9 | } |
|
101 | |||
102 | /** |
||
103 | * Check if something exists at the real path |
||
104 | * |
||
105 | * @return bool |
||
106 | */ |
||
107 | 34 | public function exists(): bool |
|
108 | { |
||
109 | 34 | $realPath = realpath($this->path); |
|
110 | 34 | if (false === $realPath) { |
|
111 | 27 | return false; |
|
112 | } |
||
113 | 12 | $this->path = $realPath; |
|
114 | 12 | $this->assertCorrectType(); |
|
115 | |||
116 | 10 | return true; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Check that the path is actually a file/directory etc |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | 12 | protected function assertCorrectType(): void |
|
125 | { |
||
126 | 12 | if (false === $this->isCorrectType()) { |
|
127 | 2 | throw new RuntimeException('path is not the correct type: ' . $this->path); |
|
128 | } |
||
129 | 10 | } |
|
130 | |||
131 | abstract protected function isCorrectType(): bool; |
||
132 | |||
133 | /** |
||
134 | * This is the specific creation logic for the concrete filesystem type. |
||
135 | */ |
||
136 | abstract protected function doCreate(): void; |
||
137 | |||
138 | 6 | private function setPermissions(): void |
|
139 | { |
||
140 | 6 | chmod($this->path, $this->createModeOctal); |
|
141 | 6 | } |
|
142 | |||
143 | /** |
||
144 | * Get the path to the filesystem item as it is currently set in this object |
||
145 | * |
||
146 | * @return null|string |
||
147 | */ |
||
148 | 15 | public function getPath(): ?string |
|
149 | { |
||
150 | 15 | return $this->path; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * Set the string path to the filesystem item |
||
155 | * |
||
156 | * @param string $path |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | 21 | public function setPath(string $path) |
|
161 | { |
||
162 | 21 | $this->path = $path; |
|
163 | |||
164 | 21 | return $this; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * Provide an SplFileInfo object, asserting that the path exists |
||
169 | * |
||
170 | * @return SplFileInfo |
||
171 | * @throws DoctrineStaticMetaException |
||
172 | */ |
||
173 | 20 | public function getSplFileInfo(): SplFileInfo |
|
174 | { |
||
175 | 20 | if (null !== $this->splFileInfo && $this->path === $this->splFileInfo->getRealPath()) { |
|
176 | 3 | return $this->splFileInfo; |
|
177 | } |
||
178 | 17 | $this->assertExists(); |
|
179 | |||
180 | 1 | return $this->createSplFileInfo(); |
|
181 | } |
||
182 | |||
183 | 38 | protected function assertExists(): void |
|
184 | { |
||
185 | 38 | $this->assertPathIsSet(); |
|
186 | 24 | if (false === $this->exists()) { |
|
187 | 17 | throw new DoctrineStaticMetaException(static::PATH_TYPE . ' does not exist at path ' . $this->path); |
|
188 | } |
||
189 | 7 | } |
|
190 | |||
191 | /** |
||
192 | * Create an SplFileInfo, assuming the path already exists |
||
193 | * |
||
194 | * @return SplFileInfo |
||
195 | */ |
||
196 | 12 | protected function createSplFileInfo(): SplFileInfo |
|
197 | { |
||
198 | 12 | if (null !== $this->splFileInfo && $this->path === $this->splFileInfo->getRealPath()) { |
|
199 | 2 | return $this->splFileInfo; |
|
200 | } |
||
201 | 11 | $this->splFileInfo = new SplFileInfo($this->path); |
|
202 | |||
203 | 11 | return $this->splFileInfo; |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * @param int $createMode |
||
208 | * |
||
209 | * @return static |
||
210 | */ |
||
211 | 2 | public function setCreateMode(int $createMode): self |
|
212 | { |
||
213 | 2 | $this->createModeOctal = $createMode; |
|
214 | 2 | $this->createModeOctalString = decoct($createMode); |
|
215 | |||
216 | 2 | return $this; |
|
217 | } |
||
218 | } |
||
219 |