CredentialsValidator.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\Config\Backend\Active;
  6. use Zend\Validator\Callback as CallbackValidator;
  7. use Zend\Validator\Uri as UriValidator;
  8. /**
  9. * Validator functions for merchant account credentials.
  10. *
  11. * @package Temando\Shipping\Model
  12. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  13. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  14. * @link https://www.temando.com/
  15. */
  16. class CredentialsValidator
  17. {
  18. /**
  19. * @var ApiConnection
  20. */
  21. private $connection;
  22. /**
  23. * CredentialsValidator constructor.
  24. *
  25. * @param ApiConnection $connection
  26. */
  27. public function __construct(ApiConnection $connection)
  28. {
  29. $this->connection = $connection;
  30. }
  31. /**
  32. * Check if credentials are available in config.
  33. *
  34. * @return CallbackValidator
  35. */
  36. public function getInputValidator()
  37. {
  38. $callback = function (\Magento\Framework\App\Config\Value $field) {
  39. $enabled = $field->getValue();
  40. // read account id from current save operation
  41. $accountId = $field->getFieldsetDataValue('account_id');
  42. // read bearer token from current save operation
  43. $bearerToken = $field->getFieldsetDataValue('bearer_token');
  44. if (!$enabled && !$accountId && !$bearerToken) {
  45. // it's ok to leave credentials empty as long as shipping method is disabled.
  46. return true;
  47. }
  48. if ($enabled && (!$accountId || !$bearerToken)) {
  49. // once shipping method is enabled, credentials must be given.
  50. return false;
  51. }
  52. return true;
  53. };
  54. $message = __('Please set API credentials before enabling Magento Shipping.');
  55. $validator = new CallbackValidator($callback);
  56. $validator->setMessage($message, CallbackValidator::INVALID_VALUE);
  57. return $validator;
  58. }
  59. /**
  60. * Check if API endpoint URI scheme is either "http" or "https" if given.
  61. *
  62. * @return CallbackValidator
  63. */
  64. public function getUriEndpointValidator()
  65. {
  66. $callback = function (\Magento\Framework\App\Config\Value $field, UriValidator $uriValidator) {
  67. // read session endpoint from current save operation
  68. $sessionUrl = $field->getFieldsetDataValue('session_endpoint');
  69. return (empty($sessionUrl) || $uriValidator->isValid($sessionUrl));
  70. };
  71. $uriValidator = new UriValidator(['uriHandler' => \Zend\Uri\Http::class]);
  72. $message = __('Please enter a valid URL. Protocol (http://, https://) is required.');
  73. $validator = new CallbackValidator($callback);
  74. $validator->setCallbackOptions([$uriValidator]);
  75. $validator->setMessage($message, CallbackValidator::INVALID_VALUE);
  76. return $validator;
  77. }
  78. /**
  79. * Check if credentials are valid.
  80. *
  81. * @return CallbackValidator
  82. */
  83. public function getAuthenticationValidator()
  84. {
  85. $callback = function (\Magento\Framework\App\Config\Value $field) {
  86. $enabled = $field->getValue();
  87. // read session endpoint from current save operation
  88. $sessionUrl = $field->getFieldsetDataValue('session_endpoint');
  89. // read account id from current save operation
  90. $accountId = $field->getFieldsetDataValue('account_id');
  91. // read bearer token from current save operation
  92. $bearerToken = $field->getFieldsetDataValue('bearer_token');
  93. if (!$enabled && !$accountId && !$bearerToken) {
  94. // it's ok to leave credentials empty as long as shipping method is disabled.
  95. return true;
  96. }
  97. try {
  98. return $this->connection->test($sessionUrl, $accountId, $bearerToken);
  99. } catch (\Exception $e) {
  100. return false;
  101. }
  102. };
  103. $message = __('Magento Shipping authentication failed. Please check your credentials.');
  104. $validator = new CallbackValidator($callback);
  105. $validator->setMessage($message, CallbackValidator::INVALID_VALUE);
  106. return $validator;
  107. }
  108. }