Calculator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 6 2
A calculate() 0 3 1
A getMaxPoints() 0 10 3
1
<?php
2
3
namespace SilverStripe\ModuleRatings;
4
5
/**
6
 * The Calculator class takes the number of points given for a module and converts it into a relative score out
7
 * of 100
8
 */
9
class Calculator
10
{
11
    /**
12
     * @var int
13
     */
14
    protected $maxPoints = 0;
15
16
    /**
17
     * @var Calculator
18
     */
19
    protected static $instance;
20
21
    /**
22
     * Get a singleton instance of this class
23
     *
24
     * @return Calculator
25
     */
26
    public static function getInstance()
27
    {
28
        if (self::$instance == null) {
29
            self::$instance = new static();
30
        }
31
        return self::$instance;
32
    }
33
34
    public function calculate($totalPoints)
35
    {
36
        return ceil(($totalPoints / $this->getMaxPoints()) * 100);
37
    }
38
39
    /**
40
     * Calculate the maximum number of check points a module could possibly receive by loading a check suite,
41
     * mocking a successful result and getting the points from every check
42
     *
43
     * @return int
44
     */
45
    public function getMaxPoints()
46
    {
47
        if (!$this->maxPoints) {
48
            $suite = new CheckSuite();
49
            foreach ($suite->getChecks() as $check) {
50
                $check->setSuccessful(true);
51
                $this->maxPoints += $check->getPoints();
52
            }
53
        }
54
        return $this->maxPoints;
55
    }
56
}
57