-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathWebhookValidator.php
More file actions
90 lines (75 loc) · 2.46 KB
/
WebhookValidator.php
File metadata and controls
90 lines (75 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/*
* This file has been created by developers from BitBag.
* Feel free to contact us once you face any issues or want to start
* You can find more information about us on https://bitbag.io and write us
* an email on hello@bitbag.io.
*/
declare(strict_types=1);
namespace BitBag\SyliusMailChimpPlugin\Validator;
use BitBag\SyliusMailChimpPlugin\Model\WebhookData;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
final class WebhookValidator
{
/** @var ValidatorInterface */
private $validator;
/** @var string */
private $listId;
/** @var string */
private $webhookSecret;
public function __construct(
ValidatorInterface $validator,
string $listId,
string $webhookSecret,
) {
$this->validator = $validator;
$this->listId = $listId;
$this->webhookSecret = $webhookSecret;
}
public function validate(WebhookData $webhookData): array
{
$data = $webhookData->getData();
$violations = $this->validator->validate($data, [
new Collection([
'allowExtraFields' => true,
'allowMissingFields' => false,
'fields' => [
'list_id' => [
new NotBlank(),
new Type(['type' => 'string']),
],
'id' => [
new NotBlank(),
new Type(['type' => 'string']),
],
'email' => [
new NotBlank(),
new Type(['type' => 'string']),
],
],
]),
]);
$errors = [];
if (0 === count($violations)) {
return $errors;
}
/** @var ConstraintViolation $violation */
foreach ($violations as $violation) {
$errors[] = $violation->getMessage();
}
return $errors;
}
public function isListIdValid(?string $listId): bool
{
return $listId === $this->listId;
}
public function isRequestValid(Request $request): bool
{
return $request->query->get('qsecret') === $this->webhookSecret;
}
}