Passed
Push — 1.11.x ( 106594...56cdc0 )
by Angel Fernando Quiroz
08:52
created

ActivitiesStateController::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 27
nc 3
nop 0
dl 0
loc 47
rs 9.488
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\PluginBundle\XApi\Lrs;
5
6
use Chamilo\PluginBundle\Entity\XApi\ActivityState;
7
use Database;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Response;
10
use Xabbuh\XApi\Model\Actor;
11
use Xabbuh\XApi\Serializer\Symfony\Serializer;
12
13
/**
14
 * Class ActivitiesStateController.
15
 *
16
 * @package Chamilo\PluginBundle\XApi\Lrs
17
 */
18
class ActivitiesStateController extends BaseController
19
{
20
    public function get(): Response
21
    {
22
        $serializer = Serializer::createSerializer();
23
24
        $requestedAgent = $this->httpRequest->query->get('agent');
25
        $activityId = $this->httpRequest->query->get('activityId');
26
        $stateId = $this->httpRequest->query->get('stateId');
27
28
        $state = Database::select(
29
            '*',
30
            Database::get_main_table('xapi_activity_state'),
31
            [
32
                'where' => [
33
                    'state_id = ? AND activity_id = ? AND MD5(agent) = ?' => [
34
                        Database::escape_string($stateId),
35
                        Database::escape_string($activityId),
36
                        md5($requestedAgent),
37
                    ],
38
                ],
39
            ],
40
            'first'
41
        );
42
43
        if (empty($state)) {
44
            return JsonResponse::create([], Response::HTTP_NOT_FOUND);
45
        }
46
47
        $requestedAgent = $serializer->deserialize(
48
            $this->httpRequest->query->get('agent'),
49
            Actor::class,
50
            'json'
51
        );
52
53
        /** @var Actor $stateAgent */
54
        $stateAgent = $serializer->deserialize(
55
            $state['agent'],
56
            Actor::class,
57
            'json'
58
        );
59
60
        if (!$stateAgent->equals($requestedAgent)) {
61
            return JsonResponse::create([], Response::HTTP_NOT_FOUND);
62
        }
63
64
        $documentData = json_decode($state['document_data'], true);
65
66
        return JsonResponse::create($documentData);
67
    }
68
69
    public function head(): Response
70
    {
71
        return $this->get()->setContent('');
72
    }
73
74
    public function post(): Response
75
    {
76
        return $this->put();
77
    }
78
79
    public function put(): Response
80
    {
81
        $activityId = $this->httpRequest->query->get('activityId');
82
        $agent = $this->httpRequest->query->get('agent');
83
        $stateId = $this->httpRequest->query->get('stateId');
84
        $documentData = $this->httpRequest->getContent();
85
86
        $state = Database::select(
87
            'id',
88
            Database::get_main_table('xapi_activity_state'),
89
            [
90
                'where' => [
91
                    'state_id = ? AND activity_id = ? AND MD5(agent) = ?' => [
92
                        Database::escape_string($stateId),
93
                        Database::escape_string($activityId),
94
                        md5($agent),
95
                    ],
96
                ],
97
            ],
98
            'first'
99
        );
100
101
        $em = Database::getManager();
102
103
        if (empty($state)) {
104
            $state = new ActivityState();
105
            $state
106
                ->setActivityId($activityId)
107
                ->setAgent(json_decode($agent, true))
108
                ->setStateId($stateId);
109
        } else {
110
            $state = $em->find(ActivityState::class, $state['id']);
111
        }
112
113
        $state->setDocumentData(json_decode($documentData, true));
114
115
        $em->persist($state);
116
        $em->flush();
117
118
        return Response::create('', Response::HTTP_NO_CONTENT);
119
    }
120
}
121