Passed
Push — master ( f45f58...80989d )
by Julien
21:56
created

Championship::teams()   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\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
    private function hasNoCustomSettings()
158
    {
159
        return
160
            $this->settings == null ||
161
            ($this->settings->ageCategory == null || $this->settings->ageCategory == 0) &&
162
            $this->settings->ageMin == null &&
163
            $this->settings->ageMax == null &&
164
            $this->settings->gradeMin == null &&
165
            $this->settings->gradeMax == null &&
166
            ($this->settings->gradeCategory == null || $this->settings->gradeCategory == 0);
167
    }
168
169
    public function buildName()
170
    {
171
        if ($this->settings != null && $this->settings->alias != null && $this->settings->alias != '')
172
            return $this->settings->alias;
173
174
        if ($this->hasNoCustomSettings()) {
175
            return $this->category->name;
176
        }
177
178
        $genders = [
179
            'M' => trans('categories.male'),
180
            'F' => trans('categories.female'),
181
            'X' => trans('categories.mixt')
182
        ];
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
        return $this->getSettings ?? new ChampionshipSettings(config('options.default_settings'));
194
    }
195
196
}
197