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

FightersGroup   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 31
c 2
b 0
f 0
lcom 2
cbo 7
dl 0
loc 207
rs 9.8

15 Methods

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