| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 0.00% | 0 / 50 |  | 0.00% | 0 / 1 | CRAP |  | 0.00% | 0 / 1 | 
| DeleteUserEndpoint |  | 0.00% | 0 / 50 |  | 0.00% | 0 / 1 | 132 |  | 0.00% | 0 / 1 | 
| handle |  | 0.00% | 0 / 50 |  | 0.00% | 0 / 1 | 132 | |||
| 1 | <?php | 
| 2 | |
| 3 | namespace Olz\Users\Endpoints; | 
| 4 | |
| 5 | use Olz\Api\OlzDeleteEntityTypedEndpoint; | 
| 6 | use Olz\Entity\AccessToken; | 
| 7 | use Olz\Entity\News\NewsEntry; | 
| 8 | use Olz\Entity\NotificationSubscription; | 
| 9 | use Olz\Entity\StravaLink; | 
| 10 | use Olz\Entity\TelegramLink; | 
| 11 | use PhpTypeScriptApi\HttpError; | 
| 12 | |
| 13 | /** | 
| 14 | * @phpstan-import-type OlzUserId from UserEndpointTrait | 
| 15 | * @phpstan-import-type OlzUserData from UserEndpointTrait | 
| 16 | * | 
| 17 | * @extends OlzDeleteEntityTypedEndpoint<OlzUserId, OlzUserData> | 
| 18 | */ | 
| 19 | class DeleteUserEndpoint extends OlzDeleteEntityTypedEndpoint { | 
| 20 | use UserEndpointTrait; | 
| 21 | |
| 22 | protected function handle(mixed $input): mixed { | 
| 23 | $entity = $this->getEntityById($input['id']); | 
| 24 | |
| 25 | $current_user = $this->authUtils()->getCurrentUser(); | 
| 26 | $is_me = ( | 
| 27 | $current_user | 
| 28 | && $entity->getUsername() === $current_user->getUsername() | 
| 29 | && $entity->getId() === $current_user->getId() | 
| 30 | ); | 
| 31 | $can_update = $this->entityUtils()->canUpdateOlzEntity($entity, null, 'users'); | 
| 32 | if (!$is_me && !$can_update) { | 
| 33 | throw new HttpError(403, "Kein Zugriff!"); | 
| 34 | } | 
| 35 | |
| 36 | // Remove news ownership | 
| 37 | $news_repo = $this->entityManager()->getRepository(NewsEntry::class); | 
| 38 | $news_entries = $news_repo->findBy(['owner_user' => $entity]); | 
| 39 | foreach ($news_entries as $news_entry) { | 
| 40 | $news_entry->setOwnerUser(null); | 
| 41 | $this->entityManager()->remove($news_entry); | 
| 42 | } | 
| 43 | $this->entityManager()->flush(); | 
| 44 | |
| 45 | // Remove notification subscriptions | 
| 46 | $notification_subscription_repo = $this->entityManager()->getRepository(NotificationSubscription::class); | 
| 47 | $subscriptions = $notification_subscription_repo->findBy(['user' => $entity]); | 
| 48 | foreach ($subscriptions as $subscription) { | 
| 49 | $this->entityManager()->remove($subscription); | 
| 50 | } | 
| 51 | $this->entityManager()->flush(); | 
| 52 | |
| 53 | // Remove telegram links | 
| 54 | $telegram_link_repo = $this->entityManager()->getRepository(TelegramLink::class); | 
| 55 | $telegram_links = $telegram_link_repo->findBy(['user' => $entity]); | 
| 56 | foreach ($telegram_links as $telegram_link) { | 
| 57 | $this->entityManager()->remove($telegram_link); | 
| 58 | } | 
| 59 | $this->entityManager()->flush(); | 
| 60 | |
| 61 | // Remove strava links | 
| 62 | $strava_link_repo = $this->entityManager()->getRepository(StravaLink::class); | 
| 63 | $strava_links = $strava_link_repo->findBy(['user' => $entity]); | 
| 64 | foreach ($strava_links as $strava_link) { | 
| 65 | $this->entityManager()->remove($strava_link); | 
| 66 | } | 
| 67 | $this->entityManager()->flush(); | 
| 68 | |
| 69 | // Remove access tokens | 
| 70 | $access_token_repo = $this->entityManager()->getRepository(AccessToken::class); | 
| 71 | $access_tokens = $access_token_repo->findBy(['user' => $entity]); | 
| 72 | foreach ($access_tokens as $access_token) { | 
| 73 | $this->entityManager()->remove($access_token); | 
| 74 | } | 
| 75 | $this->entityManager()->flush(); | 
| 76 | |
| 77 | // Remove avatar | 
| 78 | $data_path = $this->envUtils()->getDataPath(); | 
| 79 | $avatar_path = "{$data_path}img/users/{$entity->getId()}"; | 
| 80 | $this->generalUtils()->removeRecursive($avatar_path); | 
| 81 | |
| 82 | // Log out | 
| 83 | if ($this->session()->get('user') === $entity->getUsername()) { | 
| 84 | $this->session()->delete('auth'); | 
| 85 | $this->session()->delete('root'); | 
| 86 | $this->session()->delete('user'); | 
| 87 | $this->session()->delete('user_id'); | 
| 88 | $this->session()->delete('auth_user'); | 
| 89 | $this->session()->delete('auth_user_id'); | 
| 90 | $this->session()->clear(); | 
| 91 | } | 
| 92 | |
| 93 | $entity->softDelete(); | 
| 94 | $this->entityManager()->flush(); | 
| 95 | |
| 96 | return []; | 
| 97 | } | 
| 98 | } |