getNormalizeUserPost()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Egzaminer\Exam;
4
5
class CompareUserAnswersWithQuestions
6
{
7
    /**
8
     * @var array
9
     */
10
    private $post;
11
12
    /**
13
     * @var array
14
     */
15
    private $questions;
16
17
    public function __construct(array $post, array $questions)
18
    {
19
        $this->post = $post;
20
        $this->questions = $questions;
21
    }
22
23
    public function getNormalizeUserPost(): array
24
    {
25
        unset($this->post['send']);
26
        if (empty($this->post)) {
27
            return [];
28
        }
29
30
        $post = [];
31
32
        foreach ($this->post as $key => $value) {
33
            $post[str_replace('question_', '', $key)] = $value;
34
        }
35
36
        return $post;
37
    }
38
39
    public function getCompared(): array
40
    {
41
        $post = $this->getNormalizeUserPost();
42
43
        foreach ($this->questions as $key => $value) {
44
            $userAnswer = null;
45
46
            if (isset($post[$value['id']])) {
47
                $userAnswer = $post[$value['id']];
48
            }
49
50
            $this->questions[$key]['userAnswer'] = $userAnswer;
51
        }
52
53
        return $this->questions;
54
    }
55
}
56