Test Failed
Push — master ( 2d5b3a...83ab01 )
by Julien
03:19
created

Round::syncTeams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Xoco70\KendoTournaments\Models;
4
5
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Collection;
8
use Illuminate\Support\Facades\DB;
9
10
class Round extends Model
11
{
12
    protected $table = 'round';
13
    public $timestamps = true;
14
    protected $guarded = ['id'];
15
16
    /**
17
     * Check if Request contains tournamentSlug / Should Move to TreeRequest When Built
18
     * @param $request
19
     * @return bool
20
     */
21
    public static function hasTournamentInRequest($request)
22
    {
23
        return $request->tournament != null;
24
    }
25
26
    /**
27
     * Check if Request contains championshipId / Should Move to TreeRequest When Built
28
     * @param $request
29
     * @return bool
30
     */
31
    public static function hasChampionshipInRequest($request)
32
    {
33
        return $request->championshipId != null; // has return false, don't know why
34
    }
35
36
37
    /**
38
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
39
     */
40
    public function championship()
41
    {
42
        return $this->belongsTo(Championship::class);
43
    }
44
45
    /**
46
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
47
     */
48
    public function fights()
49
    {
50
        return $this->hasMany(Fight::class, 'tree_id', 'id');
51
    }
52
53
    public function teams()
54
    {
55
        return $this->belongsToMany(Team::class, 'round_team')->withTimestamps();
56
    }
57
58
    public function competitors()
59
    {
60
        return $this->belongsToMany(Competitor::class, 'round_competitor')->withTimestamps();
61
    }
62
63
64
    /**
65
     * @param $settings
66
     * @param Championship $championship
67
     */
68
    public static function generateFights(Collection $rounds, $settings, Championship $championship = null)
69
    {
70
71
        // Delete previous fight for this championship
72
73
        $arrRoundsId = $rounds->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...
74
            return $value->id;
75
        })->toArray();
76
        Fight::destroy($arrRoundsId);
77
78
        if ($settings->hasPreliminary && $settings->preliminaryGroupSize == 3) {
79
80
            for ($numRound = 1; $numRound <= $settings->preliminaryGroupSize; $numRound++) {
81
                Fight::savePreliminaryFightRound($rounds, $numRound);
82
            }
83
        } else {
84
            Fight::saveRoundRobinFights($championship, $rounds);
85
        }
86
    }
87
88
89
    /**
90
     * Supercharge of sync Many2Many function.
91
     * Original sync doesn't insert NULL ids
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('round_team')->insertGetId(
103
                    ['team_id' => null, 'round_id' => $this->id]
104
                );
105
            }
106
        }
107
    }
108
109
    /**
110
     * Supercharge of sync Many2Many function.
111
     * Original sync doesn't insert NULL ids
112
     * @param $fighters
113
     */
114 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...
115
    {
116
        $this->competitors()->detach();
117
        foreach ($fighters as $fighter) {
118
            if ($fighter != null) {
119
                $this->competitors()->attach($fighter);
120
            } else {
121
                DB::table('round_competitor')->insertGetId(
122
                    ['competitor_id' => null, 'round_id' => $this->id]
123
                );
124
            }
125
        }
126
    }
127
}