Test Failed
Push — master ( 2841c5...928d31 )
by Julien
04:15
created

FightersGroup::hasTournamentInRequest()   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 1
1
<?php
2
3
namespace Xoco70\KendoTournaments\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\DB;
8
9
class FightersGroup extends Model
10
{
11
    protected $table = 'fighters_groups';
12
    public $timestamps = true;
13
    protected $guarded = ['id'];
14
15
    /**
16
     * Check if Request contains tournamentSlug / Should Move to TreeRequest When Built.
17
     *
18
     * @param $request
19
     *
20
     * @return bool
21
     */
22
    public static function hasTournamentInRequest($request)
23
    {
24
        return $request->tournament != null;
25
    }
26
27
    /**
28
     * Check if Request contains championshipId / Should Move to TreeRequest When Built.
29
     *
30
     * @param $request
31
     *
32
     * @return bool
33
     */
34
    public static function hasChampionshipInRequest($request)
35
    {
36
        return $request->championshipId != null; // has return false, don't know why
37
    }
38
39
    /**
40
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
41
     */
42
    public function championship()
43
    {
44
        return $this->belongsTo(Championship::class);
45
    }
46
47
    /**
48
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
49
     */
50
    public function fights()
51
    {
52
        return $this->hasMany(Fight::class);
53
    }
54
55
    public function teams()
56
    {
57
        return $this->belongsToMany(Team::class, 'fighters_group_team')->withTimestamps();
58
    }
59
60
    public function competitors()
61
    {
62
        return $this->belongsToMany(Competitor::class, 'fighters_group_competitor')->withTimestamps();
63
    }
64
65
    /**
66
     * @param $settings
67
     * @param Championship $championship
68
     */
69
    public static function generateFights(Collection $fightersGroup, $settings, Championship $championship = null)
70
    {
71
72
        // Delete previous fight for this championship
73
74
        $arrGroupsId = $fightersGroup->map(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
            return $value->id;
76
        })->toArray();
77
        Fight::destroy($arrGroupsId);
78
79
        if ($settings->hasPreliminary && $settings->preliminaryGroupSize == 3) {
80
            for ($numGroup = 1; $numGroup <= $settings->preliminaryGroupSize; $numGroup++) {
81
                Fight::savePreliminaryFightGroup($fightersGroup, $numGroup);
82
            }
83
        } else {
84
            Fight::saveGroupdRobinFights($championship, $fightersGroup);
85
        }
86
    }
87
88
    /**
89
     * Supercharge of sync Many2Many function.
90
     * Original sync doesn't insert NULL ids.
91
     *
92
     * @param $fighters
93
     */
94 View Code Duplication
    public function syncTeams($fighters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $this->teams()->detach();
97
        foreach ($fighters as $fighter) {
98
            if ($fighter != null) {
99
                $this->teams()->attach($fighter);
100
            } else {
101
                // Insert row manually
102
                DB::table('fighters_group_team')->insertGetId(
103
                    ['team_id' => null, 'fighters_group_id' => $this->id]
104
                );
105
            }
106
        }
107
    }
108
109
    /**
110
     * Supercharge of sync Many2Many function.
111
     * Original sync doesn't insert NULL ids.
112
     *
113
     * @param $fighters
114
     */
115 View Code Duplication
    public function syncCompetitors($fighters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        $this->competitors()->detach();
118
        foreach ($fighters as $fighter) {
119
            if ($fighter != null) {
120
                $this->competitors()->attach($fighter);
121
            } else {
122
                DB::table('fighters_group_competitor')->insertGetId(
123
                    ['competitor_id' => null, 'fighters_group_id' => $this->id]
124
                );
125
            }
126
        }
127
    }
128
}
129