Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| ExportMembersEndpoint | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
72 | |
0.00% |
0 / 1 |
| handle | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Apps\Members\Endpoints; |
| 4 | |
| 5 | use Olz\Api\OlzTypedEndpoint; |
| 6 | use Olz\Entity\Members\Member; |
| 7 | use PhpTypeScriptApi\HttpError; |
| 8 | |
| 9 | /** |
| 10 | * @extends OlzTypedEndpoint< |
| 11 | * array{}, |
| 12 | * array{status: 'OK'|'ERROR', csvFileId?: ?non-empty-string} |
| 13 | * > |
| 14 | */ |
| 15 | class ExportMembersEndpoint extends OlzTypedEndpoint { |
| 16 | protected function handle(mixed $input): mixed { |
| 17 | if (!$this->authUtils()->hasPermission('vorstand')) { |
| 18 | throw new HttpError(403, "Kein Zugriff!"); |
| 19 | } |
| 20 | $data_path = $this->envUtils()->getDataPath(); |
| 21 | $member_repo = $this->entityManager()->getRepository(Member::class); |
| 22 | |
| 23 | $user = $this->authUtils()->getCurrentUser(); |
| 24 | $this->log()->info("Members export by {$user?->getUsername()}."); |
| 25 | |
| 26 | $updates = []; |
| 27 | foreach ($member_repo->findAll() as $member) { |
| 28 | $user = $member->getUser(); |
| 29 | if (!$user) { |
| 30 | continue; |
| 31 | } |
| 32 | $update = json_decode($member->getUpdates() ?? 'null', true); |
| 33 | if (!$update) { |
| 34 | continue; |
| 35 | } |
| 36 | $data = json_decode($member->getData(), true); |
| 37 | $updates[] = [...$data, ...$update]; |
| 38 | } |
| 39 | |
| 40 | if (!$updates) { |
| 41 | return ['status' => 'OK', 'csvFileId' => null]; |
| 42 | } |
| 43 | |
| 44 | $csv_file_id = $this->uploadUtils()->getRandomUploadId('.csv'); |
| 45 | $file_path = "{$data_path}temp/{$csv_file_id}"; |
| 46 | $fp = fopen($file_path, 'w'); |
| 47 | $this->generalUtils()->checkNotFalse($fp, "Could not open file {$file_path}"); |
| 48 | $fields = array_keys($updates[0]); |
| 49 | fputcsv($fp, $fields, $separator = ",", $enclosure = "\"", $escape = "\\", $eol = "\n"); |
| 50 | foreach ($updates as $update) { |
| 51 | $data = []; |
| 52 | foreach ($fields as $field) { |
| 53 | $value = $update[$field] ?? null; |
| 54 | $this->generalUtils()->checkNotNull($value, "Update is missing field: {$field}"); |
| 55 | $data[] = $value; |
| 56 | } |
| 57 | fputcsv($fp, $data, $separator = ",", $enclosure = "\"", $escape = "\\", $eol = "\n"); |
| 58 | } |
| 59 | fclose($fp); |
| 60 | return ['status' => 'OK', 'csvFileId' => $csv_file_id]; |
| 61 | } |
| 62 | } |