Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RunEndpointTrait | |
0.00% |
0 / 28 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
| getEntityData | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| updateEntityWithData | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| getEntityById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Anniversary\Endpoints; |
| 4 | |
| 5 | use Olz\Entity\Anniversary\RunRecord; |
| 6 | use Olz\Entity\Users\User; |
| 7 | use Olz\Utils\WithUtilsTrait; |
| 8 | use PhpTypeScriptApi\HttpError; |
| 9 | use PhpTypeScriptApi\PhpStan\IsoDateTime; |
| 10 | |
| 11 | /** |
| 12 | * @phpstan-type OlzRunId int |
| 13 | * @phpstan-type OlzRunData array{ |
| 14 | * userId?: ?int, |
| 15 | * runAt?: ?IsoDateTime, |
| 16 | * distanceMeters: int, |
| 17 | * elevationMeters: int, |
| 18 | * source?: ?non-empty-string, |
| 19 | * } |
| 20 | */ |
| 21 | trait RunEndpointTrait { |
| 22 | use WithUtilsTrait; |
| 23 | |
| 24 | /** @return OlzRunData */ |
| 25 | public function getEntityData(RunRecord $entity): array { |
| 26 | $run_at = IsoDateTime::fromDateTime($entity->getRunAt()); |
| 27 | $this->generalUtils()->checkNotNull($run_at, "Invalid run_at: {$entity}"); |
| 28 | return [ |
| 29 | 'userId' => $entity->getUser()?->getId(), |
| 30 | 'runAt' => $run_at, |
| 31 | 'distanceMeters' => $entity->getDistanceMeters(), |
| 32 | 'elevationMeters' => $entity->getElevationMeters(), |
| 33 | 'source' => $entity->getSource() ?: null, |
| 34 | ]; |
| 35 | } |
| 36 | |
| 37 | /** @param OlzRunData $input_data */ |
| 38 | public function updateEntityWithData(RunRecord $entity, array $input_data): void { |
| 39 | $now = new \DateTime($this->dateUtils()->getIsoNow()); |
| 40 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 41 | $user = $user_repo->findOneBy(['id' => $input_data['userId'] ?? null]); |
| 42 | if ($user === null) { |
| 43 | $user = $this->authUtils()->getCurrentUser(); |
| 44 | } |
| 45 | $last_name = substr($user?->getLastName() ?? '', 0, 1)."."; |
| 46 | $name = "{$user?->getFirstName()} {$last_name}"; |
| 47 | |
| 48 | $entity->setUser($user); |
| 49 | $entity->setRunnerName($name); |
| 50 | $entity->setRunAt($input_data['runAt'] ?? $now); |
| 51 | $entity->setIsCounting(true); |
| 52 | $entity->setDistanceMeters($input_data['distanceMeters']); |
| 53 | $entity->setElevationMeters($input_data['elevationMeters']); |
| 54 | $entity->setSource($input_data['source'] ?? 'manuell'); |
| 55 | } |
| 56 | |
| 57 | protected function getEntityById(int $id): RunRecord { |
| 58 | $repo = $this->entityManager()->getRepository(RunRecord::class); |
| 59 | $entity = $repo->findOneBy(['id' => $id]); |
| 60 | if (!$entity) { |
| 61 | throw new HttpError(404, "Nicht gefunden."); |
| 62 | } |
| 63 | return $entity; |
| 64 | } |
| 65 | } |