LeaderboardTimescope::scores()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Furic\Leaderboards\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class LeaderboardTimescope extends Model
8
{
9
10
    protected $guarded = [];
11
12
    public static function findByLeaderboard($leaderboard)
13
    {
14
        $leaderboardTimescope = SELF::where('leaderboard_id', $leaderboard->id)
15
                ->whereDate('start_at', '<=', date('Y-m-d'))
16
                ->whereDate('end_at', '>=', date('Y-m-d'))
17
                ->first();
18
        if (!$leaderboardTimescope) { // Create a new leaderboard timescope entry if not found
19
            $data = [];
20
            $data['leaderboard_id'] = $leaderboard->id;
21
            switch ($leaderboard->timescope) {
22
                case 1: // Daily
23
                    $data['start_at'] = date('Y-m-d');
24
                    $data['end_at'] = date('Y-m-d');
25
                    break;
26
                case 2: // Weekly
27
                    $data['start_at'] = date('Y-m-d', strtotime('monday this week'));
28
                    $data['end_at'] = date('Y-m-d', strtotime('sunday this week'));
29
                    break;
30
                case 3: // Monthly
31
                    $data['start_at'] = date('Y-m-01');
32
                    $data['end_at'] = date('Y-m-t');
33
                    break;
34
                default:
35
                case 0: // All time
36
                    $data['start_at'] = date('Y-m-d');
37
                    $data['end_at'] = date('2099-12-31');
38
                    break;
39
            }
40
            $previousLeaderboardTimescope = SELF::where('start_at', '<', $data['start_at'])->orderBy('start_at', 'desc')->first();
41
            if ($previousLeaderboardTimescope) {
42
                $data['previous_id'] = $previousLeaderboardTimescope->id;
43
            }
44
            $leaderboardTimescope = SELF::create($data);
45
        }
46
        return $leaderboardTimescope;
47
    }
48
49
    public function leaderboard()
50
    {
51
        return $this->belongsTo(Leaderboard::class);
52
    }
53
54
    public function scores()
55
    {
56
        return $this->hasMany(LeaderboardScore::class);
57
    }
58
    
59
    public function highscores()
60
    {
61
        return $this->hasMany(LeaderboardScore::class);
62
    }
63
    
64
    public function orderedScoresByScoreSum()
65
    {
66
        return $this->hasMany(LeaderboardScore::class)->orderBy('score_sum', 'desc');
67
    }
68
    
69
    public function orderedScoresByHighscore()
70
    {
71
        return $this->hasMany(LeaderboardScore::class)->orderBy('highscore', 'desc');
72
    }
73
    
74
    public function previous()
75
    {
76
        return SELF::find($this->previous_id);
77
    }
78
79
}