Passed
Branch master (8db9fa)
by Julien
24:59
created

Championship::buildName()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
205
    }
206
207
    /**
208
     * Return Groups that belongs to the round in param
209
     * @param $round
210
     * @return Collection
211
     */
212
    public function groupsByRound($round) : Collection
213
    {
214
        return $this->fightersGroups()->where('round',$round)->get();
215
    }
216
}
217