|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file has been created by developers from BitBag. |
|
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
|
6
|
|
|
* another great project. |
|
7
|
|
|
* You can find more information about us on https://bitbag.io and write us |
|
8
|
|
|
* an email on [email protected]. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace BitBag\SyliusVueStorefrontPlugin\Processor; |
|
14
|
|
|
|
|
15
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Command\CommandInterface; |
|
16
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Query\QueryInterface; |
|
17
|
|
|
use BitBag\SyliusVueStorefrontPlugin\Request\RequestInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
19
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
20
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
|
21
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class RequestProcessor implements RequestProcessorInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var ValidatorInterface */ |
|
26
|
|
|
private $validator; |
|
27
|
|
|
|
|
28
|
|
|
/** @var SerializerInterface */ |
|
29
|
|
|
private $serializer; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $className; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(ValidatorInterface $validator, SerializerInterface $serializer, string $className) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->validator = $validator; |
|
37
|
|
|
$this->serializer = $serializer; |
|
38
|
|
|
$this->className = $className; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function validate(Request $httpRequest): ConstraintViolationListInterface |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->validator->validate($this->transformHttpRequest($httpRequest)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getCommand(Request $httpRequest): CommandInterface |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->transformHttpRequest($httpRequest)->getCommand(); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getQuery(Request $httpRequest): QueryInterface |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->transformHttpRequest($httpRequest)->getQuery(); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function transformHttpRequest(Request $httpRequest): RequestInterface |
|
57
|
|
|
{ |
|
58
|
|
|
$requestBody = []; |
|
59
|
|
|
|
|
60
|
|
|
if (!empty($httpRequest->getContent())) { |
|
61
|
|
|
$requestBody = $this->serializer->decode($httpRequest->getContent(), self::FORMAT_JSON); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->serializer->denormalize( |
|
65
|
|
|
\array_merge($requestBody, $httpRequest->attributes->all(), $httpRequest->query->all()), |
|
66
|
|
|
$this->className |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: