This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
/
DoctrineController.php
executable file
·116 lines (92 loc) · 3.04 KB
/
DoctrineController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace Liip\VieBundle\Controller;
use Symfony\Component\HttpFoundation\Request,
Symfony\Component\HttpFoundation\Response,
Symfony\Component\Routing\Exception\ResourceNotFoundException,
Symfony\Component\Security\Core\Exception\AccessDeniedException,
Symfony\Component\Security\Core\SecurityContextInterface,
Symfony\Component\Validator\ValidatorInterface;
use FOS\RestBundle\View\ViewHandlerInterface,
FOS\RestBundle\View\View,
FOS\Rest\Util\Codes;
use Doctrine\Common\Persistence\ManagerRegistry;
use DMS\Filter\FilterInterface;
use Liip\VieBundle\FromJsonLdInterface,
Liip\VieBundle\ToJsonLdInterface;
abstract class DoctrineController
{
/**
* @var SecurityContext
*/
protected $securityContext;
/**
* @var ViewHandlerInterface
*/
protected $viewHandler;
/**
* @var FilterInterface
*/
protected $filter;
/**
* @var ValidatorInterface
*/
protected $validator;
/**
* @var ManagerRegistry
*/
protected $registry;
/**
* @var string
*/
protected $name;
public function __construct(ViewHandlerInterface $viewHandler, ValidatorInterface $validator, ManagerRegistry $registry, SecurityContextInterface $securityContext = null, FilterInterface $filter = null, $name = null)
{
$this->securityContext = $securityContext;
$this->viewHandler = $viewHandler;
$this->filter = $filter;
$this->validator = $validator;
$this->registry = $registry;
$this->name = $name;
}
/**
* @return Doctrine\Common\Persistence\ObjectManager
*/
protected function getManager()
{
return $this->registry->getManager($this->name);
}
/**
* Handle article PUT
*/
public function putDocumentAction(Request $request, $id)
{
if ($this->securityContext && false === $this->securityContext->isGranted("IS_AUTHENTICATED_ANONYMOUSLY")) {
throw new AccessDeniedException();
}
$data = $request->request->all();
$model = $this->getManager()->find(null, $id);
if (empty($model)) {
throw new ResourceNotFoundException($id.' not found');
}
if (!($model instanceof FromJsonLdInterface)) {
throw new \LogicException('Document does not support FromJsonLdInterface.');
}
$model->fromJsonLD($data);
if ($this->filter) {
$this->filter->filter($model);
}
$errors = $this->validator->validate($model);
if (count($errors)) {
// TODO ensure we are returning JSON-LD
$view = View::create($errors, 400)->setFormat('json');
} else {
$this->getManager()->flush();
if (!($model instanceof ToJsonLdInterface)) {
return new Response('', Codes::HTTP_NO_CONTENT);
}
// return the updated version
$view = View::create($model->toJsonLd())->setFormat('json');
}
return $this->viewHandler->handle($view, $request);
}
}