Passed
Push — master ( 2276ad...f60cc9 )
by Julien
20:48
created

Championship::firstRoundFights()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Xoco70\KendoTournaments\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use Illuminate\Foundation\Auth\User;
8
9
class Championship extends Model
10
{
11
    use SoftDeletes;
12
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];
13
    protected $table = 'championship';
14
15
    public $timestamps = true;
16
    protected $fillable = [
17
        'tournament_id',
18
        'category_id',
19
    ];
20
21
    protected static function boot()
22
    {
23
        parent::boot();
24
25
        static::deleting(function ($championship) {
26
            $championship->competitors()->delete();
27
            $championship->settings()->delete();
28
        });
29
        static::restoring(function ($championship) {
30
            $championship->competitors()->restore();
31
            $championship->settings()->restore();
32
        });
33
    }
34
35
    /**
36
     * A championship has many Competitors.
37
     *
38
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
39
     */
40
    public function competitors()
41
    {
42
        return $this->hasMany(Competitor::class);
43
    }
44
45
    /**
46
     * A championship has many Competitors.
47
     *
48
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
49
     */
50
    public function competitorsWithUser()
51
    {
52
        return $this->hasMany(Competitor::class)->with('user');
53
    }
54
55
    /**
56
     * A championship belongs to a Category.
57
     *
58
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
59
     */
60
    public function category()
61
    {
62
        return $this->belongsTo(Category::class);
63
    }
64
65
    /**
66
     * A championship belongs to a Tournament.
67
     *
68
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
69
     */
70
    public function tournament()
71
    {
72
        return $this->belongsTo(Tournament::class);
73
    }
74
75
    /**
76
     * Get All competitors from a Championships.
77
     *
78
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
79
     */
80
    public function users()
81
    {
82
        return $this->belongsToMany(User::class, 'competitor', 'championship_id')
83
            ->withPivot('confirmed')
84
            ->withTimestamps();
85
    }
86
87
    /**
88
     * A championship only has 1 Settings.
89
     *
90
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
91
     */
92
    public function settings()
93
    {
94
        return $this->hasOne(ChampionshipSettings::class);
95
    }
96
97
    /**
98
     * A championship has Many Teams.
99
     *
100
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
101
     */
102
    public function teams()
103
    {
104
        return $this->hasMany(Team::class);
105
    }
106
107
    /**
108
     * Check if Championship has Preliminary Round Configured.
109
     *
110
     * @return bool
111
     */
112
    public function hasPreliminary()
113
    {
114
        return $this->settings == null || $this->settings->hasPreliminary;
115
    }
116
117
    /**
118
     * Check if 2nd Round of Championship is Round Robin.
119
     *
120
     * @return bool
121
     */
122
    public function isPlayOffType()
123
    {
124
        return $this->settings != null && $this->settings->treeType == ChampionshipSettings::PLAY_OFF;
125
    }
126
127
    /**
128
     * Check if 2nd Round of Championship is Direct Elimination.
129
     *
130
     * @return bool
131
     */
132
    public function isDirectEliminationType()
133
    {
134
        return $this->settings != null && $this->settings->treeType == ChampionshipSettings::DIRECT_ELIMINATION;
135
    }
136
137
    /**
138
     * A championship has Many Rounds.
139
     *
140
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
141
     */
142
    public function fightersGroups()
143
    {
144
        return $this->hasMany(FightersGroup::class);
145
    }
146
147
    /**
148
     * A championship has Many fights.
149
     *
150
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
151
     */
152
    public function fights()
153
    {
154
        return $this->hasManyThrough(Fight::class, FightersGroup::class);
155
    }
156
157
    /**
158
     *
159
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
160
     */
161
    public function firstRoundFights()
162
    {
163
        return $this->hasManyThrough(Fight::class, FightersGroup::class)
164
            ->where('fighters_groups.round', 1);
165
    }
166
167
    private function hasNoCustomSettings()
168
    {
169
        return
170
            $this->settings == null ||
171
            ($this->settings->ageCategory == null || $this->settings->ageCategory == 0) &&
172
            $this->settings->ageMin == null &&
173
            $this->settings->ageMax == null &&
174
            $this->settings->gradeMin == null &&
175
            $this->settings->gradeMax == null &&
176
            ($this->settings->gradeCategory == null || $this->settings->gradeCategory == 0);
177
    }
178
179
    public function buildName()
180
    {
181
        if ($this->settings != null && $this->settings->alias != null && $this->settings->alias != '')
182
            return $this->settings->alias;
183
184
        if ($this->hasNoCustomSettings()) {
185
            return $this->category->name;
186
        }
187
188
        $genders = [
189
            'M' => trans('categories.male'),
190
            'F' => trans('categories.female'),
191
            'X' => trans('categories.mixt')
192
        ];
193
194
195
        $teamText = $this->category->isTeam == 1 ? trans_choice('core.team', 1) : trans('categories.single');
196
        $ageCategoryText = $this->category->getAgeString();
197
        $gradeText = $this->category->getGradeString();
198
199
        return $teamText . ' ' . $genders[$this->category->gender] . ' ' . $ageCategoryText . ' ' . $gradeText;
200
    }
201
202
    public function getSettings()
203
    {
204
        return $this->settings ?? new ChampionshipSettings(config('options.default_settings'));
205
    }
206
207
}
208