Completed
Push — master ( c27acc...5e9888 )
by Vincenzo
02:27
created

MatchSimulator::simulate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 23
rs 9.0856
cc 1
eloc 14
nc 1
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
    /**
18
     * @param $roundId
19
     * @return string
20
     */
21
    public static function simulateRound($roundId)
22
    {
23
        $matches = Match::where(
24
            [
25
                'league_round_id' => $roundId
26
            ]
27
        )->get();
28
        $result = [];
29
        foreach ($matches as $match) {
30
            $result[] = self::simulateSimpleResult($match->id)->toArray();
31
        }
32
        return json_encode($result);
33
    }
34
35
    /**
36
     * @param $matchId
37
     * @return mixed
38
     */
39 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...
40
    {
41
        $result = self::getCompleteResult($matchId);
42
        if (!empty($result)
43
            && !$result->simulated
0 ignored issues
show
Documentation introduced by
The property simulated does not exist on object<App\Lib\DsManager\Models\Orm\MatchResult>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
            && self::simulate($matchId) === 1
45
        ) {
46
            $result = self::getCompleteResult($matchId);
47
        }
48
        return $result;
49
    }
50
51
    /**
52
     * @param $matchId
53
     * @return mixed
54
     */
55 View Code Duplication
    public static function simulateSimpleResult($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...
56
    {
57
        $result = self::getSimpleResult($matchId);
58
        if (!empty($result)
59
            && !$result->simulated
0 ignored issues
show
Documentation introduced by
The property simulated does not exist on object<App\Lib\DsManager\Models\Orm\MatchResult>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
60
            && self::simulate($matchId) === 1
61
        ) {
62
            $result = self::getSimpleResult($matchId);
63
        }
64
        return $result;
65
    }
66
67
    /**
68
     * @param $matchId
69
     * @return mixed
70
     */
71
    private static function simulate($matchId)
72
    {
73
        $match = \App\Lib\DsManager\Models\Match::fromArray(
74
            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...
75
                ->where(
76
                    [
77
                        'id' => $matchId
78
                    ]
79
                )->first()->toArray()
80
        );
81
        $matchResult = $match->simulate()->toArray();
82
        $result = MatchResult::where(
83
            [
84
                'id' => $matchId
85
            ]
86
        )->update(
87
            MatchResult::resolveAttributes(
88
                $matchResult,
89
                $matchId
90
            )
91
        );
92
        return $result;
93
    }
94
95
96
    /**
97
     * @param $matchId
98
     * @return MatchResult
99
     */
100
    private static function getCompleteResult($matchId)
101
    {
102
        return MatchResult::complete()->where(
103
            [
104
                'id' => $matchId
105
            ]
106
        )->first();
107
    }
108
109
    /**
110
     * @param $matchId
111
     * @return MatchResult
112
     */
113
    private static function getSimpleResult($matchId)
114
    {
115
        return MatchResult::teams()->where(
116
            [
117
                'id' => $matchId
118
            ]
119
        )->first();
120
    }
121
122
}