CompareUserAnswersWithQuestions   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getNormalizeUserPost() 0 15 3
A getCompared() 0 16 3
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