| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace ElkenGit\EknotifierPHP;
- use console\helpers\Logger;
- use GuzzleHttp\Client;
- use GuzzleHttp\Psr7\Response;
- use Psr\Http\Message\ResponseInterface;
- /**
- * Class EKNotifierCommon provide an easy to use integration with the EKNotifier service.
- */
- class EknotifierCommon
- {
- /**
- * Application credentials.
- *
- * @var string|null
- */
- public $applicationCode, $applicationPassword, $notifierUrl;
- /**
- * Guzzle client.
- *
- * @var Client
- */
- private $httpClient;
- /**
- * Instantiate EknotifierCommon Class.
- *
- * @param string|null $url
- * @param string|null $applicationCode
- * @param string|null $applicationPassword
- * @throws \Exception
- */
- public function __construct(string $url = null, string $applicationCode = null, string $applicationPassword = null)
- {
- $this->httpClient = new Client();
- if (function_exists('config')) {
- $this->notifierUrl = config('eknotifier.url');
- $this->applicationCode = config('eknotifier.app_code');
- $this->applicationPassword = config('eknotifier.app_password');
- } elseif (!$url || !$applicationCode || !$applicationPassword) {
- throw new \Exception('Url, Application Code, or Application Password is missing');
- } else {
- $this->notifierUrl = $url;
- $this->applicationCode = $applicationCode;
- $this->applicationPassword = $applicationPassword;
- }
- }
- /**
- * Get the EKNotifier JWT Token.
- *
- * @return string|null
- * @throws \GuzzleHttp\Exception\GuzzleException
- * @throws \Exception
- */
- public function getAuthToken(): ?string
- {
- if (!cache('eknotifier_token')) {
- $this->login();
- }
- return cache('eknotifier_token');
- }
- /**
- * Login to the EKNotifier Service and cache the key.
- *
- * @return bool
- * @throws \GuzzleHttp\Exception\GuzzleException
- * @throws \Exception
- */
- public function login(): bool
- {
- if (cache('eknotifier_token')) {
- return true;
- }
- $response = $this->postAPI('application/login', [
- 'code' => $this->applicationCode,
- 'password' => $this->applicationPassword
- ]);
- if ($response->getStatusCode() === 200) {
- $result = json_decode(trim($response->getBody()), true);
- cache(['eknotifier_token' => $result['access_token']], 1800);
- return true;
- }
- return false;
- }
- /**
- * Call EKNotifier POST API.
- *
- * @param string $path
- * @param array $request
- * @param string|null $token
- * @return ResponseInterface
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- protected function postAPI(string $path, array $request, string $token = null): ResponseInterface
- {
- $url = $this->notifierUrl . '/api/v1/' . $path;
- $params = ['json' => $request];
- if ($token) {
- $params['headers'] = ['Authorization' => 'Bearer ' . $token];
- }
- try {
- return $this->httpClient->request('POST', $url, $params);
- } catch (\Exception $exception) {
- if ($exception instanceof \Exception) {
- Logger::trace('EKNOTIFIER ERROR' . $exception->getResponse()->getBody()->getContents());
- return new Response(
- $exception->getResponse()->getStatusCode(),
- $exception->getResponse()->getHeaders(),
- $exception->getResponse()->getBody()->getContents(),
- $exception->getResponse()->getProtocolVersion(),
- $exception->getResponse()->getReasonPhrase()
- );
- }
- Logger::trace('EKNOTIFIER ERROR' . $exception->getMessage());
- return new Response(500, [], $exception->getMessage());
- }
- }
- }
|