Passed
Push — master ( 95b92f...37aede )
by Yannick
10:09 queued 01:17
created

ActivitiesProfileController::put()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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