1 | <?php |
||
32 | class WebhookController extends Controller |
||
33 | { |
||
34 | /** |
||
35 | * @param Request $request |
||
36 | * |
||
37 | * @return Response |
||
38 | */ |
||
39 | public function createAction(Request $request): Response |
||
40 | { |
||
41 | $data = json_decode((string)$request->getContent(), true); |
||
42 | |||
43 | if (empty($data) || json_last_error() !== JSON_ERROR_NONE) { |
||
44 | return new JsonResponse(['error' => 'Malformed json provided.'], 400); |
||
45 | } |
||
46 | |||
47 | if (true !== $response = $this->validate($data)) { |
||
48 | return $response; |
||
49 | } |
||
50 | |||
51 | $message = $this->buildMessage($data); |
||
52 | |||
53 | $this->get('webhook.repository')->save($message); |
||
54 | $this->get('amqp.producer')->publish($message); |
||
55 | |||
56 | return new JsonResponse($message, Response::HTTP_CREATED); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param array $data |
||
61 | * |
||
62 | * @return bool|JsonResponse |
||
63 | */ |
||
64 | private function validate(array $data) |
||
65 | { |
||
66 | $validator = Validation::createValidator(); |
||
67 | $violations = $validator->validate($data, $this->getConstraints()); |
||
68 | |||
69 | if (0 !== $violations->count()) { |
||
70 | $errors = []; |
||
71 | foreach ($violations as $violation) { |
||
72 | $field = preg_replace('/\[|\]/', '', $violation->getPropertyPath()); |
||
73 | $error = $violation->getMessage(); |
||
74 | $errors[$field] = $error; |
||
75 | } |
||
76 | |||
77 | return new JsonResponse(['errors' => $errors], 400); |
||
78 | } |
||
79 | |||
80 | return true; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return Collection |
||
85 | */ |
||
86 | private function getConstraints(): Collection |
||
87 | { |
||
88 | $strategiesMap = StrategyRegistry::getMap(); |
||
89 | $strategies = array_keys($strategiesMap); |
||
90 | |||
91 | return new Collection([ |
||
92 | 'fields' => [ |
||
93 | 'body' => new Required(), |
||
94 | 'url' => new Url(), |
||
95 | 'strategy' => new Optional( |
||
96 | new Collection( |
||
97 | [ |
||
98 | 'fields' => [ |
||
99 | 'name' => new Required(new Choice(['choices' => $strategies, 'strict' => true])), |
||
100 | 'options' => new Optional(new All(new Length(['min' => 1]))), |
||
101 | ] |
||
102 | ]) |
||
103 | ), |
||
104 | 'raw' => new Optional(new Choice(['choices' => [true, false], 'strict' => true])), |
||
105 | 'maxAttempts' => new Optional(new Range(['min' => 1, 'max' => 100])), |
||
106 | 'expectedCode' => new Optional(new Range(['min' => 200, 'max' => 515])), |
||
107 | 'expectedContent' => new Optional(new Length(['min' => 1, 'max' => 128])), |
||
108 | 'userAgent' => new Optional(new Length(['min' => 1, 'max' => 128])), |
||
109 | 'metadata' => new Optional(new All(new Length(['min' => 1, 'max' => 128]))), |
||
110 | 'callbackUrl' => new Optional(new Url()), |
||
111 | 'reference' => new Optional(new Length(['min' => 1, 'max' => 255])), |
||
112 | ] |
||
113 | ]); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param $id |
||
118 | * |
||
119 | * @return JsonResponse |
||
120 | */ |
||
121 | public function getAction($id): JsonResponse |
||
131 | |||
132 | /** |
||
133 | * @param $id |
||
134 | * |
||
135 | * @return JsonResponse |
||
136 | */ |
||
137 | public function getByReferenceAction($id): JsonResponse |
||
143 | |||
144 | /** |
||
145 | * @return JsonResponse |
||
146 | */ |
||
147 | public function getLastAction(): JsonResponse |
||
154 | |||
155 | /** |
||
156 | * @param $data |
||
157 | * |
||
158 | * @return Webhook |
||
159 | */ |
||
160 | private function buildMessage($data): Webhook |
||
186 | } |