Completed
Branch v1.x-dev (48843d)
by Benjamin
06:26
created

AbstractTeamRuleTrait::calculateTeamValue()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

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
ccs 0
cts 20
cp 0
crap 90
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\NotFoundRuleKeyException;
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
    /**
22
     * @return int
23
     */
24 1
    public function getMaxTeamCost():int
25
    {
26 1
        return ($this->rule['max_team_cost']) ? $this->rule['max_team_cost'] : TeamValue::LIMIT;
27
    }
28
29
    /**
30
     * @param Team $team
31
     * @return int
32
     * @throws \Exception
33
     */
34 1
    public function getRerollCost(Team $team):int
35
    {
36
        /** @var Roster $roster */
37 1
        $roster = $this->getRosters()->get($team->getRoster());
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

37
        $roster = $this->/** @scrutinizer ignore-call */ getRosters()->get($team->getRoster());
Loading history...
38 1
        return (int) $roster->getRerollCost();
39
    }
40
41
    /**
42
     * @param Team $team
43
     * @return int
44
     */
45 1
    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

45
    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...
46
    {
47 1
        return (int) $this->rule['sidelines_cost']['apothecary'];
48
    }
49
50
    /**
51
     * @param Team $team
52
     * @return int
53
     */
54 1
    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

54
    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...
55
    {
56 1
        return (int) $this->rule['sidelines_cost']['cheerleaders'];
57
    }
58
59
    /**
60
     * @param Team $team
61
     * @return int
62
     */
63 1
    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

63
    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...
64
    {
65 1
        return (int) $this->rule['sidelines_cost']['assistants'];
66
    }
67
68
    /**
69
     * @param Team $team
70
     * @return int
71
     */
72 1
    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

72
    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...
73
    {
74 1
        return (int) $this->rule['sidelines_cost']['popularity'];
75
    }
76
77
    /**
78
     * @param Team $team
79
     * @return bool
80
     * @throws \Exception
81
     */
82 1
    public function couldHaveApothecary(Team $team):bool
83
    {
84
        /** @var Roster $roster */
85 1
        $roster = $this->getRosters()->get($team->getRoster());
86 1
        return (bool) $roster->canHaveApothecary();
87
    }
88
89
    public function calculateTeamRate(TeamVersion $version):?int
90
    {
91
        return $this->calculateTeamValue($version) / 10000;
92
    }
93
94
    public function calculateTeamValue(TeamVersion $version, bool $excludeDisposable = false):int
95
    {
96
        if (!$version->getTeam()) {
97
            throw new InvalidArgumentException();
98
        }
99
        $value = 0;
100
        // Players
101
        // TODO: Bug => players are not version's one (because od dead players)
102
        foreach ($version->getTeam()->getAvailablePlayers() as $player) {
103
            if ($player->getType()) {
104
                try {
105
                    $playerVersion = PlayerHelper::getLastVersion($player);
106
                } catch (NoVersionException $e) { // It's a new player !
107
                    $playerVersion = (new PlayerVersion());
108
                    $player->addVersion($playerVersion);
109
                    $version->addPlayerVersion($playerVersion);
110
                    $this->setPlayerDefaultValues($playerVersion);
111
                }
112
                if (!$playerVersion->isMissingNextGame() && !($this->playerIsDisposable($playerVersion) && $excludeDisposable)) {
113
                    $value += $playerVersion->getValue();
114
                }
115
            }
116
        }
117
        // Sidelines
118
        $value += $version->getRerolls() * $this->getRerollCost($version->getTeam());
119
        $value += $version->getAssistants() * $this->getAssistantsCost($version->getTeam());
120
        $value += $version->getCheerleaders() * $this->getCheerleadersCost($version->getTeam());
121
        $value += $version->getPopularity() * $this->getPopularityCost($version->getTeam());
122
        $value += ($version->getApothecary()) ? $this->getApothecaryCost($version->getTeam()) : 0;
123
124
        return $value;
125
    }
126
127
    abstract public function setPlayerDefaultValues(PlayerVersion $version): ?PlayerVersion;
128
    abstract public function playerIsDisposable(PlayerVersion $version):bool;
129
130
    /**
131
     * @return array
132
     */
133
    public function getInjuriesTable():array
134
    {
135
        return (array) $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

135
        return (array) $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...
136
    }
137
138
    public function getMaxPlayersByType($rosterKey, $typeKey): int
139
    {
140
        /** @var Roster $roster */
141
        $roster = $this->getRosters()->get($rosterKey);
142
        if (!$type = $roster->getPlayerTypes()[$typeKey]) {
143
            throw new NotFoundRuleKeyException($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...xception::__construct(). ( Ignorable by Annotation )

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

143
            throw new NotFoundRuleKeyException($typeKey, /** @scrutinizer ignore-type */ 'toto');
Loading history...
144
        }
145
        return (int) $type['max'];
146
    }
147
}
148