CalculateScore::isPass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Egzaminer\Exam;
4
5
class CalculateScore
6
{
7
    /**
8
     * @var array
9
     */
10
    private $exam;
11
12
    /**
13
     * @var array
14
     */
15
    private $comparedAnswers;
16
17
    /**
18
     * @var int
19
     */
20
    private $score = 0;
21
22
    public function __construct(array $exam, array $comparedAnswers)
23
    {
24
        $this->exam = $exam;
25
        $this->comparedAnswers = $comparedAnswers;
26
27
        $this->calculate();
28
    }
29
30
    public function calculate()
31
    {
32
        foreach ($this->comparedAnswers as $value) {
33
            if ($value['correct'] === $value['userAnswer']) {
34
                $this->score++;
35
            }
36
        }
37
    }
38
39
    public function getScore(): int
40
    {
41
        return $this->score;
42
    }
43
44
    public function calculatePercentageScore(): float
45
    {
46
        if ($this->exam['questions'] <= 0) {
47
            return 0.0;
48
        }
49
50
        return round($this->score / $this->exam['questions'] * 100, 2);
51
    }
52
53
    public function isPass(): bool
54
    {
55
        return $this->getScore() >= $this->exam['threshold'];
56
    }
57
58
    public function getScoreInfo(): array
59
    {
60
        return [
61
            'score'       => $this->getScore(),
62
            'percentages' => $this->calculatePercentageScore(),
63
            'isPass'      => $this->isPass(),
64
        ];
65
    }
66
}
67