Completed
Push — master ( c60305...7f3e18 )
by Vincenzo
02:47
created

MatchSimulator::simulateSimpleResult()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 22
Ratio 100 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 22
loc 22
rs 8.9197
cc 4
eloc 13
nc 2
nop 1
1
<?php
2
3
4
namespace App\Lib\DsManager\Helpers;
5
6
7
use App\Lib\DsManager\Models\Orm\Match;
8
use App\Lib\DsManager\Models\Orm\Match as MatchOrm;
9
use App\Lib\DsManager\Models\Orm\MatchResult;
10
11
/**
12
 * Class MatchSimulator
13
 * @package App\Lib\DsManager\Helpers
14
 */
15
class MatchSimulator
16
{
17
    public static function simulateRound($roundId)
18
    {
19
        $matches = Match::where(
20
            [
21
                'league_round_id' => $roundId
22
            ]
23
        )->get();
24
        $result = [];
25
        foreach ($matches as $match) {
26
            $result[] = self::simulateSimpleResult($match->id)->toArray();
27
        }
28
        return json_encode($result);
29
    }
30
31
    /**
32
     * @param $matchId
33
     * @return mixed
34
     */
35 View Code Duplication
    public static function simulateCompleteResult($matchId)
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...
36
    {
37
        $result = MatchResult::complete()->where(
38
            [
39
                'id' => $matchId
40
            ]
41
        )->first();
42
43
        if (!empty($result)
44
            && !$result->simulated
45
            && self::simulate($matchId) === 1
46
        ) {
47
            $result = MatchResult::complete()
48
                ->where(
49
                    [
50
                        'id' => $matchId
51
                    ]
52
                )->first();
53
        }
54
55
        return $result;
56
    }
57
58 View Code Duplication
    public
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...
59
    static function simulateSimpleResult($matchId)
60
    {
61
        $result = MatchResult::teams()->where(
62
            [
63
                'id' => $matchId
64
            ]
65
        )->first();
66
67
        if (!empty($result)
68
            && !$result->simulated
69
            && self::simulate($matchId) === 1
70
        ) {
71
            $result = MatchResult::teams()
72
                ->where(
73
                    [
74
                        'id' => $matchId
75
                    ]
76
                )->first();
77
        }
78
        return $result;
79
    }
80
81
    private
82
    static function simulate($matchId)
83
    {
84
        $match = \App\Lib\DsManager\Models\Match::fromArray(
85
            MatchOrm::complete()
0 ignored issues
show
Bug introduced by
The method complete() does not exist on App\Lib\DsManager\Models\Orm\Match. Did you maybe mean scopeComplete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
86
                ->where(
87
                    [
88
                        'id' => $matchId
89
                    ]
90
                )->first()->toArray()
91
        );
92
        $matchResult = $match->simulate()->toArray();
93
        $result = MatchResult::where(
94
            [
95
                'id' => $matchId
96
            ]
97
        )->update(
98
            MatchResult::resolveAttributes(
99
                $matchResult,
100
                $matchId
101
            )
102
        );
103
        return $result;
104
    }
105
}