Test Failed
Push — master ( 414239...447ec5 )
by Julien
03:43
created

Championship::buildName()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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