1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Rest\ServerBundle\Controller\Resource; |
5
|
|
|
|
6
|
|
|
use Innmind\Rest\ServerBundle\{ |
7
|
|
|
Exception\InvalidArgumentException, |
8
|
|
|
Translator\LinkTranslator |
9
|
|
|
}; |
10
|
|
|
use Innmind\Rest\Server\{ |
11
|
|
|
Response\HeaderBuilder\LinkBuilderInterface, |
12
|
|
|
GatewayInterface, |
13
|
|
|
Reference, |
14
|
|
|
Identity, |
15
|
|
|
Link\ParameterInterface, |
16
|
|
|
Link\Parameter |
17
|
|
|
}; |
18
|
|
|
use Innmind\Http\{ |
19
|
|
|
Message\ResponseInterface, |
20
|
|
|
Message\Response, |
21
|
|
|
Message\StatusCode, |
22
|
|
|
Message\ReasonPhrase, |
23
|
|
|
Headers, |
24
|
|
|
Header\Link, |
25
|
|
|
Header\LinkValue, |
26
|
|
|
Header\ParameterInterface as LinkParameterInterface |
27
|
|
|
}; |
28
|
|
|
use Innmind\Filesystem\Stream\StringStream; |
29
|
|
|
use Innmind\Immutable\{ |
30
|
|
|
MapInterface, |
31
|
|
|
Map |
32
|
|
|
}; |
33
|
|
|
use Symfony\Component\HttpFoundation\Request; |
34
|
|
|
|
35
|
|
View Code Duplication |
final class LinkController |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
private $gateways; |
38
|
|
|
private $headerBuilder; |
39
|
|
|
private $translator; |
40
|
|
|
|
41
|
2 |
|
public function __construct( |
42
|
|
|
MapInterface $gateways, |
43
|
|
|
LinkBuilderInterface $headerBuilder, |
44
|
|
|
LinkTranslator $translator |
45
|
|
|
) { |
46
|
|
|
if ( |
47
|
2 |
|
(string) $gateways->keyType() !== 'string' || |
48
|
2 |
|
(string) $gateways->valueType() !== GatewayInterface::class |
49
|
|
|
) { |
50
|
1 |
|
throw new InvalidArgumentException; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
$this->gateways = $gateways; |
54
|
1 |
|
$this->headerBuilder = $headerBuilder; |
55
|
1 |
|
$this->translator = $translator; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
1 |
|
public function defaultAction(Request $request, $identity): ResponseInterface |
59
|
|
|
{ |
60
|
1 |
|
$from = $request->attributes->get('_innmind_resource_definition'); |
61
|
1 |
|
$request = $request->attributes->get('_innmind_request'); |
62
|
1 |
|
$tos = $this->translator->translate($request->headers()->get('Link')); |
63
|
|
|
|
64
|
|
|
$linker = $this |
65
|
1 |
|
->gateways |
66
|
1 |
|
->get((string) $from->gateway()) |
67
|
1 |
|
->resourceLinker(); |
68
|
1 |
|
$linker( |
69
|
1 |
|
$from = new Reference($from, new Identity($identity)), |
70
|
|
|
$tos |
71
|
|
|
); |
72
|
|
|
|
73
|
1 |
|
return new Response( |
74
|
1 |
|
$code = new StatusCode(StatusCode::codes()->get('NO_CONTENT')), |
75
|
1 |
|
new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())), |
76
|
1 |
|
$request->protocolVersion(), |
77
|
1 |
|
new Headers( |
78
|
1 |
|
$this->headerBuilder->build( |
79
|
|
|
$request, |
80
|
|
|
$from, |
81
|
|
|
$tos |
82
|
|
|
) |
83
|
|
|
), |
84
|
1 |
|
new StringStream('') |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.