Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
UpdateMyPanini2024Endpoint | |
0.00% |
0 / 45 |
|
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 1 |
handle | |
0.00% |
0 / 45 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace Olz\Apps\Panini2024\Endpoints; |
4 | |
5 | use Olz\Api\OlzTypedEndpoint; |
6 | use Olz\Apps\Panini2024\Panini2024Constants; |
7 | use Olz\Entity\Panini2024\Panini2024Picture; |
8 | use PhpTypeScriptApi\HttpError; |
9 | |
10 | /** |
11 | * @phpstan-type OlzPanini2024PictureData array{ |
12 | * id?: ?int<1, max>, |
13 | * line1: non-empty-string, |
14 | * line2: non-empty-string, |
15 | * residence: non-empty-string, |
16 | * uploadId: non-empty-string, |
17 | * onOff: bool, |
18 | * info1: non-empty-string, |
19 | * info2: non-empty-string, |
20 | * info3: non-empty-string, |
21 | * info4: non-empty-string, |
22 | * info5: non-empty-string, |
23 | * } |
24 | * |
25 | * @extends OlzTypedEndpoint< |
26 | * array{data: OlzPanini2024PictureData}, |
27 | * array{status: 'OK'|'ERROR'} |
28 | * > |
29 | */ |
30 | class UpdateMyPanini2024Endpoint extends OlzTypedEndpoint { |
31 | protected function handle(mixed $input): mixed { |
32 | $this->checkPermission('any'); |
33 | |
34 | $current_user = $this->authUtils()->getCurrentUser(); |
35 | $panini_repo = $this->entityManager()->getRepository(Panini2024Picture::class); |
36 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
37 | $data_path = $this->envUtils()->getDataPath(); |
38 | $deadline_datetime = new \DateTime(Panini2024Constants::UPDATE_DEADLINE); |
39 | |
40 | $has_admin_access = $this->authUtils()->hasPermission('all'); |
41 | if ($now_datetime > $deadline_datetime && !$has_admin_access) { |
42 | throw new HttpError(400, "Zu spät!"); |
43 | } |
44 | |
45 | $input_data = $input['data']; |
46 | $id = $input_data['id'] ?? null; |
47 | if ($id) { |
48 | $picture = $panini_repo->findOneBy(['id' => $id]); |
49 | if (!$picture) { |
50 | throw new HttpError(404, "No such panini picture."); |
51 | } |
52 | } else { |
53 | $picture = new Panini2024Picture(); |
54 | $picture->setOwnerUser($current_user); |
55 | $picture->setOwnerRole(null); |
56 | $picture->setCreatedAt($now_datetime); |
57 | $picture->setCreatedByUser($current_user); |
58 | $picture->setLastModifiedAt($now_datetime); |
59 | $picture->setLastModifiedByUser($current_user); |
60 | } |
61 | $picture->setLine1($input_data['line1']); |
62 | $picture->setLine2($input_data['line2']); |
63 | $picture->setAssociation($input_data['residence']); |
64 | $picture->setImgSrc($input_data['uploadId']); |
65 | $picture->setImgStyle('width:100%; top:0%; left:0%;'); |
66 | $picture->setIsLandscape(false); |
67 | $picture->setHasTop(false); |
68 | $picture->setInfos([ |
69 | $input_data['info1'], |
70 | $input_data['info2'], |
71 | $input_data['info3'], |
72 | $input_data['info4'], |
73 | $input_data['info5'], |
74 | ]); |
75 | $picture->setOnOff($input_data['onOff'] ? 1 : 0); |
76 | $this->entityManager()->persist($picture); |
77 | $this->entityManager()->flush(); |
78 | |
79 | $panini_id = $picture->getId(); |
80 | |
81 | $portraits_path = "{$data_path}panini_data/portraits/{$panini_id}/"; |
82 | $valid_upload_id = $this->uploadUtils()->getValidUploadId($input_data['uploadId']); |
83 | if ($valid_upload_id) { |
84 | $this->uploadUtils()->overwriteUploads([$valid_upload_id], $portraits_path); |
85 | } |
86 | |
87 | return ['status' => 'OK']; |
88 | } |
89 | } |