Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
17 / 17 |
| GoogleSheetsGenerator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
17 / 17 |
| createFile | |
100.00% |
1 / 1 |
6 | |
100.00% |
17 / 17 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\FileGenerator\Services\FileGenerator\Generators; |
| 4 | |
| 5 | use Illuminate\Support\Facades\Log; |
| 6 | use Qmp\Laravel\GoogleApiWrapper\GoogleApiWrapper; |
| 7 | |
| 8 | class GoogleSheetsGenerator extends AbstractGenerator |
| 9 | { |
| 10 | /** |
| 11 | * @var array|string[] |
| 12 | */ |
| 13 | protected array $rules = [ |
| 14 | 'type' => 'required|in:sheets', |
| 15 | 'content' => 'required|array' |
| 16 | ]; |
| 17 | |
| 18 | /** |
| 19 | * Undocumented variable |
| 20 | * |
| 21 | * @var boolean |
| 22 | */ |
| 23 | protected bool $distant = true; |
| 24 | |
| 25 | /** |
| 26 | * |
| 27 | */ |
| 28 | protected function createFile() |
| 29 | { |
| 30 | $wrapper = GoogleApiWrapper::withUser($this->config->getOption('user')); |
| 31 | $service = $wrapper->service($this->config->type); |
| 32 | $fileId = $this->config->getOption('fileId') ?? null; |
| 33 | |
| 34 | $methods = array_filter( |
| 35 | [ |
| 36 | 'withPermission' => $this->config->getOption('permission'), |
| 37 | 'withMessage' => $this->config->getOption('message') |
| 38 | ] |
| 39 | ); |
| 40 | |
| 41 | if ($fileId) { |
| 42 | $service->spreadsheet($fileId); |
| 43 | foreach ($this->config->content as $sheetTitle => $sheetContent) { |
| 44 | $service->sheet($sheetTitle)->append($sheetContent); |
| 45 | } |
| 46 | return $fileId; |
| 47 | } |
| 48 | |
| 49 | foreach ($methods as $method => $value) { |
| 50 | $service->$method($value); |
| 51 | } |
| 52 | |
| 53 | foreach ($this->config->content as $sheetTitle => $sheetContent) { |
| 54 | $service->newSheet(is_numeric($sheetTitle) ? XlsxGenerator::DEFAULT_SHEET_NAME : $sheetTitle, $sheetContent); |
| 55 | } |
| 56 | |
| 57 | $response = $service->save($this->config->output_path); |
| 58 | return $response->spreadsheetId ?? ""; |
| 59 | } |
| 60 | } |