setka-8.x-1.0/src/SetkaEditorApi.php

src/SetkaEditorApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
 
namespace Drupal\setka_editor;
 
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Uri;
 
/**
 * Setka Editor API service.
 */
class SetkaEditorApi {
 
  use StringTranslationTrait;
  use LoggerChannelTrait;
 
  // @todo Move it to configuration, like "stage/live mode".
  const SETKA_API_CURRENT_BUILD_URL = 'https://editor.setka.io/api/v1/custom/builds/current';
  const SETKA_API_PUSH_SYSTEM_INFO = 'https://editor.setka.io/api/v1/drupal/current_theme.json';
 
  /**
   * Http client.
   *
   * @var \GuzzleHttp\Client
   */
  protected $httpClient;
 
  /**
   * Setka Editor license key.
   *
   * @var string
   */
  protected $licenseKey;
 
  /**
   * Drupal messenger interface.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;
 
  /**
   * Drupal logger channel interface.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;
 
  /**
   * {@inheritdoc}
   */
  public function __construct(Client $http_client, MessengerInterface $messenger, ConfigFactory $configFactory) {
    $config = $configFactory->get('setka_editor.settings');
    $this->messenger = $messenger;
    $this->logger = $this->getLogger('setka_editor');
    $this->licenseKey = $config->get('setka_license_key');
    $this->httpClient = $http_client;
  }
 
  /**
   * Returns current setka editor build data.
   *
   * @param string $licenseKey
   *   License key.
   *
   * @return bool|mixed
   *   Current setka editor build data or FALSE on error.
   */
  public function getCurrentBuild($licenseKey = NULL) {
    $queryLicenseKey = $licenseKey ?? $this->licenseKey;
    try {
      $uri = new Uri(self::SETKA_API_CURRENT_BUILD_URL);
      $uri = Uri::withQueryValue($uri, 'token', $queryLicenseKey);
      $response = $this->httpClient->request('GET', $uri);
      if ($response->getStatusCode() == 200) {
        $responseContent = $response->getBody()->getContents();
        if (!empty($responseContent)) {
          return Json::decode($responseContent);
        }
      }
    }
    catch (RequestException $e) {
      $this->logger->error('Setka Editor API error on Get Current Build: @error', ['@error' => $e->getMessage()]);
      if ($e->getCode() == 401) {
        $this->messenger->addError($this->t('Invalid license key.'));
      }
      else {
        $response = $e->getResponse();
        $this->messenger->addError($response->getStatusCode() . ': ' . $response->getReasonPhrase());
      }
    }
    return FALSE;
  }
 
  /**
   * Pushes information about plugin, site and CMS to Style Manager.
   *
   * @param string $licenseKey
   *   Setka Editor license key.
   */
  public function pushSystemInfo($licenseKey = NULL) {
    global $base_url;
    $queryLicenseKey = $licenseKey ?? $this->licenseKey;
    $moduleInfo = system_get_info('module', 'setka_editor');
    $appVersion = \Drupal::VERSION;
    $pluginVersion = $moduleInfo['version'];
    $pluginVersion = empty($pluginVersion) ? '8.x-1.0' : $pluginVersion;
    $domain = $base_url;
    try {
      $uri = new Uri(self::SETKA_API_PUSH_SYSTEM_INFO);
      $uri = Uri::withQueryValue($uri, 'token', $queryLicenseKey);
      $uri = Uri::withQueryValue($uri, 'app_version', $appVersion);
      $uri = Uri::withQueryValue($uri, 'plugin_version', $pluginVersion);
      $uri = Uri::withQueryValue($uri, 'domain', $domain);
      $this->httpClient->request('POST', $uri);
    }
    catch (RequestException $e) {
      $this->logger->error('Setka Editor API error on System Info Push: @error', ['@error' => $e->getMessage()]);
    }
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc