Test Failed
Push — master ( b6687a...d5e75a )
by Julien
06:32
created

Tournament   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 269
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
wmc 29
lcom 3
cbo 3
dl 0
loc 269
rs 10
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A sluggable() 0 8 1
A boot() 0 15 3
A owner() 0 4 1
A venue() 0 4 1
A categories() 0 6 1
A championships() 0 4 1
A championshipSettings() 0 4 1
A teams() 0 4 1
A competitors() 0 4 1
A trees() 0 4 1
A getDateAttribute() 0 4 1
A getRegisterDateLimitAttribute() 0 4 1
A getDateIniAttribute() 0 4 1
A getDateFinAttribute() 0 4 1
A isOpen() 0 4 1
A needsInvitation() 0 4 1
A isInternational() 0 4 1
A isNational() 0 4 1
A isRegional() 0 4 1
A isEstate() 0 4 1
A isMunicipal() 0 4 1
A isDistrictal() 0 4 1
A isLocal() 0 4 1
A hasNoLevel() 0 4 1
A getRouteKeyName() 0 4 1
A isDeleted() 0 4 1
A hasTeamCategory() 0 7 1
1
<?php
2
3
namespace Xoco70\LaravelTournaments\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use Illuminate\Foundation\Auth\User;
8
9
/**
10
 * @property mixed type
11
 * @property float latitude
12
 * @property float longitude
13
 * @property mixed created_at
14
 * @property mixed updated_at
15
 * @property mixed deleted_at
16
 */
17
class Tournament extends Model
18
{
19
    use SoftDeletes;
20
21
    /**
22
     * Return the sluggable configuration array for this model.
23
     *
24
     * @return array
25
     */
26
    public function sluggable()
27
    {
28
        return [
29
            'slug' => [
30
                'source' => 'name',
31
            ],
32
        ];
33
    }
34
35
    protected $table = 'tournament';
36
    public $timestamps = true;
37
38
    protected $fillable = [
39
        'name',
40
        'slug',
41
        'dateIni',
42
        'dateFin',
43
        'registerDateLimit',
44
        'sport',
45
        'promoter',
46
        'host_organization',
47
        'technical_assistance',
48
        'category',
49
        'rule_id',
50
        'type',
51
        'venue_id',
52
        'level_id',
53
    ];
54
55
    protected $dates = ['dateIni', 'dateFin', 'registerDateLimit', 'created_at', 'updated_at', 'deleted_at'];
56
57
    protected static function boot()
58
    {
59
        parent::boot();
60
        static::deleting(function ($tournament) {
61
            foreach ($tournament->championships as $ct) {
62
                $ct->delete();
63
            }
64
            $tournament->invites()->delete();
65
        });
66
        static::restoring(function ($tournament) {
67
            foreach ($tournament->championships()->withTrashed()->get() as $ct) {
68
                $ct->restore();
69
            }
70
        });
71
    }
72
73
    /**
74
     * A tournament is owned by a user.
75
     *
76
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
77
     */
78
    public function owner()
79
    {
80
        return $this->belongsTo(User::class, 'user_id', 'id');
81
    }
82
83
    /**
84
     * Get Full venue object.
85
     *
86
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
87
     */
88
    public function venue()
89
    {
90
        return $this->belongsTo(Venue::class);
91
    }
92
93
    /**
94
     * We can use $tournament->categories()->attach(id);
95
     * Or         $tournament->categories()->sync([1, 2, 3]);.
96
     *
97
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
98
     */
99
    public function categories()
100
    {
101
        return $this->belongsToMany(Category::class, 'championship')
102
            ->withPivot('id')
103
            ->withTimestamps();
104
    }
105
106
    /**
107
     * Get All categoriesTournament that belongs to a tournament.
108
     *
109
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
110
     */
111
    public function championships()
112
    {
113
        return $this->hasMany(Championship::class);
114
    }
115
116
    /**
117
     * Get All categoriesSettings that belongs to a tournament.
118
     *
119
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
120
     */
121
    public function championshipSettings()
122
    {
123
        return $this->hasManyThrough(ChampionshipSettings::class, Championship::class);
124
    }
125
126
    /**
127
     * çGet All teams that belongs to a tournament.
128
     *
129
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
130
     */
131
    public function teams()
132
    {
133
        return $this->hasManyThrough(Team::class, Championship::class);
134
    }
135
136
    /**
137
     * Get All competitors that belongs to a tournament.
138
     *
139
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
140
     */
141
    public function competitors()
142
    {
143
        return $this->hasManyThrough(Competitor::class, Championship::class);
144
    }
145
146
    /**
147
     * Get All trees that belongs to a tournament.
148
     *
149
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
150
     */
151
    public function trees()
152
    {
153
        return $this->hasManyThrough(FightersGroup::class, Championship::class);
154
    }
155
156
    public function getDateAttribute($date)
157
    {
158
        return $date;
159
    }
160
161
    public function getRegisterDateLimitAttribute($date)
162
    {
163
        return $date;
164
    }
165
166
    public function getDateIniAttribute($date)
167
    {
168
        return $date;
169
    }
170
171
    public function getDateFinAttribute($date)
172
    {
173
        return $date;
174
    }
175
176
    /**
177
     * Check if the tournament is Open.
178
     *
179
     * @return bool
180
     */
181
    public function isOpen()
182
    {
183
        return $this->type == 1;
184
    }
185
186
    /**
187
     * * Check if the tournament needs Invitation.
188
     *
189
     * @return bool
190
     */
191
    public function needsInvitation()
192
    {
193
        return $this->type == 0;
194
    }
195
196
    /**
197
     * @return bool
198
     */
199
    public function isInternational()
200
    {
201
        return $this->level_id == 8;
202
    }
203
204
    /**
205
     * @return bool
206
     */
207
    public function isNational()
208
    {
209
        return $this->level_id == 7;
210
    }
211
212
    /**
213
     * @return bool
214
     */
215
    public function isRegional()
216
    {
217
        return $this->level_id == 6;
218
    }
219
220
    /**
221
     * @return bool
222
     */
223
    public function isEstate()
224
    {
225
        return $this->level_id == 5;
226
    }
227
228
    /**
229
     * @return bool
230
     */
231
    public function isMunicipal()
232
    {
233
        return $this->level_id == 4;
234
    }
235
236
    /**
237
     * @return bool
238
     */
239
    public function isDistrictal()
240
    {
241
        return $this->level_id == 3;
242
    }
243
244
    /**
245
     * @return bool
246
     */
247
    public function isLocal()
248
    {
249
        return $this->level_id == 2;
250
    }
251
252
    /**
253
     * @return bool
254
     */
255
    public function hasNoLevel()
256
    {
257
        return $this->level_id == 1;
258
    }
259
260
    public function getRouteKeyName()
261
    {
262
        return 'slug';
263
    }
264
265
    /**
266
     * @return bool
267
     */
268
    public function isDeleted()
269
    {
270
        return $this->deleted_at != null;
271
    }
272
273
    /**
274
     * Check if the tournament has a Team Championship.
275
     *
276
     * @return int
277
     */
278
    public function hasTeamCategory()
279
    {
280
        return $this
281
            ->categories()
282
            ->where('isTeam', '1')
283
            ->count();
284
    }
285
}
286