Completed
Branch develop (598d0f)
by Benjamin
03:23
created

TeamHelper::getRosterNameForTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Obblm\Core\Helper;
4
5
use Obblm\Core\Entity\Team;
6
use Obblm\Core\Entity\TeamVersion;
7
use Obblm\Core\Contracts\RuleHelperInterface;
8
9
/**
10
 * Class TeamHelper
11
 * @package Obblm\Core\Helper
12
 */
13
class TeamHelper
14
{
15
    private $ruleHelper;
16
17
    public function __construct(RuleHelper $ruleHelper)
18
    {
19
        $this->ruleHelper = $ruleHelper;
20
    }
21
22
    /**
23
     * @param Team $team
24
     * @return RuleHelperInterface
25
     * @throws \Exception
26
     */
27
    public function getRuleHelper(Team $team):RuleHelperInterface
28
    {
29
        if (!$team->getRule()) {
30
            throw new \Exception('This team does not have a rule');
31
        }
32
        return $this->ruleHelper->getHelper($team->getRule());
33
    }
34
35
    public static function getLastVersion(Team  $team):TeamVersion
36
    {
37
        $versions = $team->getVersions();
38
        /** @var TeamVersion $last */
39
        $last = $versions->first();
40
        if ($last) {
0 ignored issues
show
introduced by
$last is of type Obblm\Core\Entity\TeamVersion, thus it always evaluated to true.
Loading history...
41
            return $last;
42
        }
43
        $version = new TeamVersion();
44
        $team->addVersion($version);
45
        return $version;
46
    }
47
48
    /**********************
49
     * TEAM HELPER METHODS
50
     **********************/
51
52
    /**
53
     * @param TeamVersion $version
54
     * @return int
55
     * @throws \Exception
56
     */
57
    public function calculateTeamValue(TeamVersion $version):int
58
    {
59
        return $this->getRuleHelper($version->getTeam())
0 ignored issues
show
Bug introduced by
It seems like $version->getTeam() can also be of type null; however, parameter $team of Obblm\Core\Helper\TeamHelper::getRuleHelper() does only seem to accept Obblm\Core\Entity\Team, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        return $this->getRuleHelper(/** @scrutinizer ignore-type */ $version->getTeam())
Loading history...
Bug Best Practice introduced by
The expression return $this->getRuleHel...lateTeamValue($version) could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
60
            ->calculateTeamValue($version);
61
    }
62
63
    /**
64
     * @param TeamVersion $version
65
     * @return int
66
     * @throws \Exception
67
     */
68
    public function calculateTeamRate(TeamVersion $version):int
69
    {
70
        return $this->getRuleHelper($version->getTeam())
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getRuleHel...ulateTeamRate($version) could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
It seems like $version->getTeam() can also be of type null; however, parameter $team of Obblm\Core\Helper\TeamHelper::getRuleHelper() does only seem to accept Obblm\Core\Entity\Team, maybe add an additional type check? ( Ignorable by Annotation )

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

70
        return $this->getRuleHelper(/** @scrutinizer ignore-type */ $version->getTeam())
Loading history...
71
            ->calculateTeamRate($version);
72
    }
73
74
    /**
75
     * @param Team $team
76
     * @return TeamVersion
77
     * @throws \Psr\Cache\InvalidArgumentException
78
     */
79
    public function createNewTeamVersion(Team $team):TeamVersion
80
    {
81
        return (TeamHelper::getLastVersion($team))
82
            ->setTreasure($this->ruleHelper->getHelper($team->getRule())->getMaxTeamCost());
83
    }
84
    public function destructTeamVersion(Team $team):TeamVersion
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

84
    public function destructTeamVersion(/** @scrutinizer ignore-unused */ Team $team):TeamVersion

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...
85
    {
86
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Obblm\Core\Entity\TeamVersion. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
87
88
    /****************************
89
     * TEAM INFORMATION METHODS
90
     ***************************/
91
92
    /**
93
     * @param Team $team
94
     * @return int
95
     * @throws \Exception
96
     */
97
    public function getRerollCost(Team $team):int
98
    {
99
        return (int) $this->getRuleHelper($team)->getRerollCost($team);
100
    }
101
102
    /**
103
     * @param Team $team
104
     * @return int
105
     * @throws \Exception
106
     */
107
    public function getApothecaryCost(Team $team):int
108
    {
109
        return (int) $this->getRuleHelper($team)->getApothecaryCost($team);
110
    }
111
112
    /**
113
     * @param Team $team
114
     * @return int
115
     * @throws \Exception
116
     */
117
    public function getCheerleadersCost(Team $team):int
118
    {
119
        return (int) $this->getRuleHelper($team)->getCheerleadersCost($team);
120
    }
121
122
    /**
123
     * @param Team $team
124
     * @return int
125
     * @throws \Exception
126
     */
127
    public function getAssistantsCost(Team $team):int
128
    {
129
        return (int) $this->getRuleHelper($team)->getAssistantsCost($team);
130
    }
131
132
    /**
133
     * @param Team $team
134
     * @return int
135
     * @throws \Exception
136
     */
137
    public function getPopularityCost(Team $team):int
138
    {
139
        return (int) $this->getRuleHelper($team)->getPopularityCost($team);
140
    }
141
142
    /**
143
     * @param Team $team
144
     * @return bool
145
     * @throws \Exception
146
     */
147
    public function couldHaveApothecary(Team $team):bool
148
    {
149
        if (!$team->getRule()) {
150
            throw new \Exception('This team does not have a rule');
151
        }
152
        return (bool) $this->getRuleHelper($team)->couldHaveApothecary($team);
153
    }
154
}
155