Test Failed
Push — master ( 0463aa...316b3e )
by Julien
11:52
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\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 belongs to a Category.
47
     *
48
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
49
     */
50
    public function category()
51
    {
52
        return $this->belongsTo(Category::class);
53
    }
54
55
    /**
56
     * A championship belongs to a Tournament.
57
     *
58
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
59
     */
60
    public function tournament()
61
    {
62
        return $this->belongsTo(Tournament::class);
63
    }
64
65
    /**
66
     * Get All competitors from a Championships.
67
     *
68
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
69
     */
70
    public function users()
71
    {
72
        return $this->belongsToMany(User::class, 'competitor', 'championship_id')
73
            ->withPivot('confirmed')
74
            ->withTimestamps();
75
    }
76
77
    /**
78
     * A championship only has 1 Settings.
79
     *
80
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
81
     */
82
    public function settings()
83
    {
84
        return $this->hasOne(ChampionshipSettings::class);
85
    }
86
87
    /**
88
     * A championship has Many Teams.
89
     *
90
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
91
     */
92
    public function teams()
93
    {
94
        return $this->hasMany(Team::class);
95
    }
96
97
    /**
98
     * Check if Championship has Preliminary Round Configured.
99
     *
100
     * @return bool
101
     */
102
    public function hasPreliminary()
103
    {
104
        return $this->settings == null || $this->settings->hasPreliminary;
105
    }
106
107
    /**
108
     * Check if 2nd Round of Championship is Round Robin.
109
     *
110
     * @return bool
111
     */
112
    public function isPlayOffType()
113
    {
114
        return $this->settings != null && $this->settings->treeType == ChampionshipSettings::PLAY_OFF;
115
    }
116
117
    /**
118
     * Check if 2nd Round of Championship is Direct Elimination.
119
     *
120
     * @return bool
121
     */
122
    public function isDirectEliminationType()
123
    {
124
        return $this->settings == null || $this->settings->treeType == ChampionshipSettings::DIRECT_ELIMINATION;
125
    }
126
127
    /**
128
     * A championship has Many Rounds.
129
     *
130
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
131
     */
132
    public function fightersGroups()
133
    {
134
        return $this->hasMany(FightersGroup::class);
135
    }
136
137
    /**
138
     * A championship has Many fights.
139
     *
140
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
141
     */
142
    public function fights()
143
    {
144
        return $this->hasManyThrough(Fight::class, FightersGroup::class);
145
    }
146
147
    private function hasNoCustomSettings()
148
    {
149
        return
150
            ($this->settings->ageCategory == null || $this->settings->ageCategory == 0) &&
151
            $this->settings->ageMin == null &&
152
            $this->settings->ageMax == null &&
153
            $this->settings->gradeMin == null &&
154
            $this->settings->gradeMax == null &&
155
            ($this->settings->gradeCategory == null || $this->settings->gradeCategory == 0);
156
    }
157
158
    public function buildName()
159
    {
160
        if ($this->settings != null && $this->settings->alias != null && $this->settings->alias != '')
161
            return $this->settings->alias;
162
163
        if ($this->hasNoCustomSettings()) {
164
            return $this->category->name;
165
        }
166
167
        $genders = [
168
            'M' => trans('categories.male'),
169
            'F' => trans('categories.female'),
170
            'X' => trans('categories.mixt')
171
        ];
172
173
174
        $teamText = $this->category->isTeam == 1 ? trans_choice('core.team', 1) : trans('categories.single');
175
        $ageCategoryText = $this->category->getAgeString();
176
        $gradeText = $this->category->getGradeString();
177
178
        return $teamText . ' ' . $genders[$this->category->gender] . ' ' . $ageCategoryText . ' ' . $gradeText;
179
    }
180
}
181