SessionValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
c 0
b 0
f 0
dl 0
loc 81
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A sessionDateExists() 0 6 1
A validateItem() 0 33 4
A validateVenue() 0 12 2
1
<?php
2
3
namespace Siak\Tontine\Validation\Guild;
4
5
use Illuminate\Support\Facades\Validator;
6
use Siak\Tontine\Service\TenantService;
7
use Siak\Tontine\Validation\AbstractValidator;
8
use Siak\Tontine\Validation\ValidationException;
9
10
use function substr;
11
12
class SessionValidator extends AbstractValidator
13
{
14
    /**
15
     * @param TenantService $tenantService
16
     */
17
    public function __construct(protected TenantService $tenantService)
18
    {}
19
20
    /**
21
     * Check duplicates in session date
22
     *
23
     * @param string $startAt
24
     * @param int $sessionId
25
     *
26
     * @return bool
27
     */
28
    private function sessionDateExists(string $startAt, int $sessionId): bool
29
    {
30
        return $this->tenantService->guild()->sessions()
31
            ->where('sessions.id', '!=', $sessionId)
32
            ->where('sessions.day_date', $startAt)
33
            ->first() !== null;
34
    }
35
36
    /**
37
     * @param array $values
38
     *
39
     * @return array
40
     */
41
    public function validateItem(array $values): array
42
    {
43
        $values['start_time'] = substr($values['start_time'], 0, 5);
44
        $values['end_time'] = substr($values['end_time'], 0, 5);
45
        $validator = Validator::make($this->values($values), [
46
            'title' => 'required|string|min:1',
47
            'day_date' => 'required|date_format:Y-m-d',
48
            'start_time' => 'required|date_format:H:i',
49
            'end_time' => 'required|date_format:H:i',
50
            'host_id' => 'integer|min:0',
51
        ]);
52
        $validator->after(function($validator) use($values) {
53
            if($this->sessionDateExists($values['day_date'], $values['id'] ?? 0))
54
            {
55
                $validator->errors()->add('day_date', trans('tontine.session.errors.date_dup'));
56
            }
57
        });
58
        if($validator->fails())
59
        {
60
            throw new ValidationException($validator);
61
        }
62
63
        $validated = $validator->validated();
64
        // Make sure the host belongs to the same guild
65
        $hostId = (int)$validated['host_id'];
66
        $validated['host_id'] = null;
67
        if($hostId > 0)
68
        {
69
            $validated['host_id'] = $this->tenantService->round()
70
                ->members()->find($hostId)?->id ?? null;
71
        }
72
73
        return $validated;
74
    }
75
76
    /**
77
     * @param array $values
78
     *
79
     * @return array
80
     */
81
    public function validateVenue(array $values): array
82
    {
83
        $validator = Validator::make($this->values($values), [
84
            'venue' => 'nullable|string',
85
            'notes' => 'nullable|string',
86
        ]);
87
        if($validator->fails())
88
        {
89
            throw new ValidationException($validator);
90
        }
91
92
        return $validator->validated();
93
    }
94
}
95