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

StatementsController::head()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 19
rs 9.9
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\PluginBundle\XApi\Lrs;
6
7
use Symfony\Component\HttpFoundation\Response;
8
use Xabbuh\XApi\Model\Statement;
9
use Xabbuh\XApi\Serializer\Symfony\ActorSerializer;
10
use Xabbuh\XApi\Serializer\Symfony\Serializer;
11
use Xabbuh\XApi\Serializer\Symfony\SerializerFactory;
12
use XApi\LrsBundle\Controller\StatementGetController;
13
use XApi\LrsBundle\Controller\StatementHeadController;
14
use XApi\LrsBundle\Controller\StatementPostController;
15
use XApi\LrsBundle\Controller\StatementPutController;
16
use XApi\LrsBundle\Model\StatementsFilterFactory;
17
use XApi\Repository\Doctrine\Mapping\Statement as StatementEntity;
18
use XApi\Repository\Doctrine\Repository\StatementRepository;
19
use XApiPlugin;
20
21
/**
22
 * Class StatementsController.
23
 *
24
 * @package Chamilo\PluginBundle\XApi\Lrs
25
 */
26
class StatementsController extends BaseController
27
{
28
    public function get(): Response
29
    {
30
        $pluginEm = XApiPlugin::getEntityManager();
31
32
        $serializer = Serializer::createSerializer();
33
        $factory = new SerializerFactory($serializer);
34
35
        $getStatementController = new StatementGetController(
36
            new StatementRepository(
37
                $pluginEm->getRepository(StatementEntity::class)
38
            ),
39
            $factory->createStatementSerializer(),
40
            $factory->createStatementResultSerializer(),
41
            new StatementsFilterFactory(
42
                new ActorSerializer($serializer)
43
            )
44
        );
45
46
        return $getStatementController->getStatement($this->httpRequest);
47
    }
48
49
    public function head(): Response
50
    {
51
        $pluginEm = XApiPlugin::getEntityManager();
52
53
        $serializer = Serializer::createSerializer();
54
        $factory = new SerializerFactory($serializer);
55
56
        $headStatementController = new StatementHeadController(
57
            new StatementRepository(
58
                $pluginEm->getRepository(StatementEntity::class)
59
            ),
60
            $factory->createStatementSerializer(),
61
            $factory->createStatementResultSerializer(),
62
            new StatementsFilterFactory(
63
                new ActorSerializer($serializer)
64
            )
65
        );
66
67
        return $headStatementController->getStatement($this->httpRequest);
68
    }
69
70
    /**
71
     * @return \Symfony\Component\HttpFoundation\Response
72
     */
73
    public function put()
74
    {
75
        $pluginEm = XApiPlugin::getEntityManager();
76
77
        $putStatementController = new StatementPutController(
78
            new StatementRepository(
79
                $pluginEm->getRepository(StatementEntity::class)
80
            )
81
        );
82
83
        $statement = $this->deserializeStatement(
84
            $this->httpRequest->getContent()
85
        );
86
87
        return $putStatementController->putStatement($this->httpRequest, $statement);
88
    }
89
90
    public function post(): Response
91
    {
92
        $pluginEm = XApiPlugin::getEntityManager();
93
94
        $postStatementController = new StatementPostController(
95
            new StatementRepository(
96
                $pluginEm->getRepository(StatementEntity::class)
97
            )
98
        );
99
100
        $content = $this->httpRequest->getContent();
101
102
        if (substr($content, 0, 1) !== '[') {
103
            $content = "[$content]";
104
        }
105
106
        $statements = $this->deserializeStatements($content);
107
108
        return $postStatementController->postStatements($this->httpRequest, $statements);
109
    }
110
111
    private function deserializeStatement(string $content = ''): Statement
112
    {
113
        $factory = new SerializerFactory(Serializer::createSerializer());
114
115
        return $factory->createStatementSerializer()->deserializeStatement($content);
116
    }
117
118
    private function deserializeStatements(string $content = ''): array
119
    {
120
        $factory = new SerializerFactory(Serializer::createSerializer());
121
122
        return $factory->createStatementSerializer()->deserializeStatements($content);
123
    }
124
}
125