Test Failed
Push — master ( b6687a...d5e75a )
by Julien
06:32
created

PreliminaryFight::saveFights()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
cc 5
eloc 22
nc 5
nop 2
1
<?php
2
3
namespace Xoco70\LaravelTournaments\Models;
4
5
use Illuminate\Support\Collection;
6
7
class PreliminaryFight extends Fight
8
{
9
10 View Code Duplication
    public function __construct(Fight $fight = null)
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...
11
    {
12
        parent::__construct();
13
        if ($fight!=null){
14
            $this->id= $fight->id;
15
            $this->short_id= $fight->short_id;
16
            $this->fighters_group_id= $fight->fighters_group_id;
17
            $this->c1= $fight->c1;
18
            $this->c2= $fight->c2;
19
        }
20
    }
21
22
    /**
23
     * Save a Fight.
24
     *
25
     * @param Collection $groups
26
     * @param int $numGroup
27
     */
28
    public static function saveFights(Collection $groups, $numGroup = 1)
29
    {
30
        $competitor1 = $competitor2 = null;
31
        $order = 1;
32
33
        foreach ($groups as $group) {
34
35
            $fighters = $group->getFightersWithBye();
36
37
            $fighter1 = $fighters->get(0);
38
            $fighter2 = $fighters->get(1);
39
            $fighter3 = $fighters->get(2);
40
41
            switch ($numGroup) {
42
                case 1:
43
                    $competitor1 = $fighter1;
44
                    $competitor2 = $fighter2;
45
                    break;
46
                case 2:
47
                    $competitor1 = $fighter2;
48
                    $competitor2 = $fighter3;
49
                    break;
50
                case 3:
51
                    $competitor1 = $fighter3;
52
                    $competitor2 = $fighter1;
53
                    break;
54
            }
55
            $order = self::saveFight($group, $competitor1, $competitor2, $order);
56
        }
57
    }
58
59
    /**
60
     * @param $group
61
     * @param $competitor1
62
     * @param $competitor2
63
     * @param $order
64
     * @return mixed
65
     */
66
    private static function saveFight($group, $competitor1, $competitor2, $order)
67
    {
68
        $fight = new Fight();
69
        $fight->fighters_group_id = $group->id;
70
        $fight->c1 = $competitor1 != null ? $competitor1->id : null;
71
        $fight->c2 = $competitor2 != null ? $competitor2->id : null;
72
        $fight->short_id = $order++;
73
        $fight->area = $group->area;
74
        $fight->save();
75
        return $order;
76
    }
77
}