Passed
Branch master (8db9fa)
by Julien
24:59
created

FightersGroup::competitors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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\TreeGen;
11
12
class FightersGroup extends Model
13
{
14
    protected $table = 'fighters_groups';
15
    public $timestamps = true;
16
    protected $guarded = ['id'];
17
18
    use NodeTrait;
19
20
    /**
21
     * Check if Request contains tournamentSlug / Should Move to TreeRequest When Built.
22
     *
23
     * @param $request
24
     *
25
     * @return bool
26
     */
27
    public static function hasTournamentInRequest($request)
28
    {
29
        return $request->tournament != null;
30
    }
31
32
    /**
33
     * Check if Request contains championshipId / Should Move to TreeRequest When Built.
34
     *
35
     * @param $request
36
     *
37
     * @return bool
38
     */
39
    public static function hasChampionshipInRequest($request)
40
    {
41
        return $request->championshipId != null; // has return false, don't know why
42
    }
43
44
    /**
45
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
46
     */
47
    public function championship()
48
    {
49
        return $this->belongsTo(Championship::class);
50
    }
51
52
    /**
53
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
54
     */
55
    public function fights()
56
    {
57
        return $this->hasMany(Fight::class);
58
    }
59
60
    public function teams()
61
    {
62
        return $this->belongsToMany(Team::class, 'fighters_group_team')->withTimestamps();
63
    }
64
65
    public function competitors()
66
    {
67
        return $this->belongsToMany(Competitor::class, 'fighters_group_competitor')->withTimestamps();
68
    }
69
70
    /**
71
     * Generate First Round Fights
72
     * @param Championship $championship
73
     */
74
    public static function generateFights(Championship $championship)
75
    {
76
        // Delete previous fight for this championship
77
78
        $arrGroupsId = $championship->fightersGroups->map->id->toArray();
79
        Fight::destroy($arrGroupsId);
80
81
        if ($championship->settings->hasPreliminary && $championship->settings->preliminaryGroupSize == 3) {
82
            for ($numGroup = 1; $numGroup <= $championship->settings->preliminaryGroupSize; $numGroup++) {
83
                Fight::savePreliminaryFightGroup($championship->fightersGroups, $numGroup);
84
            }
85
        } else {
86
            Fight::saveGroupFights($championship);
87
        }
88
    }
89
90
    /**
91
     * Supercharge of sync Many2Many function.
92
     * Original sync doesn't insert NULL ids.
93
     *
94
     * @param $fighters
95
     */
96
    public function syncTeams($fighters)
97
    {
98
        $this->teams()->detach();
99
        foreach ($fighters as $fighter) {
100
            if ($fighter != null) {
101
                $this->teams()->attach($fighter);
102
            } else {
103
                // Insert row manually
104
                DB::table('fighters_group_team')->insertGetId(
105
                    ['team_id' => null, 'fighters_group_id' => $this->id]
106
                );
107
            }
108
        }
109
    }
110
111
    /**
112
     * Supercharge of sync Many2Many function.
113
     * Original sync doesn't insert NULL ids.
114
     *
115
     * @param $fighters
116
     */
117
    public function syncCompetitors($fighters)
118
    {
119
        $this->competitors()->detach();
120
        foreach ($fighters as $fighter) {
121
            if ($fighter != null) {
122
                $this->competitors()->attach($fighter);
123
            } else {
124
                DB::table('fighters_group_competitor')->insertGetId(
125
                    ['competitor_id' => null, 'fighters_group_id' => $this->id,
126
                        "created_at" => Carbon::now(),
127
                        "updated_at" => Carbon::now(),
128
                    ]
129
                );
130
            }
131
        }
132
    }
133
134
135
    /**
136
     * Get the many 2 many relationship with
137
     * @return Collection
138
     */
139
    public function competitorsWithNull() : Collection
140
    {
141
        $competitors = new Collection();
142
        $fgcs = FighterGroupCompetitor::where('fighters_group_id', $this->id)
143
            ->with('competitor')
144
            ->get();
145
        foreach ($fgcs as $fgc) {
146
            $competitors->push($fgc->competitor ?? new Competitor());
147
        }
148
149
        return $competitors;
150
151
    }
152
153
154
    public function teamsWithNull() : Collection
155
    {
156
        $teams = new Collection();
157
        $fgcs = FighterGroupTeam::where('fighters_group_id', $this->id)
158
            ->with('team')
159
            ->get();
160
        foreach ($fgcs as $fgc) {
161
            $teams->push($fgc->team ?? new Team());
162
        }
163
164
        return $teams;
165
166
    }
167
168
    public function getFighters() : Collection
169
    {
170
        if ($this->championship->category->isTeam()) {
171
            $fighters = $this->teamsWithNull();
172
        } else {
173
            $fighters = $this->competitorsWithNull();
174
        }
175
176
        if (sizeof($fighters) == 0) {
177
            $treeGen = new TreeGen($this->championship, null, null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Xoco70\KendoTourn...s\ChampionshipSettings>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
178
            $fighters = $treeGen->createByeGroup(2);
179
        }
180
        return $fighters;
181
    }
182
183
    public static function getBaseNumGroups($initialGroupId, $numGroups, $numRound): int
184
    {
185
        $parentId = $initialGroupId;
186
187
        for ($i = 1; $i <= $numRound; $i++) {
188
            $parentId += $numGroups / $numRound;
189
        }
190
191
        return $parentId;
192
    }
193
}
194