Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
RequestEmailChangeAction
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace App\Action\Auth;
6
7use App\Domain\Auth\Service\EmailChangeService;
8use App\Renderer\JsonRenderer;
9use App\Support\AppUrlResolver;
10use App\Support\Row;
11use Psr\Http\Message\ResponseInterface;
12use Psr\Http\Message\ServerRequestInterface;
13
14/**
15 * POST /api/auth/change-email
16 *
17 * FSC-86: lets an authenticated investor request a change to their own email
18 * address. Requires the current password (re-authentication) and emails a
19 * verification link to the new address — the change is only applied once the
20 * link is confirmed (ConfirmEmailChangeAction). Login is by username and
21 * accounts are keyed by investor_id, so the change never affects either.
22 *
23 * Body: { currentPassword, newEmail }
24 */
25final readonly class RequestEmailChangeAction
26{
27    /**
28     * @param array<string, mixed> $emailConfig
29     * @param EmailChangeService $emailChangeService
30     * @param JsonRenderer $renderer
31     */
32    public function __construct(
33        private EmailChangeService $emailChangeService,
34        private JsonRenderer $renderer,
35        private array $emailConfig,
36    ) {}
37
38    public function __invoke(
39        ServerRequestInterface $request,
40        ResponseInterface $response,
41    ): ResponseInterface {
42        $attributes = $request->getAttributes();
43        $userId = Row::int($attributes, 'userId');
44        $investorId = Row::nullableInt($attributes, 'investorId');
45
46        $data = (array)$request->getParsedBody();
47        $currentPassword = Row::nullableString($data, 'currentPassword') ?? '';
48        $newEmail = Row::nullableString($data, 'newEmail') ?? '';
49
50        $appUrl = AppUrlResolver::resolve($request, Row::nullableString($this->emailConfig, 'app_url'));
51
52        $this->emailChangeService->requestChange($userId, $investorId, $currentPassword, $newEmail, $appUrl);
53
54        return $this->renderer->json($response, [
55            'success' => true,
56            'message' => 'Check your new email address for a link to confirm the change.',
57        ]);
58    }
59}