1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Rest\ServerBundle\Controller\Resource; |
5
|
|
|
|
6
|
|
|
use Innmind\Rest\ServerBundle\Format; |
7
|
|
|
use Innmind\Rest\Server\{ |
8
|
|
|
Gateway, |
9
|
|
|
Identity\Identity, |
10
|
|
|
HttpResource\HttpResource, |
11
|
|
|
Definition\Access, |
12
|
|
|
Response\HeaderBuilder\UpdateBuilder |
13
|
|
|
}; |
14
|
|
|
use Innmind\Http\{ |
15
|
|
|
Message\Response, |
16
|
|
|
Message\StatusCode\StatusCode, |
17
|
|
|
Message\ReasonPhrase\ReasonPhrase, |
18
|
|
|
Headers\Headers |
19
|
|
|
}; |
20
|
|
|
use Innmind\Filesystem\Stream\StringStream; |
21
|
|
|
use Innmind\Immutable\{ |
22
|
|
|
MapInterface, |
23
|
|
|
Set |
24
|
|
|
}; |
25
|
|
|
use Symfony\Component\{ |
26
|
|
|
Serializer\SerializerInterface, |
27
|
|
|
HttpFoundation\Request |
28
|
|
|
}; |
29
|
|
|
|
30
|
|
|
final class UpdateController |
31
|
|
|
{ |
32
|
|
|
private $gateways; |
33
|
|
|
private $serializer; |
34
|
|
|
private $format; |
35
|
|
|
private $buildHeader; |
36
|
|
|
|
37
|
4 |
|
public function __construct( |
38
|
|
|
MapInterface $gateways, |
39
|
|
|
SerializerInterface $serializer, |
40
|
|
|
Format $format, |
41
|
|
|
UpdateBuilder $headerBuilder |
42
|
|
|
) { |
43
|
|
|
if ( |
44
|
4 |
|
(string) $gateways->keyType() !== 'string' || |
45
|
4 |
|
(string) $gateways->valueType() !== Gateway::class |
46
|
|
|
) { |
47
|
2 |
|
throw new \TypeError(sprintf( |
48
|
2 |
|
'Argument 1 must be of type MapInterface<string, %s>', |
49
|
2 |
|
Gateway::class |
50
|
|
|
)); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
$this->gateways = $gateways; |
54
|
2 |
|
$this->serializer = $serializer; |
55
|
2 |
|
$this->format = $format; |
56
|
2 |
|
$this->buildHeader = $headerBuilder; |
57
|
2 |
|
} |
58
|
|
|
|
59
|
2 |
|
public function defaultAction(Request $request, $identity): Response |
60
|
|
|
{ |
61
|
2 |
|
$definition = $request->attributes->get('_innmind_resource_definition'); |
62
|
2 |
|
$request = $request->attributes->get('_innmind_request'); |
63
|
|
|
|
64
|
|
|
$updater = $this |
65
|
2 |
|
->gateways |
66
|
2 |
|
->get((string) $definition->gateway()) |
67
|
2 |
|
->resourceUpdater(); |
68
|
2 |
|
$updater( |
69
|
2 |
|
$definition, |
70
|
2 |
|
$identity = new Identity($identity), |
71
|
2 |
|
$resource = $this->serializer->deserialize( |
72
|
2 |
|
$request, |
73
|
2 |
|
HttpResource::class, |
74
|
2 |
|
'request_'.$this->format->contentType($request)->name(), |
75
|
|
|
[ |
76
|
2 |
|
'definition' => $definition, |
77
|
2 |
|
'mask' => new Access(Access::UPDATE), |
78
|
|
|
] |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
|
82
|
2 |
|
return new Response\Response( |
83
|
2 |
|
$code = new StatusCode(StatusCode::codes()->get('NO_CONTENT')), |
84
|
2 |
|
new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())), |
85
|
2 |
|
$request->protocolVersion(), |
86
|
2 |
|
new Headers( |
87
|
2 |
|
($this->buildHeader)($request, $definition, $identity, $resource) |
88
|
|
|
), |
89
|
2 |
|
new StringStream('') |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|