Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
14 / 14 |
|
100.00% |
9 / 9 |
CRAP | |
100.00% |
1 / 1 |
| CreateAction | |
100.00% |
14 / 14 |
|
100.00% |
9 / 9 |
11 | |
100.00% |
1 / 1 |
| handleCreate | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| create | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| force | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setOutputPath | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getOutputPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAbsoluteOutputPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fileExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasFileConflict | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| save | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| formatStringForStorage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Publications\Actions; |
| 6 | |
| 7 | use Hyde\Hyde; |
| 8 | use Illuminate\Support\Str; |
| 9 | use Hyde\Framework\Exceptions\FileConflictException; |
| 10 | use Hyde\Framework\Concerns\InteractsWithDirectories; |
| 11 | |
| 12 | use function file_exists; |
| 13 | use function file_put_contents; |
| 14 | |
| 15 | /** |
| 16 | * @see \Hyde\Publications\Testing\Feature\CreateActionTest |
| 17 | * |
| 18 | * @internal This class is experimental and is not meant to be used outside the Hyde framework. |
| 19 | */ |
| 20 | abstract class CreateAction |
| 21 | { |
| 22 | use InteractsWithDirectories; |
| 23 | |
| 24 | protected string $outputPath; |
| 25 | protected bool $force = false; |
| 26 | |
| 27 | abstract protected function handleCreate(): void; |
| 28 | |
| 29 | /** |
| 30 | * Create the file at the configured output path. |
| 31 | * |
| 32 | * @throws \Hyde\Framework\Exceptions\FileConflictException |
| 33 | */ |
| 34 | public function create(): void |
| 35 | { |
| 36 | if ($this->hasFileConflict()) { |
| 37 | throw new FileConflictException($this->outputPath); |
| 38 | } |
| 39 | |
| 40 | $this->handleCreate(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @param bool $force Should existing files at the output path be overwritten? |
| 45 | * @return $this |
| 46 | */ |
| 47 | public function force(bool $force = true): static |
| 48 | { |
| 49 | $this->force = $force; |
| 50 | |
| 51 | return $this; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param string $outputPath Relative path. |
| 56 | * @return $this |
| 57 | */ |
| 58 | public function setOutputPath(string $outputPath): static |
| 59 | { |
| 60 | $this->outputPath = $outputPath; |
| 61 | |
| 62 | return $this; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return string Relative path. |
| 67 | */ |
| 68 | public function getOutputPath(): string |
| 69 | { |
| 70 | return $this->outputPath; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return string Absolute path. |
| 75 | */ |
| 76 | public function getAbsoluteOutputPath(): string |
| 77 | { |
| 78 | return Hyde::path($this->getOutputPath()); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return bool Does a file at the output path already exist? |
| 83 | */ |
| 84 | public function fileExists(): bool |
| 85 | { |
| 86 | return file_exists($this->getAbsoluteOutputPath()); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @return bool Will the action cause a file conflict exception? |
| 91 | */ |
| 92 | public function hasFileConflict(): bool |
| 93 | { |
| 94 | return $this->fileExists() && ! $this->force; |
| 95 | } |
| 96 | |
| 97 | protected function save(string $contents): void |
| 98 | { |
| 99 | $this->needsParentDirectory($this->getAbsoluteOutputPath()); |
| 100 | file_put_contents($this->getAbsoluteOutputPath(), $contents); |
| 101 | } |
| 102 | |
| 103 | protected function formatStringForStorage(string $string): string |
| 104 | { |
| 105 | return Str::slug($string); |
| 106 | } |
| 107 | } |