getWebhookEntryValidator(); if (!$validator->validate($data)) { throw new \InvalidArgumentException($validator->getErrorMessage()); } $auth = isset($data[Webhook::AUTH]) ? $this->createAuthEntryFromArray($data[Webhook::AUTH]) : null; $responseValidation = true; // default value for validation for back compatibility, because old webhooks are nod updated and may not contain this new parameter if (array_key_exists(Webhook::RESPONSE_VALIDATION, $data)) { $responseValidation = $data[Webhook::RESPONSE_VALIDATION]; } return new Webhook( $data[Webhook::ID], $data[Webhook::URL], $data[Webhook::HTTP_METHOD], $data[Webhook::RETRY_MAX], $auth, $responseValidation, $data[Webhook::EXTRA_PAYLOAD] ?? [] ); } /** * @param array $data * @return WebhookAuth */ protected function createAuthEntryFromArray(array $data) { $validator = $this->getWebhookAuthValidator(); if (!$validator->validate($data)) { throw new \InvalidArgumentException( 'Invalid ' . Webhook::AUTH . ' config: ' . $validator->getErrorMessage() ); } return new WebhookAuth( $data[WebhookAuth::AUTH_CLASS], $data[WebhookAuth::CREDENTIALS] ); } /** * @return ArrayValidator */ private function getWebhookEntryValidator() { return (new ArrayValidator()) ->assertString([Webhook::ID, Webhook::URL, Webhook::HTTP_METHOD]) ->assertInt([Webhook::RETRY_MAX]) ->assertArray(Webhook::AUTH, false, true); } /** * @return ArrayValidator */ private function getWebhookAuthValidator() { return (new ArrayValidator()) ->assertString(WebhookAuth::AUTH_CLASS) ->assertArray(WebhookAuth::CREDENTIALS); } }