Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.08% |
49 / 51 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| EntityUtils | |
96.08% |
49 / 51 |
|
60.00% |
3 / 5 |
17 | |
0.00% |
0 / 1 |
| createOlzEntity | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 | |||
| updateOlzEntity | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
| canUpdateOlzEntity | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
6 | |||
| olzEntityClasses | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fromEnv | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Utils; |
| 4 | |
| 5 | use Olz\Entity\Anmelden\Booking; |
| 6 | use Olz\Entity\Anmelden\Registration; |
| 7 | use Olz\Entity\Anmelden\RegistrationInfo; |
| 8 | use Olz\Entity\Common\OlzEntity; |
| 9 | use Olz\Entity\Faq\Question; |
| 10 | use Olz\Entity\Faq\QuestionCategory; |
| 11 | use Olz\Entity\Karten\Karte; |
| 12 | use Olz\Entity\News\NewsEntry; |
| 13 | use Olz\Entity\Panini2024\Panini2024Picture; |
| 14 | use Olz\Entity\Quiz\Skill; |
| 15 | use Olz\Entity\Quiz\SkillCategory; |
| 16 | use Olz\Entity\Quiz\SkillLevel; |
| 17 | use Olz\Entity\Roles\Role; |
| 18 | use Olz\Entity\Service\Download; |
| 19 | use Olz\Entity\Service\Link; |
| 20 | use Olz\Entity\Snippets\Snippet; |
| 21 | use Olz\Entity\Startseite\WeeklyPicture; |
| 22 | use Olz\Entity\Termine\Termin; |
| 23 | use Olz\Entity\Termine\TerminLabel; |
| 24 | use Olz\Entity\Termine\TerminLocation; |
| 25 | use Olz\Entity\Termine\TerminTemplate; |
| 26 | use Olz\Entity\Users\User; |
| 27 | |
| 28 | class EntityUtils { |
| 29 | use WithUtilsTrait; |
| 30 | |
| 31 | /** @var array<class-string<OlzEntity>> */ |
| 32 | protected static array $olzEntityClasses = [ |
| 33 | Booking::class, |
| 34 | Registration::class, |
| 35 | RegistrationInfo::class, |
| 36 | Question::class, |
| 37 | QuestionCategory::class, |
| 38 | Karte::class, |
| 39 | NewsEntry::class, |
| 40 | Panini2024Picture::class, |
| 41 | Skill::class, |
| 42 | SkillCategory::class, |
| 43 | SkillLevel::class, |
| 44 | Role::class, |
| 45 | Download::class, |
| 46 | Link::class, |
| 47 | Snippet::class, |
| 48 | WeeklyPicture::class, |
| 49 | Termin::class, |
| 50 | TerminLabel::class, |
| 51 | TerminLocation::class, |
| 52 | TerminTemplate::class, |
| 53 | User::class, |
| 54 | ]; |
| 55 | |
| 56 | /** @param array{onOff?: bool, ownerUserId?: ?int, ownerRoleId?: ?int} $input */ |
| 57 | public function createOlzEntity(OlzEntity $entity, array $input): void { |
| 58 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 59 | $role_repo = $this->entityManager()->getRepository(Role::class); |
| 60 | $current_user = $this->authUtils()->getCurrentUser(); |
| 61 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
| 62 | |
| 63 | $on_off = ($input['onOff'] ?? false) ? 1 : 0; |
| 64 | |
| 65 | $owner_user_id = $input['ownerUserId'] ?? null; |
| 66 | $owner_user = $current_user; |
| 67 | if ($owner_user_id) { |
| 68 | $owner_user = $user_repo->findOneBy(['id' => $owner_user_id]); |
| 69 | } |
| 70 | |
| 71 | $owner_role_id = $input['ownerRoleId'] ?? null; |
| 72 | $owner_role = null; |
| 73 | if ($owner_role_id) { |
| 74 | $owner_role = $role_repo->findOneBy(['id' => $owner_role_id]); |
| 75 | } |
| 76 | |
| 77 | $entity->setOnOff($on_off); |
| 78 | $entity->setOwnerUser($owner_user); |
| 79 | $entity->setOwnerRole($owner_role); |
| 80 | $entity->setCreatedAt($now_datetime); |
| 81 | $entity->setCreatedByUser($current_user); |
| 82 | $entity->setLastModifiedAt($now_datetime); |
| 83 | $entity->setLastModifiedByUser($current_user); |
| 84 | } |
| 85 | |
| 86 | /** @param array{onOff?: bool, ownerUserId?: ?int, ownerRoleId?: ?int} $input */ |
| 87 | public function updateOlzEntity(OlzEntity $entity, array $input): void { |
| 88 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 89 | $role_repo = $this->entityManager()->getRepository(Role::class); |
| 90 | $current_user = $this->authUtils()->getCurrentUser(); |
| 91 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
| 92 | |
| 93 | $on_off = $input['onOff'] ?? null; |
| 94 | if ($on_off !== null) { |
| 95 | $entity->setOnOff($on_off ? 1 : 0); |
| 96 | } |
| 97 | |
| 98 | $owner_user_id = $input['ownerUserId'] ?? null; |
| 99 | if ($owner_user_id) { |
| 100 | $owner_user = $user_repo->findOneBy(['id' => $owner_user_id]); |
| 101 | $entity->setOwnerUser($owner_user); |
| 102 | } |
| 103 | |
| 104 | $owner_role_id = $input['ownerRoleId'] ?? null; |
| 105 | if ($owner_role_id) { |
| 106 | $owner_role = $role_repo->findOneBy(['id' => $owner_role_id]); |
| 107 | $entity->setOwnerRole($owner_role); |
| 108 | } |
| 109 | |
| 110 | $entity->setLastModifiedAt($now_datetime); |
| 111 | $entity->setLastModifiedByUser($current_user); |
| 112 | } |
| 113 | |
| 114 | /** @param ?array{onOff?: bool, ownerUserId?: ?int, ownerRoleId?: ?int} $meta_arg */ |
| 115 | public function canUpdateOlzEntity( |
| 116 | ?OlzEntity $entity, |
| 117 | ?array $meta_arg, |
| 118 | string $edit_permission = 'all', |
| 119 | ): bool { |
| 120 | $meta = $meta_arg ?? []; |
| 121 | $auth_utils = $this->authUtils(); |
| 122 | $current_user = $auth_utils->getCurrentUser(); |
| 123 | |
| 124 | if ($auth_utils->hasPermission($edit_permission)) { |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | $owner_user = $entity?->getOwnerUser(); |
| 129 | if ($owner_user && $current_user?->getId() === $owner_user->getId()) { |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | $created_by_user = $entity?->getCreatedByUser(); |
| 134 | if ($created_by_user && $current_user?->getId() === $created_by_user->getId()) { |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | // TODO: Check roles |
| 139 | |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | /** @return array<class-string<OlzEntity>> */ |
| 144 | public function olzEntityClasses(): array { |
| 145 | return $this::$olzEntityClasses; |
| 146 | } |
| 147 | |
| 148 | public static function fromEnv(): self { |
| 149 | return new self(); |
| 150 | } |
| 151 | } |