ConstraintValidator.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Payment\Gateway\Validator;
  17. use Magento\Payment\Gateway\Validator\AbstractValidator;
  18. use Magento\Payment\Gateway\Validator\ResultInterface;
  19. use Amazon\Payment\Gateway\Http\Client\Client;
  20. use Amazon\Payment\Domain\AmazonConstraint;
  21. class ConstraintValidator extends AbstractValidator
  22. {
  23. /**
  24. * Performs validation of result code
  25. *
  26. * @param array $validationSubject
  27. * @return ResultInterface
  28. */
  29. public function validate(array $validationSubject)
  30. {
  31. $response = $validationSubject['response'];
  32. if (isset($response['constraints']) && $response['constraints']) {
  33. $constraint = $response['constraints'][0];
  34. return $this->createResult(
  35. false,
  36. [$this->getConstraint($constraint)]
  37. );
  38. }
  39. // if no constraints found, continue to other validators for more specific errors
  40. return $this->createResult(
  41. true,
  42. ['status' => isset($response['status']) ? $response['status'] : __('No constraints detected.')]
  43. );
  44. }
  45. /**
  46. * @param AmazonConstraint $constraint
  47. * @return string
  48. */
  49. private function getConstraint(AmazonConstraint $constraint)
  50. {
  51. return $constraint->getId();
  52. }
  53. }