|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the xAPI package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Christian Flothmann <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace XApi\LrsBundle\Controller; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Exception\ConflictHttpException; |
|
21
|
|
|
use Xabbuh\XApi\Common\Exception\NotFoundException; |
|
22
|
|
|
use Xabbuh\XApi\Model\Statement; |
|
23
|
|
|
use Xabbuh\XApi\Model\StatementId; |
|
24
|
|
|
use XApi\Repository\Api\StatementRepositoryInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @author Christian Flothmann <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
final class StatementPutController |
|
30
|
|
|
{ |
|
31
|
|
|
private $repository; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(StatementRepositoryInterface $repository) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->repository = $repository; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function putStatement(Request $request, Statement $statement): Response |
|
39
|
|
|
{ |
|
40
|
|
|
if (null === $statementId = $request->query->get('statementId')) { |
|
41
|
|
|
throw new BadRequestHttpException('Required statementId parameter is missing.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
try { |
|
45
|
|
|
$id = StatementId::fromString($statementId); |
|
46
|
|
|
} catch (InvalidArgumentException $e) { |
|
47
|
|
|
throw new BadRequestHttpException(sprintf('Parameter statementId ("%s") is not a valid UUID.', $statementId), $e); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (null !== $statement->getId() && !$id->equals($statement->getId())) { |
|
51
|
|
|
throw new ConflictHttpException(sprintf('Id parameter ("%s") and statement id ("%s") do not match.', $id->getValue(), $statement->getId()->getValue())); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
|
|
$existingStatement = $this->repository->findStatementById($id); |
|
56
|
|
|
|
|
57
|
|
|
if (!$existingStatement->equals($statement)) { |
|
58
|
|
|
throw new ConflictHttpException('The new statement is not equal to an existing statement with the same id.'); |
|
59
|
|
|
} |
|
60
|
|
|
} catch (NotFoundException $e) { |
|
61
|
|
|
$statement = $statement->withId($id); |
|
62
|
|
|
|
|
63
|
|
|
$this->repository->storeStatement($statement, true); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return new Response('', 204); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|