Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.48% covered (warning)
81.48%
22 / 27
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
StoreImportRequest
81.48% covered (warning)
81.48%
22 / 27
66.67% covered (warning)
66.67%
2 / 3
11.77
0.00% covered (danger)
0.00%
0 / 1
 authorize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 rules
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 withValidator
68.75% covered (warning)
68.75%
11 / 16
0.00% covered (danger)
0.00%
0 / 1
11.47
1<?php
2
3declare(strict_types=1);
4
5namespace ImovelZapAi\Importing\Presentation\Http\Requests;
6
7use Illuminate\Foundation\Http\FormRequest;
8
9final class StoreImportRequest extends FormRequest
10{
11    public function authorize(): bool
12    {
13        return true;
14    }
15
16    /**
17     * @return array<string, mixed>
18     */
19    public function rules(): array
20    {
21        $maxKb = (int) ceil(((int) config('importing.max_upload_bytes', 20 * 1024 * 1024)) / 1024);
22
23        return [
24            'file' => [
25                'required',
26                'file',
27                'max:'.$maxKb,
28                'mimetypes:text/xml,application/xml,application/octet-stream',
29            ],
30            'idempotency_key' => ['nullable', 'string', 'max:100'],
31        ];
32    }
33
34    public function withValidator($validator): void
35    {
36        $validator->after(function ($validator): void {
37            $file = $this->file('file');
38            if ($file === null) {
39                return;
40            }
41
42            $path = $file->getRealPath();
43            if ($path === false) {
44                $validator->errors()->add('file', 'Arquivo inválido.');
45
46                return;
47            }
48
49            $head = (string) file_get_contents($path, false, null, 0, 512);
50            if (! str_contains($head, '<') || (! str_contains($head, 'ListingDataFeed') && ! str_contains($head, 'Listing'))) {
51                // Allow any XML-looking content; parser will validate VRSync structure.
52            }
53
54            if (preg_match('/<!DOCTYPE|<!ENTITY/i', $head) === 1) {
55                $validator->errors()->add('file', 'XML com DOCTYPE/ENTITY não é permitido.');
56            }
57
58            $extension = strtolower($file->getClientOriginalExtension());
59            if (! in_array($extension, ['xml', ''], true) && $extension !== '') {
60                $validator->errors()->add('file', 'Envie um arquivo .xml VRSync.');
61            }
62        });
63    }
64}