Test Failed
Push — master ( 84ef8e...92b331 )
by Julien
08:45
created

Championship::hasNoCustomSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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