Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
17 / 17 |
| AbstractGenerator | |
100.00% |
1 / 1 |
|
100.00% |
6 / 6 |
13 | |
100.00% |
17 / 17 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| generate | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| makeDirectory | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| checkFileExist | |
100.00% |
1 / 1 |
5 | |
100.00% |
3 / 3 |
|||
| validateConfig | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| getOptions | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| createFile | n/a |
0 / 0 |
0 | n/a |
0 / 0 |
|||||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\FileGenerator\Services\FileGenerator\Generators; |
| 4 | |
| 5 | use Illuminate\Support\Facades\Log; |
| 6 | use Illuminate\Support\Facades\Validator; |
| 7 | use Qmp\Laravel\FileGenerator\Config; |
| 8 | use Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\ConfigGeneratorException; |
| 9 | use Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\FileNotExistException; |
| 10 | |
| 11 | abstract class AbstractGenerator implements GeneratorInterface |
| 12 | { |
| 13 | protected const DEFAULT_RULES = [ |
| 14 | 'output_path' => 'required|string' |
| 15 | ]; |
| 16 | |
| 17 | /** |
| 18 | * @var array |
| 19 | */ |
| 20 | protected array $options = []; |
| 21 | |
| 22 | /** |
| 23 | * @var \Qmp\Laravel\FileGenerator\Config |
| 24 | */ |
| 25 | protected Config $config; |
| 26 | |
| 27 | /** |
| 28 | * @var array|string[] |
| 29 | */ |
| 30 | protected array $rules = []; |
| 31 | |
| 32 | /** |
| 33 | * Undocumented variable |
| 34 | * |
| 35 | * @var boolean |
| 36 | */ |
| 37 | protected bool $distant = false; |
| 38 | |
| 39 | /** |
| 40 | * @param \Qmp\Laravel\FileGenerator\Config $config |
| 41 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\ConfigGeneratorException |
| 42 | */ |
| 43 | public function __construct(Config $config) |
| 44 | { |
| 45 | $this->config = $this->validateConfig($config); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\FileNotExistException |
| 50 | */ |
| 51 | public function generate(): string |
| 52 | { |
| 53 | $this->makeDirectory(); |
| 54 | $file = $this->createFile(); |
| 55 | |
| 56 | return $this->checkFileExist($this->distant ? $file : $this->config->output_path); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * |
| 61 | */ |
| 62 | protected function makeDirectory() |
| 63 | { |
| 64 | if (!is_dir(dirname($this->config->output_path))) { |
| 65 | mkdir(dirname($this->config->output_path), 0755, true); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param string $filename |
| 71 | * @return string filename |
| 72 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\FileNotExistException |
| 73 | */ |
| 74 | protected function checkFileExist(string $filename): string |
| 75 | { |
| 76 | if((!$this->distant && !file_exists($filename)) || ($this->distant && $filename === "")) { |
| 77 | throw new FileNotExistException($filename); |
| 78 | } |
| 79 | |
| 80 | return $filename; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param \Qmp\Laravel\FileGenerator\Config $config |
| 85 | * @return \Qmp\Laravel\FileGenerator\Config |
| 86 | * @throws \Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\ConfigGeneratorException |
| 87 | */ |
| 88 | protected function validateConfig(Config $config): Config |
| 89 | { |
| 90 | $rules = array_merge(self::DEFAULT_RULES, $this->rules); |
| 91 | |
| 92 | $validator = Validator::make($config->toArray(), $rules); |
| 93 | |
| 94 | if ($validator->fails()) { |
| 95 | throw new ConfigGeneratorException($config->type, $validator->errors()); |
| 96 | } |
| 97 | |
| 98 | return $config; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @return array |
| 103 | */ |
| 104 | protected function getOptions(): array |
| 105 | { |
| 106 | return array_merge($this->options, $this->config->options); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * |
| 111 | */ |
| 112 | abstract protected function createFile(); |
| 113 | } |