Completed
Pull Request — dev (#24)
by
unknown
03:54
created

SurveyController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 93.94%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 8
dl 0
loc 102
ccs 31
cts 33
cp 0.9394
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apiSurveysAction() 0 21 3
B apiSurveyAction() 0 29 5
C apiSurveyUpdateAction() 0 31 7
1
<?php
2
3
namespace AppBundle\Controller\Api;
4
5
use AppBundle\Entity\SurveyAnswer;
6
use AppBundle\Entity\Survey;
7
use AppBundle\Entity\SurveyQuestion;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\HttpFoundation\Request;
13
14
class SurveyController extends Controller
15
{
16
    /**
17
     * @Route("/surveys", name="api_surveys")
18
     * @Method("GET")
19
     */
20 2
    public function apiSurveysAction()
21
    {
22 2
        $user = $this->getUser();
23 2
        if (!$user) {
24
            return $this->json(['message' => 'User is not authorized'], 401);
25
        }
26 2
        $em = $this->getDoctrine()->getManager();
27 2
        $surveys = $em->getRepository(Survey::class)->findSurveyByUser($user);
28 2
        if (!$surveys) {
29
            return $this->json(['message' => 'No surveys'], 404);
30
        }
31 2
        $serializer = $this->get('serializer');
32
33 2
        $json = $serializer->normalize(
34
            $surveys,
35 2
            null,
36 2
            array('groups' => array('group1'))
37
        );
38
39 2
        return $this->json($json);
40
    }
41
42
    /**
43
     * @param Survey $survey
44
     * @Route("/survey/{id}", name="api_survey_byid")
45
     * @Method("GET")
46
     * @ParamConverter("survey", class="AppBundle:Survey")
47
     */
48
    public function apiSurveyAction(Survey $survey)
49
    {
50
        $user = $this->getUser();
51
        if (!$user) {
52
            return $this->json(['message' => 'User is not authorized'], 401);
53
        }
54
55
        if (!$survey) {
56
            return $this->json(['message' => 'No surveys'], 404);
57
        }
58
        $serializer = $this->get('serializer');
59
        $json = null;
60
        if ($survey->getStatus() == 'submited') {
61
            $json = $serializer->normalize(
62
                $survey,
63
                null,
64
                array('groups' => array('group1', 'group2'))
65
            );
66
        }
67
        if ($survey->getStatus() == 'current') {
68
            $json = $serializer->normalize(
69
                $survey,
70
                null,
71
                array('groups' => array('group1', 'group3'))
72
            );
73
        }
74
75
        return $this->json($json);
76
    }
77
78
    /**
79
     * @param Request $request, Survey $survey
0 ignored issues
show
Documentation introduced by
There is no parameter named $request,. Did you maybe mean $request?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
80
     * @Route("/survey/update/{id}", name="api_survey_update")
81
     * @Method("POST")
82
     * @ParamConverter("survey", class="AppBundle:Survey")
83
     */
84 1
    public function apiSurveyUpdateAction(Request $request, Survey $survey)
85
    {
86 1
        $user = $this->getUser();
87 1
        if (!$user) {
88
            return $this->json(['message' => 'User is not authorized'], 401);
89
        }
90 1
        if (!$survey || $survey->getStatus() == 'submited') {
91
            return $this->json(['message' => 'No survey'], 404);
92
        }
93 1
        $questKey = array();
94 1
        foreach ($survey->getType()->getQuestions() as $question) {
95 1
            $questKey[] = $question->getId();
96
        }
97 1
        $data = json_decode($request->getContent(), true);
98 1
        $em = $this->getDoctrine()->getManager();
99 1
        foreach ($questKey as $key) {
100 1
            $answer = $data[$key];
101 1
            if ($answer == null) {
102
                return $this->json(['message' => 'Wrong question id'], 400);
103
            }
104 1
            $newAnswer = new SurveyAnswer();
105 1
            $newAnswer->setSurvey($survey);
106 1
            $newAnswer->setQuestion($em->getRepository(SurveyQuestion::class)->find($key));
0 ignored issues
show
Documentation introduced by
$em->getRepository(\AppB...ion::class)->find($key) is of type object|null, but the function expects a object<AppBundle\Entity\SurveyQuestion>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107 1
            $newAnswer->setContent($answer);
108 1
            $em->persist($newAnswer);
109
        }
110 1
        $survey->setStatus('submited');
111 1
        $em->flush();
112
113 1
        return $this->json(['message' => 'Survey updated'], 200);
114
    }
115
}
116