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

AbstractInducementRuleTrait   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 69
rs 10
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getInducementType() 0 3 1
A getStarPlayer() 0 6 2
A getInducementsByTeamOptions() 0 22 5
A getMaxStarPlayers() 0 3 1
A getInducements() 0 20 3
A getStarPlayers() 0 3 1
1
<?php
2
3
namespace Obblm\Core\Helper\Rule\Traits;
4
5
use Doctrine\Common\Collections\Criteria;
6
use Obblm\Core\Entity\Team;
7
use Obblm\Core\Exception\NotFoundRuleKeyExcepion;
8
use Obblm\Core\Helper\CoreTranslation;
9
use Obblm\Core\Helper\Rule\Inducement\Inducement;
10
use Obblm\Core\Helper\Rule\Inducement\InducementType;
11
12
/*********************
13
 * INDUCEMENT METHODS
14
 ********************/
15
trait AbstractInducementRuleTrait
16
{
17
    public function getInducementType(string $type):InducementType
18
    {
19
        return $this->getInducementTypes()[$type];
0 ignored issues
show
Bug introduced by
The method getInducementTypes() does not exist on Obblm\Core\Helper\Rule\T...ractInducementRuleTrait. Did you maybe mean getInducementType()? ( Ignorable by Annotation )

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

19
        return $this->/** @scrutinizer ignore-call */ getInducementTypes()[$type];

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...
20
    }
21
22
    public function getMaxStarPlayers():int
23
    {
24
        return $this->rule['inducements']['star_players']['max'];
25
    }
26
27
    public function getInducements():array
28
    {
29
        $inducements = [];
30
        $rule_key = $this->getAttachedRule()->getRuleKey();
0 ignored issues
show
Bug introduced by
It seems like getAttachedRule() 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

30
        $rule_key = $this->/** @scrutinizer ignore-call */ getAttachedRule()->getRuleKey();
Loading history...
31
        $available_inducements = $this->rule['inducements'];
32
33
        foreach ($available_inducements as $key => $value) {
34
            if ($key !== 'star_players') {
35
                $inducement = [
36
                    'type' => $this->getInducementType('inducements'),
37
                    'key' => join(CoreTranslation::TRANSLATION_GLUE, [$rule_key, 'inducements', $key]),
38
                    'translation_domain' => $this->getAttachedRule()->getRuleKey(),
39
                    'translation_key' => CoreTranslation::getInducementName($rule_key, $key),
40
                    'max' => $value['max'] ?? 0,
41
                    'value' => $value['cost'],
42
                ];
43
                $inducements[] = new Inducement($inducement);
44
            }
45
        }
46
        return $inducements;
47
    }
48
49
    public function getInducementsByTeamOptions(array $options):array
50
    {
51
        $inducements = [];
52
        $rule_key = $this->getAttachedRule()->getRuleKey();
53
        $available_inducements = $this->rule['inducements'];
54
55
        foreach ($available_inducements as $key => $value) {
56
            if ($key !== 'star_players') {
57
                if ($options[$key]) {
58
                    $inducement = [
59
                        'type' => $this->getInducementType('inducements'),
60
                        'key' => join(CoreTranslation::TRANSLATION_GLUE, [$rule_key, 'inducements', $key]),
61
                        'translation_domain' => $this->getAttachedRule()->getRuleKey(),
62
                        'translation_key' => CoreTranslation::getInducementName($rule_key, $key),
63
                        'max' => $value['max'] ?? 0,
64
                        'value' => ($options[$key] === 'discount') ? $value['discounted_cost'] : $value['cost'],
65
                    ];
66
                    $inducements[] = new Inducement($inducement);
67
                }
68
            }
69
        }
70
        return $inducements;
71
    }
72
73
    public function getStarPlayers():array
74
    {
75
        return $this->rule['star_players'];
76
    }
77
78
    public function getStarPlayer(string $key):array
79
    {
80
        if (!isset($this->rule['star_players'][$key])) {
81
            throw new NotFoundRuleKeyExcepion($key);
82
        }
83
        return $this->rule['star_players'][$key];
84
    }
85
}
86