Test Failed
Push — master ( 8ff729...bf81a5 )
by Julien
03:11
created

Tree::getGenerationStrategy()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace Xoco70\KendoTournaments\Models;
4
5
6
use App\User;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Collection;
9
use Xoco70\KendoTournaments\TreeGen\TreeGen;
10
11
class Tree extends Model
12
{
13
    protected $table = 'tree';
14
    public $timestamps = true;
15
    protected $guarded = ['id'];
16
17
    /**
18
     * Check if Request contains tournamentSlug / Should Move to TreeRequest When Built
19
     * @param $request
20
     * @return bool
21
     */
22
    public static function hasTournament($request)
23
    {
24
        return $request->tournamentSlug != null;
25
    }
26
27
    /**
28
     * Check if Request contains championshipId / Should Move to TreeRequest When Built
29
     * @param $request
30
     * @return bool
31
     */
32
    public static function hasChampionship($request)
33
    {
34
        return $request->championshipId != null; // has return false, don't know why
35
    }
36
37
38
    /**
39
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
40
     */
41
    public function championship()
42
    {
43
        return $this->belongsTo(Championship::class);
44
    }
45
46
    /**
47
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
48
     */
49
    public function fights()
50
    {
51
        return $this->hasMany(Fight::class, 'tree_id', 'id');
52
    }
53
54
    /**
55
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
56
     */
57
    public function user1()
58
    {
59
        return $this->belongsTo(User::class, 'c1', 'id');
60
    }
61
62
    /**
63
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
64
     */
65
    public function user2()
66
    {
67
        return $this->belongsTo(User::class, 'c2', 'id');
68
    }
69
70
    /**
71
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
72
     */
73
    public function user3()
74
    {
75
        return $this->belongsTo(User::class, 'c3', 'id');
76
    }
77
78
    /**
79
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
80
     */
81
    public function user4()
82
    {
83
        return $this->belongsTo(User::class, 'c4', 'id');
84
    }
85
86
    /**
87
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
88
     */
89
    public function user5()
90
    {
91
        return $this->belongsTo(User::class, 'c5', 'id');
92
    }
93
94
95
    /**
96
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
97
     */
98
    public function team1()
99
    {
100
        return $this->belongsTo(Team::class, 'c1', 'id');
101
    }
102
103
    /**
104
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
105
     */
106
    public function team2()
107
    {
108
        return $this->belongsTo(Team::class, 'c2', 'id');
109
    }
110
111
    /**
112
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
113
     */
114
    public function team3()
115
    {
116
        return $this->belongsTo(Team::class, 'c3', 'id');
117
    }
118
119
120
121
    /**
122
     * @param Collection $tree
123
     * @param $settings
124
     */
125
    public static function generateFights(Collection $tree, $settings, Championship $championship = null)
126
    {
127
128
        // Delete previous fight for this championship
129
130
        $arrayTreeId = $tree->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...
131
            return $value->id;
132
        })->toArray();
133
        Fight::destroy($arrayTreeId);
134
135
        if ($settings->hasPreliminary) {
136
            if ($settings->preliminaryGroupSize == 3) {
137
                for ($numRound = 1; $numRound <= $settings->preliminaryGroupSize; $numRound++) {
138
                    Fight::saveFightRound($tree, $numRound);
139
                }
140
            } else {
141
                Fight::saveRoundRobinFights($championship, $tree);
142
            }
143
        } elseif ($settings->treeType == config('kendo-tournaments.DIRECT_ELIMINATION')) {
144
            Fight::saveFightRound($tree); // Always C1 x C2
145
        } elseif ($settings->treeType == config('kendo-tournaments.ROUND_ROBIN')) {
146
            Fight::saveRoundRobinFights($championship, $tree);
147
148
        }
149
150
    }
151
}