CheckAnwerV0::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: device
5
 * Date: 04.03.16
6
 * Time: 15:50
7
 */
8
9
namespace AppBundle\Services;
10
11
12
use Symfony\Bridge\Doctrine\RegistryInterface;
13
14
class CheckAnwerV0
15
{
16
    private $doctrine;
17
18
    public function __construct(RegistryInterface $registryInterface)
19
    {
20
        $this->doctrine = $registryInterface;
21
    }
22
23
    public function checkAnswers(array $data)
24
    {
25
        $em = $this->doctrine->getManager();
26
        $question = $em->getRepository('AppBundle:Question')
27
            ->find($data['idQuestion']);
28
29
        $originalAnswers = $question->getAnswers();
30
        $countOriginalAnswers = count($originalAnswers);
31
32
        $sumTrueCorrect = 0;
33
        $sumAllCorrect = 0;
34
        $countAllTrueAnswers = 0;
35
        $countFalseAnswers = 0;
36
        $countAllFalseAnswers = 0;
37
38
        foreach ($originalAnswers as $item) {
39
            $item->getCorrectly() ? $countAllTrueAnswers++ : $countAllFalseAnswers++;
40
            if ($item->getCorrectly() === $data["answer_{$item->getId()}"]) {
41
                if ($item->getCorrectly() === true) {
42
                    $sumTrueCorrect++;
43
                }
44
                $sumAllCorrect++;
45
            } else {
46
                if ($item->getCorrectly() === false) {
47
                    $countFalseAnswers++;
48
                }
49
            }
50
        }
51
        //first type question
52 View Code Duplication
        if ($question->getAllIncorrect() || $data['answer_all_incorrect']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            if ($question->getAllIncorrect() === $data['answer_all_incorrect'] && $question->getAllIncorrect() === true && $sumTrueCorrect == 0) {
54
55
                return 1;
56
            }
57
58
            return 0;
59
        }
60
61
        // second type question
62
        if ($countAllTrueAnswers == 1) {
63
            if ($countFalseAnswers == 1) {
64
                $result = $sumTrueCorrect != 0 ? 1 / $sumTrueCorrect / 2 : 0;
65
            } elseif ($countFalseAnswers > 1) {
66
                $result = 0;
67
            }
68
            else {
69
                $result = $sumTrueCorrect != 0 ? 1 / $sumTrueCorrect : 0;
70
            }
71
            //third type question
72
        } else {
73
            if ($countAllTrueAnswers > 1) {
74
                $result = $sumAllCorrect != 0 ? ((1 / $countOriginalAnswers) * $sumAllCorrect) : 0;
75
            } else {
76
                $result =  0;
77
            }
78
        }
79
80
        return $result;
81
    }
82
}
83