AchievementCriteriaProgress   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 72
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B getNewValue() 0 22 7
1
<?php
2
3
namespace Zurbaev\Achievements;
4
5
class AchievementCriteriaProgress
6
{
7
    /**
8
     * @var int
9
     */
10
    public $value = 0;
11
12
    /**
13
     * @var bool
14
     */
15
    public $changed = false;
16
17
    /**
18
     * @var bool
19
     */
20
    public $completed = false;
21
22
    /**
23
     * @var array
24
     */
25
    public $data = [];
26
27
    /**
28
     * AchievementCriteriaProgress constructor.
29
     *
30
     * @param int   $value
31
     * @param bool  $changed
32
     * @param bool  $completed
33
     * @param array $data      = []
34
     */
35
    public function __construct(int $value, bool $changed = false, bool $completed = false, array $data = [])
36
    {
37
        $this->value = $value;
38
        $this->changed = $changed;
39
        $this->completed = $completed;
40
        $this->data = $data;
41
    }
42
43
    /**
44
     * Calculates new progress value.
45
     *
46
     * @param int    $maxValue
47
     * @param int    $changeValue
48
     * @param string $progressType
49
     *
50
     * @throws \InvalidArgumentException
51
     *
52
     * @return int
53
     */
54
    public function getNewValue(int $maxValue, int $changeValue, string $progressType)
55
    {
56
        switch ($progressType) {
57
            case AchievementCriteriaChange::PROGRESS_SET:
58
                $newValue = $changeValue;
59
                break;
60
            case AchievementCriteriaChange::PROGRESS_ACCUMULATE:
61
                $newValue = $this->value + $changeValue;
62
63
                if ($maxValue > 0) {
64
                    $newValue = $maxValue - $this->value > $changeValue ? $this->value + $changeValue : $maxValue;
65
                }
66
                break;
67
            case AchievementCriteriaChange::PROGRESS_HIGHEST:
68
                $newValue = $this->value < $changeValue ? $changeValue : $this->value;
69
                break;
70
            default:
71
                throw new \InvalidArgumentException('Given progress type is invalid ('.$progressType.').');
72
        }
73
74
        return $newValue;
75
    }
76
}
77