Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
7 / 8 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DownloadMyProvidedDocumentAction | |
87.50% |
7 / 8 |
|
50.00% |
1 / 2 |
3.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Action\Document\Provided; |
| 6 | |
| 7 | use App\Domain\Document\Provided\Service\ProvidedDocumentService; |
| 8 | use App\Domain\Exception\ForbiddenException; |
| 9 | use App\Support\ProvidedDocumentDownload; |
| 10 | use App\Support\Row; |
| 11 | use Psr\Http\Message\ResponseInterface; |
| 12 | use Psr\Http\Message\ServerRequestInterface; |
| 13 | |
| 14 | /** |
| 15 | * Investor downloads a document an admin provided to them (FSC-57). |
| 16 | * |
| 17 | * Ownership is enforced in the service (404 if the document isn't the |
| 18 | * caller's) before the storage layer is touched. |
| 19 | */ |
| 20 | final readonly class DownloadMyProvidedDocumentAction |
| 21 | { |
| 22 | public function __construct( |
| 23 | private ProvidedDocumentService $service, |
| 24 | ) {} |
| 25 | |
| 26 | /** |
| 27 | * @param array<string, string> $args |
| 28 | * @param ServerRequestInterface $request |
| 29 | * @param ResponseInterface $response |
| 30 | */ |
| 31 | public function __invoke( |
| 32 | ServerRequestInterface $request, |
| 33 | ResponseInterface $response, |
| 34 | array $args, |
| 35 | ): ResponseInterface { |
| 36 | $investorId = Row::nullableInt($request->getAttributes(), 'investorId'); |
| 37 | if ($investorId === null) { |
| 38 | throw new ForbiddenException('This endpoint requires an investor account.'); |
| 39 | } |
| 40 | |
| 41 | $providedDocumentId = (int)$args['id']; |
| 42 | |
| 43 | $document = $this->service->getForInvestor($investorId, $providedDocumentId); |
| 44 | $stream = $this->service->getStream($document->fileStorageKey); |
| 45 | |
| 46 | return ProvidedDocumentDownload::respond($response, $document, $stream); |
| 47 | } |
| 48 | } |