Completed
Push — master ( 0a3a64...75d382 )
by Julien
02:47
created

Fight   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Importance

Changes 12
Bugs 2 Features 1
Metric Value
wmc 31
lcom 4
cbo 3
dl 0
loc 176
rs 9.8
c 12
b 2
f 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A group() 0 4 1
A competitor1() 0 4 1
A competitor2() 0 4 1
A team1() 0 4 1
A team2() 0 4 1
B getFighterAttr() 0 17 7
A getFightersWithByes() 0 14 4
A shouldBeInFightList() 0 16 4
A has2Fighters() 0 4 2
A belongsToFirstRound() 0 6 2
A dontHave2Fighters() 0 4 2
A generateFightsId() 0 7 2
A updateShortId() 0 9 2
1
<?php
2
3
namespace Xoco70\KendoTournaments\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
8
class Fight extends Model
9
{
10
    /**
11
     * Fight constructor.
12
     * @param int $userId1
13
     * @param int $userId2
14
     */
15
    public function __construct($userId1 = null, $userId2 = null)
16
    {
17
        $this->c1 = $userId1;
18
        $this->c2 = $userId2;
19
    }
20
21
    protected $table = 'fight';
22
    public $timestamps = true;
23
24
    protected $fillable = [
25
        'group_id',
26
        'c1',
27
        'c2',
28
    ];
29
30
31
    /**
32
     * Get First Fighter.
33
     *
34
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
35
     */
36
    public function group()
37
    {
38
        return $this->belongsTo(FightersGroup::class, 'fighters_group_id');
39
    }
40
41
    /**
42
     * @param FightersGroup|null $group
43
     * @return Collection
44
     * @internal param Championship $championship
45
     *
46
     */
47
    protected static function getFightersWithByes(FightersGroup $group)
48
    {
49
        if ($group == null) return null;
50
        $fighters = $group->getFighters();
51
        $fighterType = $group->getFighterType();
52
        if (sizeof($fighters) == 0) {
53
            $fighters->push(new $fighterType);
54
            $fighters->push(new $fighterType);
55
        } else if (count($fighters) % 2 != 0) {
56
            $fighters->push(new $fighterType);
57
        }
58
59
        return $fighters;
60
    }
61
62
    /**
63
     * Get First Fighter.
64
     *
65
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
66
     */
67
    public function competitor1()
68
    {
69
        return $this->belongsTo(Competitor::class, 'c1', 'id');
70
    }
71
72
    /**
73
     * Get Second Fighter.
74
     *
75
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
76
     */
77
    public function competitor2()
78
    {
79
        return $this->belongsTo(Competitor::class, 'c2', 'id');
80
    }
81
82
    /**
83
     * Get First Fighter.
84
     *
85
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
86
     */
87
    public function team1()
88
    {
89
        return $this->belongsTo(Team::class, 'c1', 'id');
90
    }
91
92
    /**
93
     * Get Second Fighter.
94
     *
95
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
96
     */
97
    public function team2()
98
    {
99
        return $this->belongsTo(Team::class, 'c2', 'id');
100
    }
101
102
103
    public function getFighterAttr($numFighter, $attr)
104
    {
105
        $isTeam = $this->group->championship->category->isTeam;
106
        if ($isTeam) {
107
            $teamToUpdate = 'team' . $numFighter;
108
            return $this->$teamToUpdate == null ? '' : $this->$teamToUpdate->$attr;
109
        }
110
        $competitorToUpdate = 'competitor' . $numFighter;
111
        if ($attr == 'name') {
112
            return $this->$competitorToUpdate == null
113
                ? 'BYE'
114
                : $this->$competitorToUpdate->user->firstname . " " . $this->$competitorToUpdate->user->lastname;
115
        } elseif ($attr == 'short_id') {
116
            return $this->$competitorToUpdate == null ? '' : $this->$competitorToUpdate->short_id;
117
        }
118
        return null;
119
    }
120
121
122
    public function shouldBeInFightList()
123
    {
124
        if ($this->belongsToFirstRound() && $this->dontHave2Fighters()) return false;
125
        if ($this->has2Fighters()) return true;
126
        // We aint in the first round, and there is 1 or 0 competitor
127
        // We check children, and see :
128
        // if there is 2  - 2 fighters -> undetermine, we cannot add it to fight list
129
        // if there is 2  - 1 fighters -> undetermine, we cannot add it to fight list
130
        // if there is 2  - 0 fighters -> undetermine, we cannot add it to fight list
131
        // if there is 1  - 2 fighters -> undetermine, we cannot add it to fight list
132
        // if there is 1  - 1 fighters -> fight should have 2 fighters, undetermines
133
        // if there is 1  - 0 fighters -> determined, fight should not be in the list
134
        // if there is 0  - 1 fighters -> determined, fight should not be in the list
135
        // So anyway, we should return false
136
        return false;
137
    }
138
139
    /**
140
     * return true if fight has 2 fighters ( No BYE )
141
     * @return bool
142
     */
143
    public function has2Fighters(): bool
144
    {
145
        return $this->c1 != null && $this->c2 != null;
146
    }
147
148
    private function belongsToFirstRound()
149
    {
150
        $firstRoundFights = $this->group->championship->firstRoundFights->pluck('id')->toArray();
151
        if (in_array($this->id, $firstRoundFights)) return true;
152
        return false;
153
    }
154
155
    private function dontHave2Fighters() // 1 or 0
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
156
    {
157
        return $this->c1 == null || $this->c2 == null;
158
    }
159
160
161
    public static function generateFightsId($championship)
162
    {
163
        $order = 1;
164
        foreach ($championship->fights as $fight) {
165
            $order = $fight->updateShortId($order);
166
        }
167
    }
168
169
    /**
170
     * @param $order
171
     * @return int
172
     */
173
    public function updateShortId($order)
174
    {
175
        if ($this->shouldBeInFightList()) {
176
            $this->short_id = $order;
177
            $this->save();
178
            return ++$order;
179
        }
180
        return $order;
181
    }
182
183
}