Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| BorrowBookRequest | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromHttpRequest | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| toArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace AppDemo\Loan\Presentation\Request; |
| 11 | |
| 12 | use AppDemo\Loan\Domain\Exception\InvalidLoanPeriodException; |
| 13 | use AppDemo\Loan\Domain\LoanPeriod; |
| 14 | use InvalidArgumentException; |
| 15 | use Psr\Http\Message\ServerRequestInterface; |
| 16 | |
| 17 | final readonly class BorrowBookRequest |
| 18 | { |
| 19 | private function __construct( |
| 20 | public string $bookId, |
| 21 | public string $loanPeriod |
| 22 | ) {} |
| 23 | |
| 24 | public static function fromHttpRequest(ServerRequestInterface $request): self |
| 25 | { |
| 26 | $data = (array) $request->getParsedBody(); |
| 27 | |
| 28 | return new self( |
| 29 | (string) ($data['book_id'] ?? ''), |
| 30 | (string) ($data['loan_period'] ?? '14') |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | public function validate(): array |
| 35 | { |
| 36 | $errors = []; |
| 37 | |
| 38 | try { |
| 39 | LoanPeriod::fromInt((int) $this->loanPeriod); |
| 40 | } catch (InvalidArgumentException|InvalidLoanPeriodException $e) { |
| 41 | $errors['loan_period'] = $e->getMessage(); |
| 42 | } |
| 43 | |
| 44 | return $errors; |
| 45 | } |
| 46 | |
| 47 | public function toArray(): array |
| 48 | { |
| 49 | return [ |
| 50 | 'book_id' => $this->bookId, |
| 51 | 'loan_period' => $this->loanPeriod, |
| 52 | ]; |
| 53 | } |
| 54 | } |