Completed
Push — master ( 0a3a64...75d382 )
by Julien
02:47
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\KendoTournaments\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
        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 a Fight.
23
     *
24
     * @param Collection $groups
25
     * @param int $numGroup
26
     */
27
    public static function saveFights($groups, $numGroup = 1)
28
    {
29
        $competitor1 = $competitor2 = null;
30
        $order = 1;
31
32
        foreach ($groups as $group) {
33
34
            $fighters = $group->getFighters();
35
36
            $fighter1 = $fighters->get(0);
37
            $fighter2 = $fighters->get(1);
38
            $fighter3 = $fighters->get(2);
39
40
            switch ($numGroup) {
41
                case 1:
42
                    $competitor1 = $fighter1;
43
                    $competitor2 = $fighter2;
44
                    break;
45
                case 2:
46
                    $competitor1 = $fighter2;
47
                    $competitor2 = $fighter3;
48
                    break;
49
                case 3:
50
                    $competitor1 = $fighter3;
51
                    $competitor2 = $fighter1;
52
                    break;
53
            }
54
            $order = self::saveFight($group, $competitor1, $competitor2, $order);
55
        }
56
    }
57
58
    /**
59
     * @param $group
60
     * @param $competitor1
61
     * @param $competitor2
62
     * @param $order
63
     * @return mixed
64
     */
65
    private static function saveFight($group, $competitor1, $competitor2, $order)
66
    {
67
        $fight = new Fight();
68
        $fight->fighters_group_id = $group->id;
69
        $fight->c1 = $competitor1 != null ? $competitor1->id : null;
70
        $fight->c2 = $competitor2 != null ? $competitor2->id : null;
71
        $fight->short_id = $order++;
72
        $fight->area = $group->area;
73
        $fight->save();
74
        return $order;
75
    }
76
}