Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| SetMyAvatarController | |
100.00% |
11 / 11 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Identity\Infrastructure\Controllers; |
| 6 | |
| 7 | use App\Identity\Application\Command\ChangeMyAvatarCommand; |
| 8 | use App\Identity\Application\Command\ChangeMyAvatarHandler; |
| 9 | use App\Identity\Domain\Exceptions\UserNotFound; |
| 10 | use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser; |
| 11 | use InvalidArgumentException; |
| 12 | use OpenApi\Attributes as OA; |
| 13 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 14 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 15 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 16 | use Symfony\Component\HttpFoundation\Request; |
| 17 | use Symfony\Component\Routing\Attribute\Route; |
| 18 | |
| 19 | #[OA\Tag(name: 'Users')] |
| 20 | class SetMyAvatarController extends AbstractController |
| 21 | { |
| 22 | use ResolvesAuthenticatedUser; |
| 23 | |
| 24 | public function __construct( |
| 25 | private readonly ChangeMyAvatarHandler $handler, |
| 26 | ) { |
| 27 | } |
| 28 | |
| 29 | /** Multipart upload with a single `file` part; replaces the current avatar. */ |
| 30 | #[Route('/users/me/avatar', name: 'users_avatar_set', methods: ['POST'])] |
| 31 | public function set(Request $request): JsonResponse |
| 32 | { |
| 33 | $securityUser = $this->securityUser(); |
| 34 | |
| 35 | $file = $request->files->get('file'); |
| 36 | if (!$file instanceof UploadedFile) { |
| 37 | return new JsonResponse(['error' => 'Missing <file> upload.'], 400); |
| 38 | } |
| 39 | |
| 40 | try { |
| 41 | $view = $this->handler->handle(new ChangeMyAvatarCommand($securityUser->userId(), $file)); |
| 42 | } catch (UserNotFound $e) { |
| 43 | return new JsonResponse(['error' => $e->getMessage()], 404); |
| 44 | } catch (InvalidArgumentException $e) { |
| 45 | return new JsonResponse(['error' => $e->getMessage()], 400); |
| 46 | } |
| 47 | |
| 48 | return new JsonResponse($view->toArray(), 200); |
| 49 | } |
| 50 | } |