Passed
Push — master ( 32373f...f18c17 )
by Benjamin
05:50 queued 01:43
created

AbstractTeamRuleTrait::getInjuriesTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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\NotFoundKeyException;
11
use Obblm\Core\Exception\NotFoundRuleKeyException;
12
use Obblm\Core\Exception\NoVersionException;
13
use Obblm\Core\Helper\PlayerHelper;
14
use Obblm\Core\Helper\Rule\Roster\Roster;
15
use Obblm\Core\Validator\Constraints\TeamValue;
16
17
/****************************
18
 * TEAM INFORMATION METHODS
19
 ***************************/
20
trait AbstractTeamRuleTrait
21
{
22
    /**
23
     * @return int
24
     */
25 1
    public function getMaxTeamCost():int
26
    {
27 1
        return ($this->rule['max_team_cost']) ? $this->rule['max_team_cost'] : TeamValue::LIMIT;
28
    }
29
30
    /**
31
     * @param Team $team
32
     * @return int
33
     */
34 1
    public function getRerollCost(Team $team):int
35
    {
36
        /** @var Roster $roster */
37 1
        $roster = $this->getRosters()->get($team->getRoster());
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 1
    public function calculateTeamRate(TeamVersion $version):?int
90
    {
91 1
        return $this->calculateTeamValue($version) / 10000;
92
    }
93
94 2
    public function calculateTeamValue(TeamVersion $version, bool $excludeDisposable = false):int
95
    {
96 2
        if (!$version->getTeam()) {
97 1
            throw new InvalidArgumentException();
98
        }
99 1
        $value = 0;
100
        // Players
101
        // TODO: Bug => players are not version's one (because od dead players)
102 1
        foreach ($version->getTeam()->getAvailablePlayers() as $player) {
103 1
            if ($player->getType()) {
104
                try {
105 1
                    $playerVersion = PlayerHelper::getLastVersion($player);
106 1
                } catch (NoVersionException $e) { // It's a new player !
107 1
                    $playerVersion = (new PlayerVersion());
108 1
                    $player->addVersion($playerVersion);
109 1
                    $version->addPlayerVersion($playerVersion);
110 1
                    $this->setPlayerDefaultValues($playerVersion);
111
                }
112 1
                if (!$playerVersion->isMissingNextGame() && !($this->playerIsDisposable($playerVersion) && $excludeDisposable)) {
113 1
                    $value += $playerVersion->getValue();
114
                }
115
            }
116
        }
117
        // Sidelines
118 1
        $value += $version->getRerolls() * $this->getRerollCost($version->getTeam());
119 1
        $value += $version->getAssistants() * $this->getAssistantsCost($version->getTeam());
120 1
        $value += $version->getCheerleaders() * $this->getCheerleadersCost($version->getTeam());
121 1
        $value += $version->getPopularity() * $this->getPopularityCost($version->getTeam());
122 1
        $value += ($version->getApothecary()) ? $this->getApothecaryCost($version->getTeam()) : 0;
123
124 1
        return $value;
125
    }
126
127
    abstract public function setPlayerDefaultValues(PlayerVersion $version): ?PlayerVersion;
128
    abstract public function playerIsDisposable(PlayerVersion $version):bool;
129
    abstract public function getRosters():ArrayCollection;
130
131
    /**
132
     * @return array
133
     */
134
    public function getInjuriesTable():array
135
    {
136
        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

136
        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...
137
    }
138
139 2
    public function getMaxPlayersByType($rosterKey, $typeKey): int
140
    {
141
        /** @var Roster $roster */
142 2
        $roster = $this->getRosters()->get($rosterKey);
143 2
        if (!isset($roster->getPlayerTypes()[$typeKey])) {
144 1
            throw new NotFoundKeyException($typeKey, "getRosters()->get('$rosterKey')->getPlayerTypes()", self::class);
145
        }
146 1
        $type = $roster->getPlayerTypes()[$typeKey];
147 1
        return (int) $type['max'];
148
    }
149
}
150