Jacek Kowalski
2026-03-17 67e4ab9dfca8a5cf647b4204c6534546d029706a
src/UcrmHelper.php
@@ -2,47 +2,55 @@
namespace SIPL\UCRM\wFirma;
use Ubnt\UcrmPluginSdk\Service\UcrmOptionsManager;
class UcrmHelper {
   protected $rootDirectory;
   protected $api = NULL;
   protected $attributes = NULL;
   protected $paymentMethods = NULL;
   protected string $rootDirectory;
   protected ?\Ubnt\UcrmPluginSdk\Service\UcrmApi $api = NULL;
   protected ?UcrmAttributes $attributes = NULL;
   protected ?UcrmPaymentMethods $paymentMethods = NULL;
   protected $config = NULL;
   protected $event = NULL;
   function __construct(?string $rootDirectory = NULL) {
      if ($rootDirectory === NULL) {
         $rootDirectory = __DIR__ . '/..';
         $rootDirectory = dirname(__DIR__);
      }
      $this->rootDirectory = $rootDirectory;
   }
   function getRootDirectory() {
   function getRootDirectory(): string {
      return $this->rootDirectory;
   }
   function getApi() {
   function getApi(): \Ubnt\UcrmPluginSdk\Service\UcrmApi {
      if ($this->api === NULL) {
         $this->api = \Ubnt\UcrmPluginSdk\Service\UcrmApi::create($this->rootDirectory);
      }
      return $this->api;
   }
   function getAttributes() {
   function getSelfUrl(): string {
      $ucrmOptionsManager = new UcrmOptionsManager($this->rootDirectory);
      $options = $ucrmOptionsManager->loadOptions();
      return $options->ucrmLocalUrl ?? $options->ucrmPublicUrl ?? '';
   }
   function getAttributes(): UcrmAttributes {
      if ($this->attributes === NULL) {
         $this->attributes = new UcrmAttributes($this);
      }
      return $this->attributes;
   }
   function getPaymentMethods() {
   function getPaymentMethods(): UcrmPaymentMethods {
      if ($this->paymentMethods === NULL) {
         $this->paymentMethods = new UcrmPaymentMethods($this);
      }
      return $this->paymentMethods;
   }
   function getConfig() {
   function getConfig(): array {
      if ($this->config === NULL) {
         $configManager = \Ubnt\UcrmPluginSdk\Service\PluginConfigManager::create($this->rootDirectory);
         $this->config = $configManager->loadConfig();
@@ -50,7 +58,12 @@
      return $this->config;
   }
   function getCurrentEvent() {
   function getVersion(): string {
      $response = $this->getApi()->get('/version');
      return $response['version'];
   }
   function getCurrentEvent(): array {
      if ($this->event === NULL) {
         try {
            if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
@@ -74,17 +87,17 @@
               throw new \RuntimeException('Failed to process event - event not found');
            }
            if (!is_int($event['entityId'])) {
            if (!ctype_digit($event['entityId'])) {
               throw new \RuntimeException('Failed to process event - invalid entity ID');
            }
            $this->event = $data;
         } catch (\Exception $e) {
            $this->event = $event;
         } catch (\Throwable $e) {
            $this->event = $e;
         }
      }
      if ($this->event instanceof \Exception) {
      if ($this->event instanceof \Throwable) {
         throw $this->event;
      }