Test Failed
Push — master ( b6687a...d5e75a )
by Julien
06:32
created

Championship::tournament()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Xoco70\LaravelTournaments\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
use Illuminate\Foundation\Auth\User;
9
use Xoco70\LaravelTournaments\Contracts\TreeGenerable;
10
use Xoco70\LaravelTournaments\TreeGen\DirectEliminationCompetitorTreeGen;
11
use Xoco70\LaravelTournaments\TreeGen\DirectEliminationTeamTreeGen;
12
use Xoco70\LaravelTournaments\TreeGen\PlayOffCompetitorTreeGen;
13
use Xoco70\LaravelTournaments\TreeGen\PlayOffTeamTreeGen;
14
15
class Championship extends Model
16
{
17
    use SoftDeletes;
18
    protected $dates = ['created_at', 'updated_at', 'deleted_at'];
19
    protected $table = 'championship';
20
21
    public $timestamps = true;
22
    protected $fillable = [
23
        'tournament_id',
24
        'category_id',
25
    ];
26
27
    protected static function boot()
28
    {
29
        parent::boot();
30
31
        static::deleting(function ($championship) {
32
            $championship->competitors()->delete();
33
            $championship->settings()->delete();
34
        });
35
        static::restoring(function ($championship) {
36
            $championship->competitors()->restore();
37
            $championship->settings()->restore();
38
        });
39
    }
40
41
    /**
42
     * A championship has many Competitors.
43
     *
44
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
45
     */
46
    public function competitors()
47
    {
48
        return $this->hasMany(Competitor::class);
49
    }
50
51
    public function fighters()
52
    {
53
        if ($this->category->isTeam) {
54
            return $this->hasMany(Team::class);
55
        }
56
        return $this->hasMany(Competitor::class);
57
    }
58
59
    /**
60
     * A championship belongs to a Category.
61
     *
62
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
63
     */
64
    public function category()
65
    {
66
        return $this->belongsTo(Category::class);
67
    }
68
69
    /**
70
     * A championship belongs to a Tournament.
71
     *
72
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
73
     */
74
    public function tournament()
75
    {
76
        return $this->belongsTo(Tournament::class);
77
    }
78
79
    /**
80
     * Get All competitors from a Championships.
81
     *
82
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
83
     */
84
    public function users()
85
    {
86
        return $this->belongsToMany(User::class, 'competitor', 'championship_id')
87
            ->withPivot('confirmed')
88
            ->withTimestamps();
89
    }
90
91
    /**
92
     * A championship only has 1 Settings.
93
     *
94
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
95
     */
96
    public function settings()
97
    {
98
        return $this->hasOne(ChampionshipSettings::class);
99
    }
100
101
    /**
102
     * A championship has Many Teams.
103
     *
104
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
105
     */
106
    public function teams()
107
    {
108
        return $this->hasMany(Team::class);
109
    }
110
111
    /**
112
     * Check if Championship has Preliminary Round Configured.
113
     *
114
     * @return bool
115
     */
116
    public function hasPreliminary()
117
    {
118
        return $this->settings == null || $this->settings->hasPreliminary;
119
    }
120
121
    /**
122
     * Check if 2nd Round of Championship is Round Robin.
123
     *
124
     * @return bool
125
     */
126
    public function isPlayOffType()
127
    {
128
        return $this->settings != null && $this->settings->treeType == ChampionshipSettings::PLAY_OFF;
129
    }
130
131
    /**
132
     * Check if 2nd Round of Championship is Direct Elimination.
133
     *
134
     * @return bool
135
     */
136
    public function isDirectEliminationType()
137
    {
138
        return $this->settings != null && $this->settings->treeType == ChampionshipSettings::DIRECT_ELIMINATION;
139
    }
140
141
    /**
142
     * A championship has Many Groups of Fighters.
143
     *
144
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
145
     */
146
    public function fightersGroups()
147
    {
148
        return $this->hasMany(FightersGroup::class);
149
    }
150
151
    /**
152
     * A championship has Many fights.
153
     *
154
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
155
     */
156
    public function fights()
157
    {
158
        return $this->hasManyThrough(Fight::class, FightersGroup::class);
159
    }
160
161
    /**
162
     * Get the fights that happen to the first round
163
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
164
     */
165
    public function firstRoundFights()
166
    {
167
        return $this->hasManyThrough(Fight::class, FightersGroup::class)
168
            ->where('fighters_groups.round', 1);
169
    }
170
171
    private function hasNoCustomSettings()
172
    {
173
        return $this->settings == null;
174
    }
175
176
    public function buildName()
177
    {
178
        if ($this->settings != null && $this->settings->alias != null)
179
            return $this->settings->alias;
180
181
        if ($this->hasNoCustomSettings()) {
182
            return $this->category->name;
183
        }
184
185
        $genders = [
186
            'M' => trans('categories.male'),
187
            'F' => trans('categories.female'),
188
            'X' => trans('categories.mixt')
189
        ];
190
191
        $teamText = $this->category->isTeam == 1 ? trans_choice('core.team', 1) : trans('categories.single');
192
        $ageCategoryText = $this->category->getAgeString();
193
        $gradeText = $this->category->getGradeString();
194
195
        return $teamText . ' ' . $genders[$this->category->gender] . ' ' . $ageCategoryText . ' ' . $gradeText;
196
    }
197
198
    public function getSettings()
199
    {
200
        return $this->settings ?? new ChampionshipSettings(ChampionshipSettings::DEFAULT_SETTINGS);
201
    }
202
203
    /**
204
     * Return Groups that belongs to a round
205
     * @param integer $round
206
     * @return HasMany
207
     */
208
    public function groupsByRound($round)
209
    {
210
        return $this->fightersGroups()->where('round', $round);
211
    }
212
213
    /**
214
     * Return Groups that belongs to a round
215
     * @param integer $round
216
     * @return HasMany
217
     */
218
    public function groupsFromRound($round)
219
    {
220
        return $this->fightersGroups()->where('round', '>=', $round);
221
    }
222
223
    /**
224
     * Return Fights that belongs to a round
225
     *
226
     * @param integer $round
227
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
228
     */
229
    public function fightsByRound($round)
230
    {
231
        return $this->hasManyThrough(Fight::class, FightersGroup::class)->where('round', $round);
232
    }
233
234
    public function isPlayoffCompetitor()
235
    {
236
        return !$this->category->isTeam() && $this->isPlayOffType() || $this->hasPreliminary();
237
    }
238
239
    public function isPlayoffTeam()
240
    {
241
        return $this->category->isTeam() && $this->isPlayOffType();
242
    }
243
244
    public function isDirectEliminationCompetitor()
245
    {
246
        return !$this->category->isTeam() && $this->isDirectEliminationType();
247
    }
248
249
    public function isDirectEliminationTeam()
250
    {
251
        return $this->category->isTeam() && $this->isDirectEliminationType();
252
    }
253
254
    /**
255
     * @return TreeGenerable
256
     */
257
    public function chooseGenerationStrategy()
258
    {
259
        switch (true) {
260
            case $this->isDirectEliminationCompetitor():
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
261
                $generation = new DirectEliminationCompetitorTreeGen($this, null);
262
                break;
263
            case $this->isDirectEliminationTeam():
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
264
                $generation = new DirectEliminationTeamTreeGen($this, null);
265
                break;
266
            case $this->isPlayoffCompetitor():
267
                $generation = new PlayOffCompetitorTreeGen($this, null);
268
                break;
269
            case $this->isPlayoffTeam():
270
                $generation = new PlayOffTeamTreeGen($this, null);
271
                break;
272
            default:
273
                $generation = new PlayOffCompetitorTreeGen($this, null);
274
        }
275
        return $generation;
276
    }
277
278
    /**
279
     * @return int
280
     */
281
    public function getGroupSize()
282
    {
283
        if ($this->hasPreliminary()) {
284
            return $this->getSettings()->preliminaryGroupSize;
285
        }
286
        return 2;
287
    }
288
}
289