AdminLoginObserver.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Observer;
  6. use Magento\Framework\Event\Observer;
  7. use Magento\Framework\Event\ObserverInterface;
  8. use Magento\Framework\Exception\InputException;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Message\ManagerInterface;
  11. use Temando\Shipping\Model\Shipping\Carrier;
  12. use Temando\Shipping\Rest\AuthenticationInterface;
  13. use Temando\Shipping\Webservice\Config\WsConfigInterface;
  14. /**
  15. * Temando Login Observer
  16. *
  17. * @package Temando\Shipping\Observer
  18. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  19. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. * @link https://www.temando.com/
  21. */
  22. class AdminLoginObserver implements ObserverInterface
  23. {
  24. /**
  25. * @var ManagerInterface
  26. */
  27. private $messageManager;
  28. /**
  29. * @var Carrier
  30. */
  31. private $carrier;
  32. /**
  33. * @var WsConfigInterface
  34. */
  35. private $config;
  36. /**
  37. * @var AuthenticationInterface
  38. */
  39. private $auth;
  40. /**
  41. * AdminLoginObserver constructor.
  42. *
  43. * @param ManagerInterface $messageManager
  44. * @param Carrier $carrier
  45. * @param WsConfigInterface $config
  46. * @param AuthenticationInterface $auth
  47. */
  48. public function __construct(
  49. ManagerInterface $messageManager,
  50. Carrier $carrier,
  51. WsConfigInterface $config,
  52. AuthenticationInterface $auth
  53. ) {
  54. $this->messageManager = $messageManager;
  55. $this->carrier = $carrier;
  56. $this->config = $config;
  57. $this->auth = $auth;
  58. }
  59. /**
  60. * @param Observer $observer
  61. * @return void
  62. */
  63. public function execute(Observer $observer)
  64. {
  65. if (!$this->carrier->getConfigFlag('active')) {
  66. return;
  67. }
  68. $bearerToken = $this->config->getBearerToken();
  69. $accountId = $this->config->getAccountId();
  70. try {
  71. $this->auth->connect($accountId, $bearerToken);
  72. } catch (InputException $e) {
  73. // credentials missing
  74. $msg = 'Temando Shipping is not properly configured. Please register an account.';
  75. $this->messageManager->addWarningMessage(__($msg));
  76. } catch (LocalizedException $e) {
  77. // other
  78. $this->messageManager->addExceptionMessage($e, $e->getMessage());
  79. }
  80. }
  81. }