vendor/pimcore/translations-provider-interfaces/src/Service/ConfigurationService.php line 68

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\TranslationsProviderInterfaceBundle\Service;
  12. use Pimcore\Config\LocationAwareConfigRepository;
  13. use Pimcore\TranslationsProviderInterfaceBundle\TranslationsProviderManager\TranslationsProviderManagerInterface;
  14. class ConfigurationService
  15. {
  16.     const CONFIG_ID 'pimcore_translations_provider_interface';
  17.     const CONFIGURATION_DIRECTORY PIMCORE_CONFIGURATION_DIRECTORY '/translations-provider';
  18.     const DEFAULT_VALUE_AUTO_SUBMIT true;
  19.     const DEFAULT_VALUE_REDELIVERY_THRESHOLD 60;
  20.     const DEFAULT_VALUE_ERROR_THRESHOLD 10;
  21.     const DEFAULT_VALUE_PROJECT_SHORTCODE 'Pimcore';
  22.     const DEFAULT_VALUE_TRANSLATIONS_DEFAULT_FILE_FORMAT 'PimcoreXML';
  23.     const DEFAULT_VALUE_TRANSLATIONS_WSDL_PATH 'https://gl-connect2.translations.com/PD/services/';
  24.     const DEFAULT_VALUE_TRANSLATIONS_USER_AGENT 'pimcore';
  25.     protected ?array $translationsComSettings;
  26.     protected ?array $xplanationSettings;
  27.     protected ?array $deepLSettings;
  28.     protected bool $autoSubmit;
  29.     protected ?string $defaultProvider;
  30.     protected ?string $notificationRecipients;
  31.     protected ?string $allowedLanguages;
  32.     protected int $redeliveryThreshold;
  33.     protected int $errorThreshold;
  34.     protected ?string $projectShortcode;
  35.     protected bool $ignorePermissions false;
  36.     protected bool $isWriteable;
  37.     protected bool $configLoaded false;
  38.     protected ?LocationAwareConfigRepository $locationAwareConfigRepository;
  39.     protected TranslationsProviderManagerInterface $translationsProviderManager;
  40.     public function __construct(array $configTranslationsProviderManagerInterface $translationsProviderManager)
  41.     {
  42.         $this->translationsProviderManager $translationsProviderManager;
  43.         $containerConfig = [];
  44.         if (isset($config['ignore_permissions']) && $config['ignore_permissions'] === true) {
  45.             trigger_deprecation('pimcore/translations-provider-interfaces''2.4''Config `ignore_permissions` is used but deprecated. Make sure you set permissions properly to your users/roles. Will be removed in version 3.0');
  46.             $this->ignorePermissions $config['ignore_permissions'];
  47.         }
  48.         if (!empty($config['settings'])) {
  49.             //check if other keys than default keys exist
  50.             $hasAdditionalKeys false;
  51.             if (!empty(array_diff(array_keys($config['settings']), ['auto_submit''redelivery_threshold''error_threshold''project_shortcode']))) {
  52.                 $hasAdditionalKeys true;
  53.             }
  54.             //check if default keys have default values
  55.             $defaultValuesAreSet false;
  56.             if (
  57.                 $config['settings']['auto_submit'] !== self::DEFAULT_VALUE_AUTO_SUBMIT ||
  58.                 $config['settings']['redelivery_threshold'] !== self::DEFAULT_VALUE_REDELIVERY_THRESHOLD ||
  59.                 $config['settings']['error_threshold'] !== self::DEFAULT_VALUE_ERROR_THRESHOLD ||
  60.                 $config['settings']['project_shortcode'] !== self::DEFAULT_VALUE_PROJECT_SHORTCODE
  61.             ) {
  62.                 $defaultValuesAreSet true;
  63.             }
  64.             if ($hasAdditionalKeys || $defaultValuesAreSet) {
  65.                 $containerConfig[self::CONFIG_ID]['settings'] = $config['settings'];
  66.             }
  67.         }
  68.         if (!empty($config['translations_com'])) {
  69.             $containerConfig[self::CONFIG_ID]['translations_com'] = $config['translations_com'];
  70.         }
  71.         if (!empty($config['xplanation'])) {
  72.             $containerConfig[self::CONFIG_ID]['xplanation'] = $config['xplanation'];
  73.         }
  74.         if (!empty($config['deepl'])) {
  75.             $containerConfig[self::CONFIG_ID]['deepl'] = $config['deepl'];
  76.         }
  77.         $this->locationAwareConfigRepository = new LocationAwareConfigRepository(
  78.             $containerConfig,
  79.             self::CONFIG_ID,
  80.             self::CONFIGURATION_DIRECTORY,
  81.             'PIMCORE_WRITE_TARGET_TRANSLATIONS_PROVIDER'
  82.         );
  83.     }
  84.     protected function loadConfig()
  85.     {
  86.         if ($this->configLoaded === false) {
  87.             list($config$dataSource) = $this->locationAwareConfigRepository->loadConfigByKey(self::CONFIG_ID);
  88.             $this->translationsComSettings $config['translations_com'] ?? null;
  89.             $this->xplanationSettings $config['xplanation'] ?? [];
  90.             $this->deepLSettings $config['deepl'] ?? [];
  91.             $this->autoSubmit $config['settings']['auto_submit'] ?? self::DEFAULT_VALUE_AUTO_SUBMIT;
  92.             $this->defaultProvider $config['settings']['default_provider'] ?? null;
  93.             $this->notificationRecipients $config['settings']['notification_recipients'] ?? null;
  94.             $this->allowedLanguages $config['settings']['allowed_languages'] ?? null;
  95.             $this->redeliveryThreshold $config['settings']['redelivery_threshold'] ?? self::DEFAULT_VALUE_REDELIVERY_THRESHOLD;
  96.             $this->errorThreshold $config['settings']['error_threshold'] ?? self::DEFAULT_VALUE_ERROR_THRESHOLD;
  97.             $this->projectShortcode $config['settings']['project_shortcode'] ?? self::DEFAULT_VALUE_PROJECT_SHORTCODE;
  98.             $this->isWriteable $this->locationAwareConfigRepository->isWriteable(self::CONFIG_ID$dataSource);
  99.             $this->configLoaded true;
  100.         }
  101.     }
  102.     /**
  103.      *
  104.      * @throws \Exception
  105.      */
  106.     public function saveConfiguration(
  107.         array $settings, ?array $translationsComSettings null, ?array $xplanationSettings null, ?array $deeplSettings null
  108.     ) {
  109.         $data = [
  110.             'settings' => [
  111.                 'auto_submit' => $settings['auto_submit'] ?? self::DEFAULT_VALUE_AUTO_SUBMIT,
  112.                 'default_provider' => $settings['default_provider'] ?? null,
  113.                 'notification_recipients' => $settings['notification_recipients'] ?? null,
  114.                 'allowed_languages' => is_array($settings['allowed_languages'] ?? null) ? implode(','$settings['allowed_languages']) : ($settings['allowed_languages'] ?? ''),
  115.                 'redelivery_threshold' => $settings['redelivery_threshold'] ?? self::DEFAULT_VALUE_REDELIVERY_THRESHOLD,
  116.                 'error_threshold' => $settings['error_threshold'] ?? self::DEFAULT_VALUE_ERROR_THRESHOLD,
  117.                 'project_shortcode' => $settings['project_shortcode'] ?? self::DEFAULT_VALUE_PROJECT_SHORTCODE
  118.             ],
  119.             'translations_com' => $translationsComSettings,
  120.             'xplanation' => $xplanationSettings,
  121.             'deepl' => $deeplSettings
  122.         ];
  123.         $this->locationAwareConfigRepository->saveConfig(self::CONFIG_ID$data, function ($key$data) {
  124.             return [
  125.                 $key => $data
  126.             ];
  127.         });
  128.         $this->configLoaded false;
  129.     }
  130.     public function getFullConfiguration(): array
  131.     {
  132.         $this->loadConfig();
  133.         return [
  134.             'settings' => [
  135.                 'auto_submit' => $this->autoSubmit,
  136.                 'default_provider' => $this->defaultProvider,
  137.                 'notification_recipients' => $this->notificationRecipients,
  138.                 'allowed_languages' => $this->allowedLanguages,
  139.                 'redelivery_threshold' => $this->redeliveryThreshold,
  140.                 'error_threshold' => $this->errorThreshold,
  141.                 'project_shortcode' => $this->projectShortcode
  142.             ],
  143.             'translations_com' => $this->translationsComSettings,
  144.             'xplanation' => $this->xplanationSettings,
  145.             'deepl' => $this->deepLSettings
  146.         ];
  147.     }
  148.     public function getProviderTypes(): array
  149.     {
  150.         $classes = [];
  151.         foreach ($this->translationsProviderManager->getTranslationsProviders() as $provider) {
  152.             $classes[] = get_class($provider);
  153.         }
  154.         return $classes;
  155.     }
  156.     public function getAvailableProviders(): array
  157.     {
  158.         $providers = [];
  159.         foreach ($this->translationsProviderManager->getTranslationsProviders() as $id => $provider) {
  160.             $providers[] = [$id$provider->getShortcut()];
  161.         }
  162.         return $providers;
  163.     }
  164.     public function isWriteable(): bool
  165.     {
  166.         $this->loadConfig();
  167.         return $this->isWriteable;
  168.     }
  169.     public function getTranslationsComSettings(): ?array
  170.     {
  171.         $this->loadConfig();
  172.         return $this->translationsComSettings;
  173.     }
  174.     public function getXplanationSettings(): ?array
  175.     {
  176.         $this->loadConfig();
  177.         return $this->xplanationSettings;
  178.     }
  179.     public function getDeeplSettings(): ?array
  180.     {
  181.         $this->loadConfig();
  182.         return $this->deepLSettings;
  183.     }
  184.     public function getAutoSubmit(): bool
  185.     {
  186.         $this->loadConfig();
  187.         return $this->autoSubmit;
  188.     }
  189.     public function getDefaultProvider(): ?string
  190.     {
  191.         $this->loadConfig();
  192.         return $this->defaultProvider;
  193.     }
  194.     public function getNotificationRecipients(): ?string
  195.     {
  196.         $this->loadConfig();
  197.         return $this->notificationRecipients;
  198.     }
  199.     public function getAllowedLanguages(): ?string
  200.     {
  201.         $this->loadConfig();
  202.         return $this->allowedLanguages;
  203.     }
  204.     public function getRedeliveryThreshold(): int
  205.     {
  206.         $this->loadConfig();
  207.         return $this->redeliveryThreshold;
  208.     }
  209.     public function getErrorThreshold(): int
  210.     {
  211.         $this->loadConfig();
  212.         return $this->errorThreshold;
  213.     }
  214.     public function getProjectShortcode(): ?string
  215.     {
  216.         $this->loadConfig();
  217.         return $this->projectShortcode;
  218.     }
  219.     /**
  220.      *
  221.      * @deprecated
  222.      */
  223.     public function getIgnorePermissions(): bool
  224.     {
  225.         return $this->ignorePermissions;
  226.     }
  227. }