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

ActivitiesProfileController::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
nc 2
nop 0
dl 0
loc 22
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\XApi\Lrs;
6
7
use Chamilo\PluginBundle\Entity\XApi\ActivityProfile;
8
use Symfony\Component\HttpFoundation\Response;
9
10
/**
11
 * Class ActivitiesProfileController.
12
 *
13
 * @package Chamilo\PluginBundle\XApi\Lrs
14
 */
15
class ActivitiesProfileController extends BaseController
16
{
17
    public function get(): Response
18
    {
19
        $profileId = $this->httpRequest->query->get('profileId');
20
        $activityId = $this->httpRequest->query->get('activityId');
21
22
        $em = \Database::getManager();
23
        $profileRepo = $em->getRepository(ActivityProfile::class);
24
25
        /** @var ActivityProfile $activityProfile */
26
        $activityProfile = $profileRepo->findOneBy(
27
            [
28
                'profileId' => $profileId,
29
                'activityId' => $activityId,
30
            ]
31
        );
32
33
        if (empty($activityProfile)) {
34
            return Response::create(null, Response::HTTP_NO_CONTENT);
35
        }
36
37
        return Response::create(
38
            json_encode($activityProfile->getDocumentData())
39
        );
40
    }
41
42
    public function head(): Response
43
    {
44
        return $this->get()->setContent('');
45
    }
46
47
    public function put(): Response
48
    {
49
        $profileId = $this->httpRequest->query->get('profileId');
50
        $activityId = $this->httpRequest->query->get('activityId');
51
        $documentData = $this->httpRequest->getContent();
52
53
        $em = \Database::getManager();
54
        $profileRepo = $em->getRepository(ActivityProfile::class);
55
56
        /** @var ActivityProfile $activityProfile */
57
        $activityProfile = $profileRepo->findOneBy(
58
            [
59
                'profileId' => $profileId,
60
                'activityId' => $activityId,
61
            ]
62
        );
63
64
        if (empty($activityProfile)) {
65
            $activityProfile = new ActivityProfile();
66
            $activityProfile
67
                ->setProfileId($profileId)
68
                ->setActivityId($activityId);
69
        }
70
71
        $activityProfile->setDocumentData(json_decode($documentData, true));
72
73
        $em->persist($activityProfile);
74
        $em->flush();
75
76
        return Response::create(null, Response::HTTP_NO_CONTENT);
77
    }
78
}
79