Completed
Push — master ( 5d04a1...0cf74a )
by Vincenzo
02:49
created

MatchSimulator::simulate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 52
rs 8.6868
cc 5
eloc 30
nc 4
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\MatchResult;
9
10
/**
11
 * Class MatchSimulator
12
 * @package App\Lib\DsManager\Helpers
13
 */
14
class MatchSimulator
15
{
16
    public static function simulateRound($roundId)
17
    {
18
        $matches = Match::where(
19
            [
20
                'league_round_id' => $roundId
21
            ]
22
        )->get();
23
        $result = [];
24
        foreach ($matches as $match) {
25
            $result[] = self::simulate($match->id, false)->toArray();
26
        }
27
        return $result;
28
    }
29
30
    /**
31
     * @param $matchId
32
     * @param bool $completeResult
33
     * @return mixed
34
     */
35
    public static function simulate($matchId, $completeResult = true)
36
    {
37
        $result = MatchResult::complete()
38
            ->where(
39
                [
40
                    'id' => $matchId
41
                ]
42
            )->first();
43
        if (!empty($result) && !$result->simulated) {
44
            //simulate match
45
            $match = \App\Lib\DsManager\Models\Match::fromArray(
46
                \App\Lib\DsManager\Models\Orm\Match::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...
47
                    ->where(
48
                        [
49
                            'id' => $matchId
50
                        ]
51
                    )->first()->toArray()
52
            );
53
54
            $matchResult = $match->simulate()->toArray();
55
56
            $result = MatchResult::where(
57
                [
58
                    'id' => $matchId
59
                ]
60
            )->update(
61
                MatchResult::resolveAttributes(
62
                    $matchResult,
63
                    $matchId
64
                )
65
            );
66
            if ($result === 1) {
67
                if ($completeResult) {
68
                    $result = MatchResult::complete()
69
                        ->where(
70
                            [
71
                                'id' => $matchId
72
                            ]
73
                        )->first();
74
                } else {
75
                    $result = MatchResult::teams()
76
                        ->where(
77
                            [
78
                                'id' => $matchId
79
                            ]
80
                        )->first();
81
                }
82
            }
83
84
        }
85
        return $result;
86
    }
87
}