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

MatchSimulator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 0
cbo 3
dl 0
loc 74
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A simulateRound() 0 13 2
B simulate() 0 52 5
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
}