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