Completed
Push — master ( c1ba47...3b776f )
by Benjamin
10:05 queued 04:46
created

AbstractTeamRuleTrait::calculateTeamValue()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 20
c 1
b 0
f 0
nc 13
nop 2
dl 0
loc 31
rs 8.0555
1
<?php
2
3
namespace Obblm\Core\Helper\Rule\Traits;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Obblm\Core\Entity\PlayerVersion;
7
use Obblm\Core\Entity\Team;
8
use Obblm\Core\Entity\TeamVersion;
9
use Obblm\Core\Exception\InvalidArgumentException;
10
use Obblm\Core\Exception\NotFoundRuleKeyExcepion;
11
use Obblm\Core\Exception\NoVersionException;
12
use Obblm\Core\Helper\PlayerHelper;
13
use Obblm\Core\Helper\Rule\Roster\Roster;
14
use Obblm\Core\Validator\Constraints\TeamValue;
15
16
/****************************
17
 * TEAM INFORMATION METHODS
18
 ***************************/
19
trait AbstractTeamRuleTrait
20
{
21
    public function getAvailableRosters(): ArrayCollection
22
    {
23
        return $this->getRosters();
0 ignored issues
show
Bug introduced by
It seems like getRosters() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        return $this->/** @scrutinizer ignore-call */ getRosters();
Loading history...
24
    }
25
26
    /**
27
     * @return int
28
     */
29
    public function getMaxTeamCost():int
30
    {
31
        return ($this->rule['max_team_cost']) ? $this->rule['max_team_cost'] : TeamValue::LIMIT;
32
    }
33
34
    /**
35
     * @param Team $team
36
     * @return array
37
     */
38
    public function getTeamAvailablePlayerTypes(Team $team)
39
    {
40
        return $this->getAvailablePlayerTypes($team->getRoster());
0 ignored issues
show
Bug introduced by
The method getAvailablePlayerTypes() does not exist on Obblm\Core\Helper\Rule\T...s\AbstractTeamRuleTrait. Did you maybe mean getAvailableRosters()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        return $this->/** @scrutinizer ignore-call */ getAvailablePlayerTypes($team->getRoster());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
    }
42
43
    /**
44
     * @param Team $team
45
     * @return int
46
     * @throws \Exception
47
     */
48
    public function getRerollCost(Team $team):int
49
    {
50
        /** @var Roster $roster */
51
        $roster = $this->getRosters()->get($team->getRoster());
52
        return (int) $roster->getRerollCost();
53
    }
54
55
    /**
56
     * @param Team $team
57
     * @return int
58
     */
59
    public function getApothecaryCost(Team $team):int
0 ignored issues
show
Unused Code introduced by
The parameter $team is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

59
    public function getApothecaryCost(/** @scrutinizer ignore-unused */ Team $team):int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
        return (int) $this->rule['sidelines_cost']['apothecary'];
62
    }
63
64
    /**
65
     * @param Team $team
66
     * @return int
67
     */
68
    public function getCheerleadersCost(Team $team):int
0 ignored issues
show
Unused Code introduced by
The parameter $team is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

68
    public function getCheerleadersCost(/** @scrutinizer ignore-unused */ Team $team):int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        return (int) $this->rule['sidelines_cost']['cheerleaders'];
71
    }
72
73
    /**
74
     * @param Team $team
75
     * @return int
76
     */
77
    public function getAssistantsCost(Team $team):int
0 ignored issues
show
Unused Code introduced by
The parameter $team is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

77
    public function getAssistantsCost(/** @scrutinizer ignore-unused */ Team $team):int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
        return (int) $this->rule['sidelines_cost']['assistants'];
80
    }
81
82
    /**
83
     * @param Team $team
84
     * @return int
85
     */
86
    public function getPopularityCost(Team $team):int
0 ignored issues
show
Unused Code introduced by
The parameter $team is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

86
    public function getPopularityCost(/** @scrutinizer ignore-unused */ Team $team):int

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
87
    {
88
        return (int) $this->rule['sidelines_cost']['popularity'];
89
    }
90
91
    /**
92
     * @param Team $team
93
     * @return bool
94
     * @throws \Exception
95
     */
96
    public function couldHaveApothecary(Team $team):bool
97
    {
98
        /** @var Roster $roster */
99
        $roster = $this->getRosters()->get($team->getRoster());
100
        return (bool) $roster->canHaveApothecary();
101
    }
102
103
    public function calculateTeamRate(TeamVersion $version):?int
104
    {
105
        return $this->calculateTeamValue($version) / 10000;
106
    }
107
108
    public function calculateTeamValue(TeamVersion $version, bool $excludeDisposable = false):int
109
    {
110
        if (!$version->getTeam()) {
111
            throw new InvalidArgumentException();
112
        }
113
        $value = 0;
114
        // Players
115
        // TODO: Bug => players are not version's one (because od dead players)
116
        foreach ($version->getTeam()->getAvailablePlayers() as $player) {
117
            if ($player->getType()) {
118
                try {
119
                    $playerVersion = PlayerHelper::getLastVersion($player);
120
                } catch (NoVersionException $e) { // It's a new player !
121
                    $playerVersion = (new PlayerVersion());
122
                    $player->addVersion($playerVersion);
123
                    $version->addPlayerVersion($playerVersion);
124
                    $this->setPlayerDefaultValues($playerVersion);
125
                }
126
                if (!$playerVersion->isMissingNextGame() && !($this->playerIsDisposable($playerVersion) && $excludeDisposable)) {
127
                    $value += $playerVersion->getValue();
128
                }
129
            }
130
        }
131
        // Sidelines
132
        $value += $version->getRerolls() * $this->getRerollCost($version->getTeam());
133
        $value += $version->getAssistants() * $this->getAssistantsCost($version->getTeam());
134
        $value += $version->getCheerleaders() * $this->getCheerleadersCost($version->getTeam());
135
        $value += $version->getPopularity() * $this->getPopularityCost($version->getTeam());
136
        $value += ($version->getApothecary()) ? $this->getApothecaryCost($version->getTeam()) : 0;
137
138
        return $value;
139
    }
140
141
    abstract public function setPlayerDefaultValues(PlayerVersion $version): ?PlayerVersion;
142
    abstract public function playerIsDisposable(PlayerVersion $version):bool;
143
144
    /**
145
     * @return array
146
     */
147
    public function getInjuriesTable():array
148
    {
149
        return $this->getInjuries();
0 ignored issues
show
Bug introduced by
The method getInjuries() does not exist on Obblm\Core\Helper\Rule\T...s\AbstractTeamRuleTrait. Did you maybe mean getInjuriesTable()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
        return $this->/** @scrutinizer ignore-call */ getInjuries();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
    }
151
152
    public function getMaxPlayersByType($rosterKey, $typeKey): int
153
    {
154
        /** @var Roster $roster */
155
        $roster = $this->getRosters()->get($rosterKey);
156
        if (!$type = $roster->getPlayerTypes()[$typeKey]) {
157
            throw new NotFoundRuleKeyExcepion($typeKey, 'toto');
0 ignored issues
show
Bug introduced by
'toto' of type string is incompatible with the type integer expected by parameter $code of Obblm\Core\Exception\Not...Excepion::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
            throw new NotFoundRuleKeyExcepion($typeKey, /** @scrutinizer ignore-type */ 'toto');
Loading history...
158
        }
159
        return (int) $type['max'];
160
    }
161
}
162