1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xoco70\LaravelTournaments\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
|
7
|
|
|
class PreliminaryFight extends Fight |
8
|
|
|
{ |
9
|
|
View Code Duplication |
public function __construct(Fight $fight = null) |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
parent::__construct(); |
12
|
|
|
if ($fight != null) { |
13
|
|
|
$this->id = $fight->id; |
|
|
|
|
14
|
|
|
$this->short_id = $fight->short_id; |
|
|
|
|
15
|
|
|
$this->fighters_group_id = $fight->fighters_group_id; |
|
|
|
|
16
|
|
|
$this->c1 = $fight->c1; |
|
|
|
|
17
|
|
|
$this->c2 = $fight->c2; |
|
|
|
|
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Save all fights. |
23
|
|
|
* |
24
|
|
|
* @param Collection $groups |
25
|
|
|
* @param int $numGroup |
26
|
|
|
*/ |
27
|
|
|
public static function saveFights(Collection $groups, $numGroup = 1) |
28
|
|
|
{ |
29
|
|
|
$competitor1 = $competitor2 = null; |
30
|
|
|
$order = 1; |
31
|
|
|
|
32
|
|
|
foreach ($groups as $group) { |
33
|
|
|
$fighters = $group->getFightersWithBye(); |
34
|
|
|
|
35
|
|
|
$fighter1 = $fighters->get(0); |
36
|
|
|
$fighter2 = $fighters->get(1); |
37
|
|
|
$fighter3 = $fighters->get(2); |
38
|
|
|
|
39
|
|
View Code Duplication |
switch ($numGroup) { |
|
|
|
|
40
|
|
|
case 1: |
41
|
|
|
$competitor1 = $fighter1; |
42
|
|
|
$competitor2 = $fighter2; |
43
|
|
|
break; |
44
|
|
|
case 2: |
45
|
|
|
$competitor1 = $fighter2; |
46
|
|
|
$competitor2 = $fighter3; |
47
|
|
|
break; |
48
|
|
|
case 3: |
49
|
|
|
$competitor1 = $fighter3; |
50
|
|
|
$competitor2 = $fighter1; |
51
|
|
|
break; |
52
|
|
|
} |
53
|
|
|
$order = self::saveFight($group, $competitor1, $competitor2, $order); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Save a Fight. |
60
|
|
|
* |
61
|
|
|
* @param Collection $group |
62
|
|
|
* @param int $numGroup |
63
|
|
|
* @param int $order |
64
|
|
|
*/ |
65
|
|
|
public static function saveFight2(FightersGroup $group, $numGroup = 1, $order = 1) // TODO Rename it, bad name |
66
|
|
|
{ |
67
|
|
|
$competitor1 = $competitor2 = null; |
68
|
|
|
|
69
|
|
|
$fighters = $group->getFightersWithBye(); |
70
|
|
|
$fighter1 = $fighters->get(0); |
71
|
|
|
$fighter2 = $fighters->get(1); |
72
|
|
|
$fighter3 = $fighters->get(2); |
73
|
|
|
|
74
|
|
View Code Duplication |
switch ($numGroup) { |
|
|
|
|
75
|
|
|
case 1: |
76
|
|
|
$competitor1 = $fighter1; |
77
|
|
|
$competitor2 = $fighter2; |
78
|
|
|
break; |
79
|
|
|
case 2: |
80
|
|
|
$competitor1 = $fighter2; |
81
|
|
|
$competitor2 = $fighter3; |
82
|
|
|
break; |
83
|
|
|
case 3: |
84
|
|
|
$competitor1 = $fighter3; |
85
|
|
|
$competitor2 = $fighter1; |
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
self::saveFight($group, $competitor1, $competitor2, $order); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $group |
93
|
|
|
* @param $competitor1 |
94
|
|
|
* @param $competitor2 |
95
|
|
|
* @param $order |
96
|
|
|
* |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
|
|
private static function saveFight($group, $competitor1, $competitor2, $order) |
100
|
|
|
{ |
101
|
|
|
$fight = new Fight(); |
102
|
|
|
$fight->fighters_group_id = $group->id; |
|
|
|
|
103
|
|
|
$fight->c1 = optional($competitor1)->id; |
|
|
|
|
104
|
|
|
$fight->c2 = optional($competitor2)->id; |
|
|
|
|
105
|
|
|
$fight->short_id = $order++; |
|
|
|
|
106
|
|
|
$fight->area = $group->area; |
|
|
|
|
107
|
|
|
$fight->save(); |
108
|
|
|
return $order; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.