MatchResult::pickAScorer()   B
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 23
rs 8.7972
cc 4
eloc 18
nc 2
nop 1
1
<?php
2
3
namespace App\Lib\DsManager\Models;
4
5
use App\Lib\DsManager\Helpers\Randomizer;
6
use App\Lib\DsManager\Models\Common\DsManagerModel;
7
use App\Lib\Helpers\Config;
8
9
10
/**
11
 * Class MatchResult
12
 * @package App\Lib\DsManager\Models
13
 */
14
class MatchResult extends DsManagerModel
15
{
16
    /**
17
     * @var
18
     */
19
    private $goalHome;
20
    /**
21
     * @var
22
     */
23
    private $goalAway;
24
    /**
25
     * @var Team
26
     */
27
    private $homeTeam;
28
    /**
29
     * @var Team
30
     */
31
    private $awayTeam;
32
33
    /**
34
     * MatchResult constructor.
35
     * @param $goalHome
36
     * @param $goalAway
37
     * @param Team $home
38
     * @param Team $away
39
     */
40
    public function __construct($goalHome, $goalAway, Team $home, Team $away)
41
    {
42
        $this->goalHome = $goalHome;
43
        $this->goalAway = $goalAway;
44
        $this->homeTeam = $home;
45
        $this->awayTeam = $away;
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public function getWinnerLoser()
52
    {
53
        $isDraw = false;
54
        $winner = $this->homeTeam;
55
        $loser = $this->awayTeam;
56
        if ($this->goalAway == $this->goalHome) {
57
            $isDraw = true;
58
        } else if ($this->goalHome < $this->goalAway) {
59
            $winner = $this->awayTeam;
60
            $loser = $this->homeTeam;
61
        }
62
        return [
63
            'is_draw' => $isDraw,
64
            'winner_id' => $winner->id,
65
            'loser_id' => $loser->id
66
        ];
67
    }
68
69
    /**
70
     * @return array
71
     */
72
    public function toArray()
73
    {
74
        $result = [];
75
        $result['home_team_id'] = $this->homeTeam->id;
76
        $result['away_team_id'] = $this->awayTeam->id;
77
        $result['goal_home'] = $this->goalHome;
78
        $result['goal_away'] = $this->goalAway;
79
        $result['info'] = $this->getWinnerLoser();
80
        $result['info']['scorers'] = $this->getScorers();
81
        $result['simulated'] = true;
82
        return $result;
83
    }
84
    /**
85
     * @return array
86
     */
87
    private function getScorers()
88
    {
89
        $scorers = [
90
            'home' => [],
91
            'away' => []
92
        ];
93
        for ($i = 0; $i < $this->goalHome; $i++) {
94
            $scorers['home'][] = $this->pickAScorer($this->homeTeam);
95
        }
96
        for ($i = 0; $i < $this->goalAway; $i++) {
97
            $scorers['away'][] = $this->pickAScorer($this->awayTeam);
98
        }
99
        return $scorers;
100
    }
101
102
    /**
103
     * @param Team $team
104
     * @return Player
105
     */
106
    private function pickAScorer(Team $team)
107
    {
108
        $player = null;
109
        if (Randomizer::boolOnPercentage(70)) {
110
            $roles = Config::get('modules.roles');
111
            $forwards = array_splice($roles, count($roles) / 2);
112
            $pos = array_rand($forwards);
113
            unset($forwards[$pos]);
114
            $player = $team->getBestPlayerForRole($pos);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $player is correct as $team->getBestPlayerForRole($pos) (which targets App\Lib\DsManager\Models...:getBestPlayerForRole()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
115
            while (empty($player)) {
116
                if (!empty($forwards)) {
117
                    $pos = array_rand($forwards);
118
                    unset($forwards[$pos]);
119
                    $player = $team->getBestPlayerForRole($pos);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $player is correct as $team->getBestPlayerForRole($pos) (which targets App\Lib\DsManager\Models...:getBestPlayerForRole()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
120
                } else {
121
                    $player = $team->roster[array_rand($team->roster)];
122
                }
123
            }
124
        } else {
125
            $player = $team->roster[array_rand($team->roster)];
126
        }
127
        return $player;
128
    }
129
130
}