Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
TransferTaskDTO
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 fromRequest
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Domain\TransferTask\DTOs;
4
5use App\Models\Address\Address;
6use App\Models\Product\Product;
7use App\Models\Team\Team;
8use App\Models\User;
9use Domain\Shared\Interfaces\DataTransferObjectInterface;
10use Illuminate\Foundation\Http\FormRequest;
11use Illuminate\Support\Collection;
12
13readonly class TransferTaskDTO implements DataTransferObjectInterface
14{
15    public ?string $observation;
16    public ?User $responsible;
17    public Team $team;
18    /** @var Collection[TransferTaskProductDTO] */
19    public Collection $products;
20
21    public function __construct(array $args)
22    {
23        $this->team = $args['team'];
24        $this->products = $args['transfer_task_products'];
25        $this->responsible = $args['responsible'] ?? null;
26        $this->observation = $args['observation'] ?? null;
27    }
28
29    public function toArray(): array
30    {
31        return [
32            'observation' => $this->observation,
33            'responsible_id' => $this->responsible?->getKey(),
34            'team_id' => $this->team->getKey(),
35            'transfer_task_products' => $this->products->map(fn(TransferTaskProductDTO $transferTaskProductDTO) => $transferTaskProductDTO->toArray())->toArray(),
36        ];
37    }
38
39    public static function fromRequest(FormRequest $request): TransferTaskDTO
40    {
41        $products = array_map(
42            fn(array $product) => new TransferTaskProductDTO([
43                'product' => Product::find($product['product_id']),
44                'suggestion_amount' => $product['suggestion_amount'] ?? null,
45                'suggestion_origin' => Address::find($product['suggestion_origin_id'] ?? null),
46                'suggestion_destination' => Address::find($product['suggestion_destination_id'] ?? null),
47            ]),
48            $request->validated('transfer_task_products')
49        );
50
51        return new self([
52            'team' => Team::find($request->validated('team_id')),
53            'responsible' => User::find($request->validated('responsible_id')),
54            'observation' => $request->validated('observation'),
55            'transfer_task_products' => new Collection($products),
56        ]);
57    }
58}