1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2020. |
4
|
|
|
* @author Paweł Antosiak <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace Gorynych\Operation; |
10
|
|
|
|
11
|
|
|
use Gorynych\Resource\AbstractResource; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Webmozart\Assert\Assert; |
15
|
|
|
|
16
|
|
|
abstract class AbstractOperation implements ResourceOperationInterface |
17
|
|
|
{ |
18
|
|
|
use ControllerTrait; |
19
|
|
|
|
20
|
|
|
public const GET_METHOD = 'GET'; |
21
|
|
|
public const POST_METHOD = 'POST'; |
22
|
|
|
public const PUT_METHOD = 'PUT'; |
23
|
|
|
public const PATCH_METHOD = 'PATCH'; |
24
|
|
|
public const DELETE_METHOD = 'DELETE'; |
25
|
|
|
|
26
|
|
|
/** @var AbstractResource */ |
27
|
|
|
protected $resource; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function setResource(AbstractResource $resource): self |
33
|
|
|
{ |
34
|
|
|
$this->resource = $resource; |
35
|
|
|
|
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function getResponseStatus(): int |
43
|
|
|
{ |
44
|
|
|
return Response::HTTP_OK; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
1 |
|
public function handle(Request $request) |
51
|
|
|
{ |
52
|
1 |
|
Assert::isCallable($this); |
53
|
|
|
|
54
|
1 |
|
$invoke = new \ReflectionMethod($this, '__invoke'); |
55
|
1 |
|
$argumentType = empty($invoke->getParameters()) ? |
56
|
1 |
|
Request::class : current($invoke->getParameters())->getType()->getName(); |
57
|
|
|
|
58
|
1 |
|
if ($this->serializer->canDeserialize($argumentType)) { |
59
|
1 |
|
$input = $this->deserializeBody( |
60
|
1 |
|
$request, |
61
|
|
|
$argumentType, |
62
|
1 |
|
$this->getDeserializationContext()['definition'], |
63
|
1 |
|
$this->getDeserializationContext()['context'], |
64
|
1 |
|
$request->getContentType() |
|
|
|
|
65
|
|
|
); |
66
|
|
|
|
67
|
1 |
|
$this->validate($input, (new \ReflectionClass($input))->getShortName()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var callable|AbstractOperation $this */ |
71
|
1 |
|
$output = $this($input ?? $request); |
72
|
|
|
|
73
|
1 |
|
return $this->serializer->canNormalize($output) ? |
74
|
1 |
|
$this->normalizeResource( |
75
|
1 |
|
$output, |
76
|
1 |
|
$this->getNormalizationContext()['definition'], |
77
|
1 |
|
$this->getNormalizationContext()['context'] |
78
|
|
|
) : |
79
|
1 |
|
$output; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return array{definition: ?string, context: array} |
84
|
|
|
*/ |
85
|
1 |
|
protected function getDeserializationContext(): array |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
1 |
|
'definition' => null, |
89
|
|
|
'context' => [], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array{definition: ?string, context: array} |
95
|
|
|
*/ |
96
|
1 |
|
protected function getNormalizationContext(): array |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
1 |
|
'definition' => null, |
100
|
|
|
'context' => [], |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|