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

MatchSimulator::simulateRound()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 2
eloc 8
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\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
}