Test.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Apiconnector;
  3. /**
  4. * test class for validation of the api creds.
  5. */
  6. class Test
  7. {
  8. /**
  9. * @var \Dotdigitalgroup\Email\Helper\Data
  10. */
  11. private $helper;
  12. /**
  13. * @var \Magento\Framework\App\Config\ReinitableConfigInterface
  14. */
  15. private $config;
  16. /**
  17. * Test constructor.
  18. *
  19. * @param \Dotdigitalgroup\Email\Helper\Data $data
  20. * @param \Magento\Framework\App\Config\ReinitableConfigInterface $config
  21. */
  22. public function __construct(
  23. \Dotdigitalgroup\Email\Helper\Data $data,
  24. \Magento\Framework\App\Config\ReinitableConfigInterface $config
  25. ) {
  26. $this->helper = $data;
  27. $this->config = $config;
  28. }
  29. /**
  30. * Validate apiuser on save.
  31. *
  32. * @param string $apiUsername
  33. * @param string $apiPassword
  34. *
  35. * @return bool|mixed
  36. */
  37. public function validate($apiUsername, $apiPassword)
  38. {
  39. //Clear config cache
  40. $this->config->reinit();
  41. $website = $this->helper->getWebsiteForSelectedScopeInAdmin();
  42. if (!$this->helper->isEnabled($website)) {
  43. return false;
  44. }
  45. if ($apiPassword == '******') {
  46. $apiPassword = $this->helper->getApiPassword($website);
  47. }
  48. $client = $this->helper->clientFactory->create();
  49. if ($apiUsername && $apiPassword) {
  50. $client->setApiUsername($apiUsername)
  51. ->setApiPassword($apiPassword);
  52. $accountInfo = $client->getAccountInfo();
  53. if (isset($accountInfo->message)) {
  54. $this->helper->log('VALIDATION ERROR : ' . $accountInfo->message);
  55. return false;
  56. }
  57. // If api endpoint then force save
  58. if ($apiEndpoint = $this->getApiEndPoint($accountInfo)) {
  59. $this->helper->saveApiEndpoint($apiEndpoint, $website->getId());
  60. }
  61. return $accountInfo;
  62. }
  63. return false;
  64. }
  65. /**
  66. * Get api endpoint
  67. *
  68. * @param Object|null $accountInfo
  69. * @return string
  70. */
  71. private function getApiEndPoint($accountInfo)
  72. {
  73. if (is_object($accountInfo)) {
  74. //save endpoint for account
  75. foreach ($accountInfo->properties as $property) {
  76. if ($property->name == 'ApiEndpoint' && !empty($property->value)) {
  77. return $property->value;
  78. }
  79. }
  80. }
  81. }
  82. }