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