Passed
Pull Request — master (#5629)
by Angel Fernando Quiroz
10:06 queued 01:32
created

StatementsController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 50
dl 0
loc 115
c 1
b 0
f 0
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 12 1
A saveLog() 0 10 3
A put() 0 18 1
A head() 0 12 1
A __construct() 0 11 1
A post() 0 22 2
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\PluginBundle\XApi\Lrs\Util\InternalLogUtil;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Xabbuh\XApi\Common\Exception\NotFoundException;
13
use Xabbuh\XApi\Model\StatementId;
14
use Xabbuh\XApi\Serializer\Symfony\ActorSerializer;
15
use Xabbuh\XApi\Serializer\Symfony\Serializer;
16
use Xabbuh\XApi\Serializer\Symfony\SerializerFactory;
17
use XApi\LrsBundle\Controller\StatementGetController;
18
use XApi\LrsBundle\Controller\StatementHeadController;
19
use XApi\LrsBundle\Controller\StatementPostController;
20
use XApi\LrsBundle\Controller\StatementPutController;
21
use XApi\LrsBundle\Model\StatementsFilterFactory;
22
use XApi\Repository\Doctrine\Mapping\Statement as StatementEntity;
23
use XApi\Repository\Doctrine\Repository\StatementRepository;
24
use XApiPlugin;
25
26
/**
27
 * Class StatementsController.
28
 */
29
class StatementsController extends BaseController
30
{
31
    /**
32
     * @var StatementRepository
33
     */
34
    private $statementRepository;
35
36
    /**
37
     * @var \Symfony\Component\Serializer\Serializer|\Symfony\Component\Serializer\SerializerInterface
38
     */
39
    private $serializer;
40
41
    /**
42
     * @var SerializerFactory
43
     */
44
    private $serializerFactory;
45
46
    public function __construct(Request $httpRequest)
47
    {
48
        parent::__construct($httpRequest);
49
50
        $pluginEm = XApiPlugin::getEntityManager();
51
52
        $this->statementRepository = new StatementRepository(
53
            $pluginEm->getRepository(StatementEntity::class)
54
        );
55
        $this->serializer = Serializer::createSerializer();
56
        $this->serializerFactory = new SerializerFactory($this->serializer);
57
    }
58
59
    public function get(): Response
60
    {
61
        $getStatementController = new StatementGetController(
62
            $this->statementRepository,
63
            $this->serializerFactory->createStatementSerializer(),
64
            $this->serializerFactory->createStatementResultSerializer(),
65
            new StatementsFilterFactory(
66
                new ActorSerializer($this->serializer)
67
            )
68
        );
69
70
        return $getStatementController->getStatement($this->httpRequest);
71
    }
72
73
    public function head(): Response
74
    {
75
        $headStatementController = new StatementHeadController(
76
            $this->statementRepository,
77
            $this->serializerFactory->createStatementSerializer(),
78
            $this->serializerFactory->createStatementResultSerializer(),
79
            new StatementsFilterFactory(
80
                new ActorSerializer($this->serializer)
81
            )
82
        );
83
84
        return $headStatementController->getStatement($this->httpRequest);
85
    }
86
87
    public function put(): Response
88
    {
89
        $statement = $this->serializerFactory
90
            ->createStatementSerializer()
91
            ->deserializeStatement(
92
                $this->httpRequest->getContent()
93
            )
94
        ;
95
96
        $putStatementController = new StatementPutController($this->statementRepository);
97
98
        $response = $putStatementController->putStatement($this->httpRequest, $statement);
99
100
        $this->saveLog(
101
            [$this->httpRequest->query->get('statementId')]
102
        );
103
104
        return $response;
105
    }
106
107
    public function post(): Response
108
    {
109
        $content = $this->httpRequest->getContent();
110
111
        if ('[' !== substr($content, 0, 1)) {
112
            $content = "[$content]";
113
        }
114
115
        $statements = $this->serializerFactory
116
            ->createStatementSerializer()
117
            ->deserializeStatements($content)
118
        ;
119
120
        $postStatementController = new StatementPostController($this->statementRepository);
121
122
        $response = $postStatementController->postStatements($this->httpRequest, $statements);
123
124
        $this->saveLog(
125
            json_decode($response->getContent(), false)
126
        );
127
128
        return $response;
129
    }
130
131
    /**
132
     * @param array<string> $statementsId
133
     */
134
    private function saveLog(array $statementsId): void
135
    {
136
        foreach ($statementsId as $statementId) {
137
            try {
138
                $storedStatement = $this->statementRepository->findStatementById(
139
                    StatementId::fromString($statementId)
140
                );
141
142
                InternalLogUtil::saveStatementForInternalLog($storedStatement);
143
            } catch (NotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
144
            }
145
        }
146
    }
147
}
148