Completed
Push — master ( de03dc...acff3e )
by Julien
26:33 queued 11:32
created

Tournament::getDefaultCategoriesName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace App;
4
5
6
use Cocur\Slugify\Slugify;
7
use Cviebrock\EloquentSluggable\Engines\IdeographicEngine;
8
use Cviebrock\EloquentSluggable\Engines\KoreanEngine;
9
use Cviebrock\EloquentSluggable\Sluggable;
10
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
11
use Illuminate\Database\Eloquent\SoftDeletes;
12
use OwenIt\Auditing\AuditingTrait;
13
use Xoco70\LaravelTournaments\Models\ChampionshipSettings;
14
15
16
/**
17
 * @property mixed type
18
 * @property float latitude
19
 * @property float longitude
20
 * @property mixed created_at
21
 * @property mixed updated_at
22
 * @property mixed deleted_at
23
 */
24
class Tournament extends \Xoco70\LaravelTournaments\Models\Tournament
25
{
26
    use SoftDeletes;
27
    use Sluggable;
28
    use AuditingTrait;
29
    use SluggableScopeHelpers;
30
31
    public $timestamps = true;
32
    protected $table = 'tournament';
33
    protected $fillable = [
34
        'name',
35
        'dateIni',
36
        'dateFin',
37
        'registerDateLimit',
38
        'sport',
39
        'promoter',
40
        'host_organization',
41
        'technical_assistance',
42
        'category',
43
        'rule_id',
44
        'type',
45
        'venue_id',
46
        'level_id'
47
    ];
48
    protected $dates = ['dateIni', 'dateFin', 'registerDateLimit', 'created_at', 'updated_at', 'deleted_at'];
49
50 34
    protected static function boot()
51
    {
52 34
        parent::boot();
53 34
        static::deleting(function ($tournament) {
54 2
            $tournament->championships->each->delete();
55 2
            $tournament->invites->each->delete();
56
57 34
        });
58 34
        static::restoring(function ($tournament) {
59 1
            $tournament->championships()->withTrashed()->get()->each->restore();
60 34
        });
61
62 34
    }
63
64
    /**
65
     * Return the sluggable configuration array for this model.
66
     *
67
     * @return array
68
     */
69 20
    public function sluggable()
70
    {
71
        return [
72 20
            'slug' => [
73
                'source' => 'name'
74
            ]
75
        ];
76
    }
77
78 1
    public function customizeSlugEngine(Slugify $engine, $attribute)
79
    {
80 1
        if (isJapanese($this->name)) { // Or Korean, or any unsupported language
81
            return new IdeographicEngine();
82
        }
83 1
        if (isKorean($this->name)) { // Or Korean, or any unsupported language
84
            return new KoreanEngine();
85
        }
86 1
        return $engine;
87
    }
88
89
    /**
90
     * A tournament is owned by a user
91
     *
92
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
93
     */
94 13
    public function owner()
95
    {
96 13
        return $this->belongsTo(User::class, 'user_id', 'id');
97
    }
98
99
    /**
100
     * Get All Tournaments levels
101
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
102
     */
103 1
    public function level()
104
    {
105 1
        return $this->belongsTo(TournamentLevel::class, 'level_id', 'id');
106
    }
107
108
    /**
109
     * Get Full venue object
110
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
111
     */
112 8
    public function venue()
113
    {
114 8
        return $this->belongsTo(Venue::class);
115
    }
116
117
    /**
118
     * Get All categoriesTournament that belongs to a tournament
119
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
120
     */
121 15
    public function championships()
122
    {
123 15
        return $this->hasMany(Championship::class);
124
    }
125
126
    /**
127
     * Get All categoriesSettings that belongs to a tournament
128
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
129
     */
130 8
    public function championshipSettings()
131
    {
132 8
        return $this->hasManyThrough(ChampionshipSettings::class, Championship::class);
133
    }
134
135
    /**
136
     * çGet All teams that belongs to a tournament
137
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
138
     */
139 6
    public function teams()
140
    {
141 6
        return $this->hasManyThrough(Team::class, Championship::class);
142
    }
143
144
    /**
145
     * Get All competitors that belongs to a tournament
146
     * @param null $championshipId
147
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
148
     */
149 11
    public function competitors($championshipId = null)
150
    {
151 11
        return $this->hasManyThrough(Competitor::class, Championship::class);
152
    }
153
154
    /**
155
     * Get All trees that belongs to a tournament
156
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
157
     */
158 5
    public function trees()
159
    {
160 5
        return $this->hasManyThrough(FightersGroup::class, Championship::class);
161
    }
162
163
    /**
164
     * Get all Invitations that belongs to a tournament
165
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
166
     */
167 2
    public function invites()
168
    {
169 2
        return $this->morphMany(Invite::class, 'object');
170
    }
171
172
    /**
173
     * Get Category List with <Select> Format
174
     * @return mixed
175
     */
176 6
    public function getCategoryList()
177
    {
178 6
        return $this->categories->pluck('id')->all();
179
    }
180
181
    public function getDateAttribute($date)
182
    {
183
        return $date;
184
    }
185
186 6
    public function getRegisterDateLimitAttribute($date)
187
    {
188 6
        return $date;
189
    }
190
191 8
    public function getDateIniAttribute($date)
192
    {
193 8
        return $date;
194
    }
195
196 6
    public function getDateFinAttribute($date)
197
    {
198 6
        return $date;
199
    }
200
201
    /**
202
     * Check if the tournament is Open
203
     * @return bool
204
     */
205 1
    public function isOpen()
206
    {
207 1
        return $this->type == 1;
208
    }
209
210
    /**
211
     * * Check if the tournament needs Invitation
212
     * @return bool
213
     */
214
    public function needsInvitation()
215
    {
216
        return $this->type == 0;
217
    }
218
219
    /**
220
     * @return bool
221
     */
222
    public function isInternational()
223
    {
224
        return $this->level_id == 8;
225
    }
226
227
    /**
228
     * @return bool
229
     */
230
    public function isNational()
231
    {
232
        return $this->level_id == 7;
233
    }
234
235
    /**
236
     * @return bool
237
     */
238
    public function isRegional()
239
    {
240
        return $this->level_id == 6;
241
    }
242
243
    /**
244
     * @return bool
245
     */
246
    public function isEstate()
247
    {
248
        return $this->level_id == 5;
249
    }
250
251
    /**
252
     * @return bool
253
     */
254
    public function isMunicipal()
255
    {
256
        return $this->level_id == 4;
257
    }
258
259
    /**
260
     * @return bool
261
     */
262
    public function isDistrictal()
263
    {
264
        return $this->level_id == 3;
265
    }
266
267
    /**
268
     * @return bool
269
     */
270
    public function isLocal()
271
    {
272
        return $this->level_id == 2;
273
    }
274
275
    /**
276
     * @return bool
277
     */
278
    public function hasNoLevel()
279
    {
280
        return $this->level_id == 1;
281
    }
282
283 15
    public function getRouteKeyName()
284
    {
285 15
        return 'slug';
286
    }
287
288
    /**
289
     * @return bool
290
     */
291 2
    public function isDeleted()
292
    {
293 2
        return $this->deleted_at != null;
294
    }
295
296
    /**
297
     * Create and Configure Championships depending the rule ( IKF, EKF, LAKF, etc )
298
     * @param $ruleId
299
     */
300 1
    public function setAndConfigureCategories($ruleId)
301
    {
302 1
        if ($ruleId == 0) return; // No Rules Selected
303
304 1
        $options = $this->loadRulesOptions($ruleId);
305
306
        // Create Tournament Categories
307 1
        $arrCategories = array_keys($options);
308 1
        $this->categories()->sync($arrCategories);
309
310
        // Configure each category creating categorySetting Object
311
312 1
        foreach ($this->championships as $championship) {
313 1
            $rules = $options[$championship->category->id];
314 1
            $rules['championship_id'] = $championship->id;
315 1
            ChampionshipSettings::create($rules);
316
        }
317 1
    }
318
319
    /**
320
     * return correct presets rues
321
     * @param $ruleId
322
     * @return mixed|null
323
     */
324 1
    private function loadRulesOptions($ruleId)
325
    {
326
        switch ($ruleId) {
327 1
            case 0: // No preset selected
328
                return null;
329 1
            case 1:
330 1
                return $options = config('options.ikf_settings');
0 ignored issues
show
Unused Code introduced by
$options 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...
331
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
332
            case 2:
333
                return $options = config('options.ekf_settings');
0 ignored issues
show
Unused Code introduced by
$options 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...
334
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
335
            case 3:
336
                return $options = config('options.lakc_settings');
0 ignored issues
show
Unused Code introduced by
$options 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...
337
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
338
            default:
339
                return null;
340
        }
341
    }
342
343
    /**
344
     * We can use $tournament->categories()->attach(id);
345
     * Or         $tournament->categories()->sync([1, 2, 3]);
346
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
347
     */
348 9
    public function categories()
349
    {
350 9
        return $this->belongsToMany(Category::class, 'championship')
351 9
            ->withPivot('id')
352 9
            ->withTimestamps();
353
    }
354
355
    /**
356
     * create a category List with Category name associated to championshipId
357
     *
358
     * @return array
359
     */
360
    public function buildCategoryList()
361
    {
362
        $championships = Championship::with('category', 'settings')
0 ignored issues
show
Bug introduced by
The method whereHas does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
363
            ->whereHas('category', function ($query) {
364
                return $query->where('isTeam', 1);
365
            })
366
            ->where('tournament_id', $this->id)
367
            ->get();
368
369
        $array = [];
370
        foreach ($championships as $championship) {
371
            $array[$championship->id] = $championship->settings->alias != ''
372
                ? $championship->settings->alias
373
                : trim($championship->buildName());
374
        }
375
        return $array;
376
    }
377
378
    /**
379
     * Get predefined translatable categories for categories quick add in tournament editing
380
     * @return array
381
     */
382 5
    public function getDefaultCategoriesName()
383
    {
384 5
        return Category::take(7)
385 5
            ->pluck('name', 'id')
386 5
            ->sortBy('id')
387 5
            ->toArray();
388
    }
389
}