Draw.php 1.97 KB
Newer Older
1
2
<?php

3
namespace App\Models;
4

Korko's avatar
Korko committed
5
use App\Casts\EncryptedString;
Korko's avatar
Korko committed
6
7
8
use DateInterval;
use DateTime;
use DB;
Korko's avatar
Korko committed
9
use Illuminate\Database\Eloquent\Model;
10
11
12

class Draw extends Model
{
Korko's avatar
Korko committed
13
    use HashId {
Korko's avatar
Korko committed
14
        resolveRouteBinding as resolveHash;
Korko's avatar
Korko committed
15
    }
16

17
    protected $hashConnection = 'draw';
18

Korko's avatar
Korko committed
19
    /**
20
     * The attributes that should be cast.
Korko's avatar
Korko committed
21
22
23
     *
     * @var array
     */
24
25
26
27
    protected $casts = [
        'mail_title' => EncryptedString::class,
        'mail_body' => EncryptedString::class,
    ];
Korko's avatar
Korko committed
28

29
    /**
30
     * The attributes that should be mutated to dates.
31
32
33
     *
     * @var array
     */
Korko's avatar
Korko committed
34
35
36
37
    protected $dates = [
        'expires_at',
    ];

Korko's avatar
Korko committed
38
    /**
39
     * The attributes that are mass assignable.
Korko's avatar
Korko committed
40
41
42
     *
     * @var array
     */
43
    protected $fillable = ['mail_title', 'mail_body', 'expires_at'];
Korko's avatar
Korko committed
44

Korko's avatar
Korko committed
45
    public function save(array $options = [])
46
    {
Korko's avatar
Korko committed
47
        $this->expires_at = $this->expires_at ?: (new DateTime('now'))->add(new DateInterval('P7D'));
Korko's avatar
Korko committed
48
49

        return parent::save($options);
50
51
    }

Korko's avatar
Korko committed
52
    public static function cleanup()
53
    {
54
        // Recap is sent 2 days after so remove everything 3 days after
Korko's avatar
Korko committed
55
        self::where('expires_at', '<=', (new DateTime('now'))->sub(new DateInterval('P3D')))->delete();
56
57
    }

Korko's avatar
Korko committed
58
    public function participants()
59
    {
Korko's avatar
Korko committed
60
        return $this->hasMany(Participant::class)->with('mail');
61
62
    }

Korko's avatar
Korko committed
63
    public function getOrganizerAttribute()
64
    {
Korko's avatar
Korko committed
65
        return $this->participants->first();
66
    }
Korko's avatar
Korko committed
67
68
69

    public function getExpiredAttribute()
    {
70
        return $this->expires_at->isPast();
Korko's avatar
Korko committed
71
72
73
74
75
76
77
78
79
80
81
    }

    /**
     * Retrieve the model for a bound value.
     *
     * @param  mixed  $value
     * @param  string|null  $field
     * @return \Illuminate\Database\Eloquent\Model|null
     */
    public function resolveRouteBinding($value, $field = null)
    {
Korko's avatar
Korko committed
82
83
84
        $draw = $field === 'hash' ?
            $this->resolveHash($value) :
            parent::resolveRouteBinding($value, $field);
Korko's avatar
Korko committed
85

Korko's avatar
Korko committed
86
        abort_if($draw === null || $draw->expired, 404);
Korko's avatar
Korko committed
87

Korko's avatar
Korko committed
88
        return $draw;
Korko's avatar
Korko committed
89
    }
90
}