Completed
Push — master ( 47ea5c...7bab12 )
by Julien
03:03
created

FightersGroup   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 33
c 4
b 0
f 0
lcom 2
cbo 7
dl 0
loc 209
rs 9.3999

16 Methods

Rating   Name   Duplication   Size   Complexity  
A championship() 0 4 1
A fights() 0 4 1
A fightersWithBye() 0 7 2
A fighters() 0 7 2
A teams() 0 4 1
A competitors() 0 4 1
A syncTeams() 0 14 3
A syncCompetitors() 0 16 3
A competitorsWithBye() 0 13 2
A teamsWithBye() 0 13 2
A getBaseNumGroups() 0 10 2
A getFighterType() 0 7 2
A hasDeterminedParent() 0 12 4
A getValueToUpdate() 0 7 2
A getParentFighterToUpdate() 0 11 3
A getFightersWithBye() 0 8 2
1
<?php
2
3
namespace Xoco70\KendoTournaments\Models;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Facades\DB;
9
use Kalnoy\Nestedset\NodeTrait;
10
use Xoco70\KendoTournaments\TreeGen\DirectEliminationTreeGen;
11
use Xoco70\KendoTournaments\TreeGen\TreeGen;
12
13
class FightersGroup extends Model
14
{
15
    protected $table = 'fighters_groups';
16
    public $timestamps = true;
17
    protected $guarded = ['id'];
18
19
    use NodeTrait;
20
21
    /**
22
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
23
     */
24
    public function championship()
25
    {
26
        return $this->belongsTo(Championship::class);
27
    }
28
29
    /**
30
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
31
     */
32
    public function fights()
33
    {
34
        return $this->hasMany(Fight::class);
35
    }
36
37
    public function fightersWithBye()
38
    {
39
        if ($this->championship->category->isTeam()) {
40
            return $this->teamsWithBye();
41
        }
42
        return $this->competitorsWithBye();
43
    }
44
45
    public function fighters()
46
    {
47
        if ($this->championship->category->isTeam()) {
48
            return $this->teams;
49
        }
50
        return $this->competitors;
51
    }
52
53
    public function teams()
54
    {
55
        return $this->belongsToMany(Team::class, 'fighters_group_team')->withTimestamps();
56
    }
57
58
    public function competitors()
59
    {
60
        return $this->belongsToMany(Competitor::class, 'fighters_group_competitor')->withTimestamps();
61
    }
62
63
64
    /**
65
     * Supercharge of sync Many2Many function.
66
     * Original sync doesn't insert NULL ids.
67
     *
68
     * @param $fighters
69
     */
70
    public function syncTeams($fighters)
71
    {
72
        $this->teams()->detach();
73
        foreach ($fighters as $fighter) {
74
            if ($fighter != null) {
75
                $this->teams()->attach($fighter);
76
            } else {
77
                // Insert row manually
78
                DB::table('fighters_group_team')->insertGetId(
79
                    ['team_id' => null, 'fighters_group_id' => $this->id]
80
                );
81
            }
82
        }
83
    }
84
85
    /**
86
     * Supercharge of sync Many2Many function.
87
     * Original sync doesn't insert NULL ids.
88
     *
89
     * @param $fighters
90
     */
91
    public function syncCompetitors($fighters)
92
    {
93
        $this->competitors()->detach();
94
        foreach ($fighters as $fighter) {
95
            if ($fighter != null) {
96
                $this->competitors()->attach($fighter);
97
            } else {
98
                DB::table('fighters_group_competitor')->insertGetId(
99
                    ['competitor_id' => null, 'fighters_group_id' => $this->id,
100
                        "created_at" => Carbon::now(),
101
                        "updated_at" => Carbon::now(),
102
                    ]
103
                );
104
            }
105
        }
106
    }
107
108
109
    /**
110
     * Get the many 2 many relationship with the Null Rows
111
     *
112
     * @return Collection
113
     */
114
    public function competitorsWithBye(): Collection
115
    {
116
        $competitors = new Collection();
117
        $fgcs = FighterGroupCompetitor::where('fighters_group_id', $this->id)
118
            ->with('competitor')
119
            ->get();
120
121
        foreach ($fgcs as $fgc) {
122
            $competitors->push($fgc->competitor ?? new Competitor());
123
        }
124
        return $competitors;
125
126
    }
127
128
129
    public function teamsWithBye(): Collection
130
    {
131
        $teams = new Collection();
132
        $fgcs = FighterGroupTeam::where('fighters_group_id', $this->id)
133
            ->with('team')
134
            ->get();
135
        foreach ($fgcs as $fgc) {
136
            $teams->push($fgc->team ?? new Team());
137
        }
138
139
        return $teams;
140
141
    }
142
143
    public function getFightersWithBye(): Collection
144
    {
145
        if ($this->championship->category->isTeam()) {
146
            return $this->teamsWithBye();
147
        }
148
        return $this->competitorsWithBye();
149
150
    }
151
152
    public static function getBaseNumGroups($initialGroupId, $numGroups, $numRound): int
153
    {
154
        $parentId = $initialGroupId;
155
156
        for ($i = 1; $i <= $numRound; $i++) {
157
            $parentId += $numGroups / $numRound;
158
        }
159
160
        return $parentId;
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    public function getFighterType()
167
    {
168
        if ($this->championship->category->isTeam()) {
169
            return Team::class;
170
        }
171
        return Competitor::class;
172
    }
173
174
    /**
175
     * Check if we are able to fill the parent fight or not
176
     * If one of the children has c1 x c2, then we must wait to fill parent
177
     *
178
     * @return bool
179
     */
180
    public function hasDeterminedParent()
181
    {
182
        // There is more than 1 fight, should be Preliminary
183
        if (sizeof($this->fighters()) > 1){
184
            return false;
185
        }
186
        foreach ($this->children as $child) {
187
            if (sizeof($child->fighters()) > 1) return false;
188
        }
189
        return true;
190
191
    }
192
193
194
    /**
195
     * In the original fight ( child ) return the field that contains data to copy to parent
196
     * @return int
197
     */
198
    public function getValueToUpdate()
199
    {
200
        if ($this->championship->category->isTeam()) {
201
            return $this->teams->map->id[0];
202
        }
203
        return $this->competitors->map->id[0];
204
    }
205
206
    /**
207
     * Returns the parent field that need to be updated
208
     * @return null|string
209
     */
210
    public function getParentFighterToUpdate($keyGroup)
211
    {
212
        if (intval($keyGroup % 2) == 0) {
213
            return "c1";
214
        }
215
        if (intval($keyGroup % 2) == 1) {
216
            return "c2";
217
        }
218
219
        return null;
220
    }
221
}
222