aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Models/Quiz.php
blob: 5e2cda6a8d8a810ca8b85e16a6203164468852bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

declare(strict_types=1);

namespace Nsfisis\Albatross\Models;

use DateTimeImmutable;
use DateTimeZone;
use Nsfisis\Albatross\Exceptions\EntityValidationException;

final class Quiz
{
    public function __construct(
        public readonly int $quiz_id,
        public readonly DateTimeImmutable $created_at,
        public readonly DateTimeImmutable $started_at,
        public readonly DateTimeImmutable $ranking_hidden_at,
        public readonly DateTimeImmutable $finished_at,
        public readonly string $title,
        public readonly string $slug,
        public readonly string $description,
        public readonly string $example_code,
        public readonly ?int $birdie_code_size,
    ) {
    }

    public function isStarted(?DateTimeImmutable $now = null): bool
    {
        if ($now === null) {
            $now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
        }
        return $this->started_at <= $now;
    }

    public function isFinished(?DateTimeImmutable $now = null): bool
    {
        if ($now === null) {
            $now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
        }
        return $this->finished_at <= $now;
    }

    public function isOpenToAnswer(?DateTimeImmutable $now = null): bool
    {
        return $this->isStarted($now) && !$this->isFinished($now);
    }

    public function isClosedToAnswer(?DateTimeImmutable $now = null): bool
    {
        return !$this->isOpenToAnswer($now);
    }

    public function isRankingHidden(?DateTimeImmutable $now = null): bool
    {
        if ($now === null) {
            $now = new DateTimeImmutable('now', new DateTimeZone('UTC'));
        }
        $rankingShownAgainAt = $this->finished_at->modify('+90 minutes');
        return $this->ranking_hidden_at <= $now && $now < $rankingShownAgainAt;
    }

    public static function create(
        string $title,
        string $slug,
        string $description,
        string $example_code,
        ?int $birdie_code_size,
        DateTimeImmutable $started_at,
        DateTimeImmutable $ranking_hidden_at,
        DateTimeImmutable $finished_at,
    ): self {
        self::validate(
            $title,
            $slug,
            $description,
            $example_code,
            $birdie_code_size,
            $started_at,
            $ranking_hidden_at,
            $finished_at,
        );
        $quiz = new self(
            quiz_id: 0,
            created_at: new DateTimeImmutable(), // dummy
            started_at: $started_at,
            ranking_hidden_at: $ranking_hidden_at,
            finished_at: $finished_at,
            title: $title,
            slug: $slug,
            description: $description,
            example_code: $example_code,
            birdie_code_size: $birdie_code_size,
        );
        return $quiz;
    }

    public static function validate(
        string $title,
        string $slug,
        string $description,
        string $example_code,
        ?int $birdie_code_size,
        DateTimeImmutable $started_at,
        DateTimeImmutable $ranking_hidden_at,
        DateTimeImmutable $finished_at,
    ): void {
        $errors = [];
        if (strlen($slug) < 1) {
            $errors['slug'] = 'スラグは必須です';
        }
        if (32 < strlen($slug)) {
            $errors['slug'] = 'スラグは32文字以下である必要があります';
        }
        if (strlen($description) < 1) {
            $errors['description'] = '説明は必須です';
        }
        if (strlen($example_code) < 1) {
            $errors['example_code'] = '実装例は必須です';
        }
        if ($birdie_code_size !== null && $birdie_code_size < 1) {
            $errors['birdie_code_size'] = 'バーディになるコードサイズは 1 byte 以上である必要があります';
        }
        if ($ranking_hidden_at < $started_at) {
            $errors['ranking_hidden_at'] = 'ランキングが非表示になる日時は開始日時より後である必要があります';
        }
        if ($finished_at < $ranking_hidden_at) {
            $errors['finished_at'] = '終了日時はランキングが非表示になる日時より後である必要があります';
        }

        if (0 < count($errors)) {
            throw new EntityValidationException(errors: $errors);
        }
    }
}