custom/plugins/PevrPayeverIntegration/src/Service/PayeverApi/ClientFactory.php line 265

Open in your IDE?
  1. <?php
  2. /**
  3.  * payever GmbH
  4.  *
  5.  * NOTICE OF LICENSE
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in all
  15.  * copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.  * SOFTWARE.
  24.  *
  25.  * DISCLAIMER
  26.  *
  27.  * Do not edit or add to this file if you wish to upgrade payever Shopware package
  28.  * to newer versions in the future.
  29.  *
  30.  * @category    Payever
  31.  * @author      payever GmbH <service@payever.de>
  32.  * @copyright   Copyright (c) 2021 payever GmbH (http://www.payever.de)
  33.  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  34.  */
  35. declare(strict_types=1);
  36. namespace Payever\PayeverPayments\Service\PayeverApi;
  37. use Payever\Sdk\Core\ClientConfiguration;
  38. use Payever\Sdk\Core\Enum\ChannelSet;
  39. use Payever\Sdk\Inventory\InventoryApiClient;
  40. use Payever\Sdk\Payments\PaymentsApiClient;
  41. use Payever\Sdk\Payments\ThirdPartyPluginsApiClient;
  42. use Payever\Sdk\Plugins\PluginsApiClient;
  43. use Payever\Sdk\Products\ProductsApiClient;
  44. use Payever\Sdk\ThirdParty\ThirdPartyApiClient;
  45. use Payever\PayeverPayments\Service\PayeverApi\Core\Authorization\TokenList;
  46. use Payever\PayeverPayments\Service\PayeverApi\Plugins\PluginRegistryInfoProvider;
  47. use Payever\PayeverPayments\Service\Setting\PayeverSettingGeneralStruct;
  48. use Payever\PayeverPayments\Service\Setting\SettingsServiceInterface;
  49. use Psr\Log\LoggerInterface;
  50. /**
  51.  * @SuppressWarnings(PHPMD.TooManyFields)
  52.  */
  53. class ClientFactory
  54. {
  55.     /**
  56.      * @var SettingsServiceInterface
  57.      */
  58.     private $settingsService;
  59.     /**
  60.      * @var PluginRegistryInfoProvider
  61.      */
  62.     private $registryInfoProvider;
  63.     /**
  64.      * @var TokenList
  65.      */
  66.     private $tokenList;
  67.     /**
  68.      * @var LoggerInterface
  69.      */
  70.     private $logger;
  71.     /**
  72.      * @var PaymentsApiClient[]
  73.      */
  74.     private $paymentsApiClients = [];
  75.     /**
  76.      * @var ThirdPartyPluginsApiClient[]
  77.      */
  78.     private $thirdPartyPluginsApiClient = [];
  79.     /**
  80.      * @var PluginsApiClient[]
  81.      */
  82.     private $pluginsApiClients = [];
  83.     /**
  84.      * @var ThirdPartyApiClient
  85.      */
  86.     private $thirdPartyApiClient;
  87.     /**
  88.      * @var ProductsApiClient
  89.      */
  90.     private $productApiClient;
  91.     /**
  92.      * @var InventoryApiClient
  93.      */
  94.     private $inventoryApiClient;
  95.     /**
  96.      * @var ClientConfiguration[]
  97.      */
  98.     private $clientConfigurations = [];
  99.     /**
  100.      * @var ClientConfiguration[]
  101.      */
  102.     private $thirdPartyProductsClientCfgs = [];
  103.     /**
  104.      * @param SettingsServiceInterface $settingsService
  105.      * @param PluginRegistryInfoProvider $pluginRegistryInfoProvider
  106.      * @param TokenList $tokenList
  107.      * @param LoggerInterface $logger
  108.      */
  109.     public function __construct(
  110.         SettingsServiceInterface $settingsService,
  111.         PluginRegistryInfoProvider $pluginRegistryInfoProvider,
  112.         TokenList $tokenList,
  113.         LoggerInterface $logger
  114.     ) {
  115.         $this->settingsService $settingsService;
  116.         $this->registryInfoProvider $pluginRegistryInfoProvider;
  117.         $this->tokenList $tokenList;
  118.         $this->logger $logger;
  119.     }
  120.     /**
  121.      * @param string|null $salesChannelId
  122.      *
  123.      * @return PaymentsApiClient
  124.      *
  125.      * @throws \Exception
  126.      */
  127.     public function getPaymentsApiClient(?string $salesChannelId null): PaymentsApiClient
  128.     {
  129.         if (!isset($this->paymentsApiClients[$salesChannelId])) {
  130.             $this->tokenList->setSalesChannelId($salesChannelId);
  131.             $this->paymentsApiClients[$salesChannelId] = new PaymentsApiClient(
  132.                 $this->getClientConfiguration($salesChannelId),
  133.                 $this->tokenList
  134.             );
  135.         }
  136.         return $this->paymentsApiClients[$salesChannelId];
  137.     }
  138.     /**
  139.      * @param string|null $salesChannelId
  140.      *
  141.      * @return ThirdPartyPluginsApiClient
  142.      *
  143.      * @throws \Exception
  144.      */
  145.     public function getThirdPartyPluginsApiClient(?string $salesChannelId null): ThirdPartyPluginsApiClient
  146.     {
  147.         if (!isset($this->thirdPartyPluginsApiClient[$salesChannelId])) {
  148.             $this->tokenList->setSalesChannelId($salesChannelId);
  149.             $this->thirdPartyPluginsApiClient[$salesChannelId] = new ThirdPartyPluginsApiClient(
  150.                 $this->getClientConfiguration($salesChannelId),
  151.                 $this->tokenList
  152.             );
  153.         }
  154.         return $this->thirdPartyPluginsApiClient[$salesChannelId];
  155.     }
  156.     /**
  157.      * @param string|null $salesChannelId
  158.      *
  159.      * @return PluginsApiClient
  160.      *
  161.      * @throws \Exception
  162.      */
  163.     public function getPluginsApiClient(?string $salesChannelId null): PluginsApiClient
  164.     {
  165.         if (!isset($this->pluginsApiClients[$salesChannelId])) {
  166.             $this->tokenList->setSalesChannelId($salesChannelId);
  167.             $this->pluginsApiClients[$salesChannelId] = new PluginsApiClient(
  168.                 $this->registryInfoProvider,
  169.                 $this->getClientConfiguration($salesChannelId),
  170.                 $this->tokenList
  171.             );
  172.         }
  173.         return $this->pluginsApiClients[$salesChannelId];
  174.     }
  175.     /**
  176.      * @return ThirdPartyApiClient
  177.      * @throws \Exception
  178.      */
  179.     public function getThirdPartyApiClient(): ThirdPartyApiClient
  180.     {
  181.         if (null === $this->thirdPartyApiClient) {
  182.             $this->thirdPartyApiClient = new ThirdPartyApiClient($this->getThirdPartyProductsClientConfigs());
  183.         }
  184.         return $this->thirdPartyApiClient;
  185.     }
  186.     /**
  187.      * @return ProductsApiClient
  188.      * @throws \Exception
  189.      */
  190.     public function getProductsApiClient(): ProductsApiClient
  191.     {
  192.         if (null === $this->productApiClient) {
  193.             $this->productApiClient = new ProductsApiClient($this->getThirdPartyProductsClientConfigs());
  194.         }
  195.         return $this->productApiClient;
  196.     }
  197.     /**
  198.      * @return InventoryApiClient
  199.      * @throws \Exception
  200.      */
  201.     public function getInventoryApiClient(): InventoryApiClient
  202.     {
  203.         if (null === $this->inventoryApiClient) {
  204.             $this->inventoryApiClient = new InventoryApiClient($this->getThirdPartyProductsClientConfigs());
  205.         }
  206.         return $this->inventoryApiClient;
  207.     }
  208.     public function getLogger()
  209.     {
  210.         try {
  211.             // Added this part to try-catche because
  212.             // at first time of activating plugin,
  213.             // the PayeverSettingGeneralStruct is not available,
  214.             // and the plugin cannot activate
  215.             $this->getClientConfiguration();
  216.         } catch (\Exception $exception) {
  217.             //
  218.         }
  219.         return $this->logger;
  220.     }
  221.     /**
  222.      * @retrun void
  223.      */
  224.     public function reset(): void
  225.     {
  226.         $this->paymentsApiClients $this->paymentsApiClients = [];
  227.         $this->clientConfigurations $this->thirdPartyProductsClientCfgs = [];
  228.         $this->thirdPartyApiClient $this->productApiClient $this->inventoryApiClient null;
  229.     }
  230.     /**
  231.      * @param string|null $salesChannelId
  232.      * @return ClientConfiguration
  233.      * @throws \Payever\PayeverPayments\Service\Setting\Exception\PayeverSettingsInvalidException
  234.      * @throws \Exception
  235.      */
  236.     private function getClientConfiguration(?string $salesChannelId null): ClientConfiguration
  237.     {
  238.         if (!isset($this->clientConfigurations[$salesChannelId])) {
  239.             $this->clientConfigurations[$salesChannelId] = $this->loadClientConfiguration(
  240.                 $this->settingsService->getSettings($salesChannelId)
  241.             );
  242.         }
  243.         return $this->clientConfigurations[$salesChannelId];
  244.     }
  245.     /**
  246.      * @param string|null $salesChannelId
  247.      * @return ClientConfiguration
  248.      * @throws \Exception
  249.      */
  250.     private function getThirdPartyProductsClientConfigs(?string $salesChannelId null): ClientConfiguration
  251.     {
  252.         if (!isset($this->thirdPartyProductsClientCfgs[$salesChannelId])) {
  253.             $settings $this->settingsService->getSettings($salesChannelId);
  254.             $clientConfig $this->loadClientConfiguration($settings);
  255.             $clientConfig->setCustomSandboxUrl(null);
  256.             $sandboxUrl $settings->getThirdPartyProductsSandboxUrl();
  257.             if ($sandboxUrl) {
  258.                 $clientConfig->setCustomSandboxUrl($sandboxUrl);
  259.             }
  260.             $clientConfig->setCustomLiveUrl(null);
  261.             $liveUrl $settings->getThirdPartyProductsLiveUrl();
  262.             if ($liveUrl) {
  263.                 $clientConfig->setCustomLiveUrl($liveUrl);
  264.             }
  265.             $this->thirdPartyProductsClientCfgs[$salesChannelId]  = $clientConfig;
  266.         }
  267.         return $this->thirdPartyProductsClientCfgs[$salesChannelId];
  268.     }
  269.     /**
  270.      * @param PayeverSettingGeneralStruct $settings
  271.      * @return ClientConfiguration
  272.      * @throws \Exception
  273.      */
  274.     private function loadClientConfiguration(PayeverSettingGeneralStruct $settings): ClientConfiguration
  275.     {
  276.         $clientConfiguration = new ClientConfiguration();
  277.         if ($settings) {
  278.             $apiMode $settings->isSandbox()
  279.                 ? ClientConfiguration::API_MODE_SANDBOX
  280.                 ClientConfiguration::API_MODE_LIVE;
  281.             $clientConfiguration->setChannelSet(ChannelSet::CHANNEL_SHOPWARE)
  282.                 ->setApiMode($apiMode)
  283.                 ->setClientId($settings->getClientId())
  284.                 ->setClientSecret($settings->getClientSecret())
  285.                 ->setBusinessUuid($settings->getBusinessUuid())
  286.                 ->setCustomSandboxUrl($settings->getSandboxUrl())
  287.                 ->setCustomLiveUrl($settings->getLiveUrl())
  288.                 ->setLogDiagnostic($settings->hasDiagnostic())
  289.                 ->setApmSecretService($this->getPayeverApmSecretServicePrototype())
  290.                 ->setLogger($this->logger);
  291.         }
  292.         $this->logger $clientConfiguration->getLogger();
  293.         return $clientConfiguration;
  294.     }
  295.     private function getPayeverApmSecretServicePrototype()
  296.     {
  297.         return new PayeverApmSecretService($this->settingsService);
  298.     }
  299. }