| @@ 27-83 (lines=57) @@ | ||
| 24 | }; |
|
| 25 | use Symfony\Component\HttpFoundation\Request; |
|
| 26 | ||
| 27 | final class LinkController |
|
| 28 | { |
|
| 29 | private $gateways; |
|
| 30 | private $buildHeader; |
|
| 31 | private $translator; |
|
| 32 | ||
| 33 | public function __construct( |
|
| 34 | MapInterface $gateways, |
|
| 35 | LinkBuilder $headerBuilder, |
|
| 36 | LinkTranslator $translator |
|
| 37 | ) { |
|
| 38 | if ( |
|
| 39 | (string) $gateways->keyType() !== 'string' || |
|
| 40 | (string) $gateways->valueType() !== Gateway::class |
|
| 41 | ) { |
|
| 42 | throw new \TypeError(sprintf( |
|
| 43 | 'Argument 1 must be of type MapInterface<string, %s>', |
|
| 44 | Gateway::class |
|
| 45 | )); |
|
| 46 | } |
|
| 47 | ||
| 48 | $this->gateways = $gateways; |
|
| 49 | $this->buildHeader = $headerBuilder; |
|
| 50 | $this->translator = $translator; |
|
| 51 | } |
|
| 52 | ||
| 53 | public function defaultAction(Request $request, $identity): Response |
|
| 54 | { |
|
| 55 | $from = $request->attributes->get('_innmind_resource_definition'); |
|
| 56 | $request = $request->attributes->get('_innmind_request'); |
|
| 57 | ||
| 58 | if (!$request->headers()->has('Link')) { |
|
| 59 | throw new BadRequest; |
|
| 60 | } |
|
| 61 | ||
| 62 | $tos = $this->translator->translate($request->headers()->get('Link')); |
|
| 63 | ||
| 64 | $linker = $this |
|
| 65 | ->gateways |
|
| 66 | ->get((string) $from->gateway()) |
|
| 67 | ->resourceLinker(); |
|
| 68 | $linker( |
|
| 69 | $from = new Reference($from, new Identity($identity)), |
|
| 70 | $tos |
|
| 71 | ); |
|
| 72 | ||
| 73 | return new Response\Response( |
|
| 74 | $code = new StatusCode(StatusCode::codes()->get('NO_CONTENT')), |
|
| 75 | new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())), |
|
| 76 | $request->protocolVersion(), |
|
| 77 | new Headers( |
|
| 78 | ($this->buildHeader)($request, $from, $tos) |
|
| 79 | ), |
|
| 80 | new StringStream('') |
|
| 81 | ); |
|
| 82 | } |
|
| 83 | } |
|
| 84 | ||
| @@ 27-87 (lines=61) @@ | ||
| 24 | }; |
|
| 25 | use Symfony\Component\HttpFoundation\Request; |
|
| 26 | ||
| 27 | final class UnlinkController |
|
| 28 | { |
|
| 29 | private $gateways; |
|
| 30 | private $buildHeader; |
|
| 31 | private $translator; |
|
| 32 | ||
| 33 | public function __construct( |
|
| 34 | MapInterface $gateways, |
|
| 35 | UnlinkBuilder $headerBuilder, |
|
| 36 | LinkTranslator $translator |
|
| 37 | ) { |
|
| 38 | if ( |
|
| 39 | (string) $gateways->keyType() !== 'string' || |
|
| 40 | (string) $gateways->valueType() !== Gateway::class |
|
| 41 | ) { |
|
| 42 | throw new \TypeError(sprintf( |
|
| 43 | 'Argument 1 must be of type MapInterface<string, %s>', |
|
| 44 | Gateway::class |
|
| 45 | )); |
|
| 46 | } |
|
| 47 | ||
| 48 | $this->gateways = $gateways; |
|
| 49 | $this->buildHeader = $headerBuilder; |
|
| 50 | $this->translator = $translator; |
|
| 51 | } |
|
| 52 | ||
| 53 | public function defaultAction(Request $request, $identity): Response |
|
| 54 | { |
|
| 55 | $from = $request->attributes->get('_innmind_resource_definition'); |
|
| 56 | $request = $request->attributes->get('_innmind_request'); |
|
| 57 | ||
| 58 | if (!$request->headers()->has('Link')) { |
|
| 59 | throw new BadRequest; |
|
| 60 | } |
|
| 61 | ||
| 62 | $tos = $this->translator->translate($request->headers()->get('Link')); |
|
| 63 | ||
| 64 | $unlinker = $this |
|
| 65 | ->gateways |
|
| 66 | ->get((string) $from->gateway()) |
|
| 67 | ->resourceUnlinker(); |
|
| 68 | $unlinker( |
|
| 69 | $from = new Reference($from, new Identity($identity)), |
|
| 70 | $tos |
|
| 71 | ); |
|
| 72 | ||
| 73 | return new Response\Response( |
|
| 74 | $code = new StatusCode(StatusCode::codes()->get('NO_CONTENT')), |
|
| 75 | new ReasonPhrase(ReasonPhrase::defaults()->get($code->value())), |
|
| 76 | $request->protocolVersion(), |
|
| 77 | new Headers( |
|
| 78 | ($this->buildHeader)( |
|
| 79 | $request, |
|
| 80 | $from, |
|
| 81 | $tos |
|
| 82 | ) |
|
| 83 | ), |
|
| 84 | new StringStream('') |
|
| 85 | ); |
|
| 86 | } |
|
| 87 | } |
|
| 88 | ||