EknotifierCommon.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace ElkenGit\EknotifierPHP;
  3. use console\helpers\Logger;
  4. use GuzzleHttp\Client;
  5. use GuzzleHttp\Psr7\Response;
  6. use Psr\Http\Message\ResponseInterface;
  7. /**
  8. * Class EKNotifierCommon provide an easy to use integration with the EKNotifier service.
  9. */
  10. class EknotifierCommon
  11. {
  12. /**
  13. * Application credentials.
  14. *
  15. * @var string|null
  16. */
  17. public $applicationCode, $applicationPassword, $notifierUrl;
  18. /**
  19. * Guzzle client.
  20. *
  21. * @var Client
  22. */
  23. private $httpClient;
  24. /**
  25. * Instantiate EknotifierCommon Class.
  26. *
  27. * @param string|null $url
  28. * @param string|null $applicationCode
  29. * @param string|null $applicationPassword
  30. * @throws \Exception
  31. */
  32. public function __construct(string $url = null, string $applicationCode = null, string $applicationPassword = null)
  33. {
  34. $this->httpClient = new Client();
  35. if (function_exists('config')) {
  36. $this->notifierUrl = config('eknotifier.url');
  37. $this->applicationCode = config('eknotifier.app_code');
  38. $this->applicationPassword = config('eknotifier.app_password');
  39. } elseif (!$url || !$applicationCode || !$applicationPassword) {
  40. throw new \Exception('Url, Application Code, or Application Password is missing');
  41. } else {
  42. $this->notifierUrl = $url;
  43. $this->applicationCode = $applicationCode;
  44. $this->applicationPassword = $applicationPassword;
  45. }
  46. }
  47. /**
  48. * Get the EKNotifier JWT Token.
  49. *
  50. * @return string|null
  51. * @throws \GuzzleHttp\Exception\GuzzleException
  52. * @throws \Exception
  53. */
  54. public function getAuthToken(): ?string
  55. {
  56. if (!cache('eknotifier_token')) {
  57. $this->login();
  58. }
  59. return cache('eknotifier_token');
  60. }
  61. /**
  62. * Login to the EKNotifier Service and cache the key.
  63. *
  64. * @return bool
  65. * @throws \GuzzleHttp\Exception\GuzzleException
  66. * @throws \Exception
  67. */
  68. public function login(): bool
  69. {
  70. if (cache('eknotifier_token')) {
  71. return true;
  72. }
  73. $response = $this->postAPI('application/login', [
  74. 'code' => $this->applicationCode,
  75. 'password' => $this->applicationPassword
  76. ]);
  77. if ($response->getStatusCode() === 200) {
  78. $result = json_decode(trim($response->getBody()), true);
  79. cache(['eknotifier_token' => $result['access_token']], 1800);
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. * Call EKNotifier POST API.
  86. *
  87. * @param string $path
  88. * @param array $request
  89. * @param string|null $token
  90. * @return ResponseInterface
  91. * @throws \GuzzleHttp\Exception\GuzzleException
  92. */
  93. protected function postAPI(string $path, array $request, string $token = null): ResponseInterface
  94. {
  95. $url = $this->notifierUrl . '/api/v1/' . $path;
  96. $params = ['json' => $request];
  97. if ($token) {
  98. $params['headers'] = ['Authorization' => 'Bearer ' . $token];
  99. }
  100. try {
  101. return $this->httpClient->request('POST', $url, $params);
  102. } catch (\Exception $exception) {
  103. if ($exception instanceof \Exception) {
  104. Logger::trace('EKNOTIFIER ERROR' . $exception->getResponse()->getBody()->getContents());
  105. return new Response(
  106. $exception->getResponse()->getStatusCode(),
  107. $exception->getResponse()->getHeaders(),
  108. $exception->getResponse()->getBody()->getContents(),
  109. $exception->getResponse()->getProtocolVersion(),
  110. $exception->getResponse()->getReasonPhrase()
  111. );
  112. }
  113. Logger::trace('EKNOTIFIER ERROR' . $exception->getMessage());
  114. return new Response(500, [], $exception->getMessage());
  115. }
  116. }
  117. }