Completed
Push — master ( 8efd5e...9e77ef )
by Julien
39:33
created

Tournament::getCategoryIdArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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