custom/plugins/UandiEfbErpSynchronization/src/Service/Api/SoapService.php line 83

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uandi\UandiEfbErpSynchronization\Service\Api;
  3. use Exception;
  4. use Monolog\Logger;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use SoapFault;
  7. use Uandi\UandiEfbErpSynchronization\Service\Api\SoapClient as SoapClient;
  8. use Uandi\UandiEfbErpSynchronization\Service\ApiConnectionServiceInterface;
  9. class SoapService implements ApiConnectionServiceInterface
  10. {
  11.     private SystemConfigService $systemConfigService;
  12.     private Logger $logger;
  13.     public function __construct(SystemConfigService $systemConfigServiceLogger $logger) {
  14.         $this->systemConfigService $systemConfigService;
  15.         $this->logger $logger;
  16.     }
  17.     /**
  18.      * Execute soap request
  19.      *
  20.      * @param array $request
  21.      * @param string|null $requestName
  22.      * @param string|null $salesChannelId
  23.      * @return array
  24.      */
  25.     public function execute(array $request = [], ?string $requestName null, ?string $salesChannelId null): array
  26.     {
  27.         try {
  28.             //encode the request array
  29.             $jsonRequest json_encode($request);
  30.             // create ERP request
  31.             $client $this->createConnection($salesChannelId);
  32.             $jsonResponse $client->$requestName(["request_p" => $jsonRequest]);
  33.             $returnValue $jsonResponse->return_value;
  34.             $responseArray $this->jsonDecode($returnValue);
  35.             // handle/log the error response
  36.             if (isset($responseArray['error'])) {
  37.                 $this->logger->critical(
  38.                     sprintf(
  39.                         '%s: Request name: %s, Error code: %s, Error text: %s',
  40.                         __METHOD__,
  41.                         $requestName,
  42.                         $responseArray['error']['error_code'],
  43.                         $responseArray['error']['error_text']
  44.                     )
  45.                 );
  46.                 return ['request' => $request];
  47.             }
  48.             $responseArray['request'] = $request;
  49.             return $responseArray;
  50.         } catch (Exception $e) {
  51.             $this->logger->critical(
  52.                 sprintf(
  53.                     '%s, %s: %s, trace: %s',
  54.                     $request['customer_number'] ?? '',
  55.                     __METHOD__,
  56.                     $e->getMessage(),
  57.                     $e->getTraceAsString()
  58.                 )
  59.             );
  60.             return ['request' => $request];
  61.         }
  62.     }
  63.     /**
  64.      * Create connection to the ERP
  65.      *
  66.      * @param string|null $salesChannelId
  67.      * @return SoapClient
  68.      * @throws SoapFault
  69.      */
  70.     private function createConnection(?string $salesChannelId null): SoapClient
  71.     {
  72.         $wsdl $this->systemConfigService->get('UandiEfbErpSynchronization.config.soapApiEndpoint'$salesChannelId);
  73.         $user $this->systemConfigService->get('UandiEfbErpSynchronization.config.soapApiUsername'$salesChannelId);
  74.         $password $this->systemConfigService->get('UandiEfbErpSynchronization.config.soapApiPassword'$salesChannelId);
  75.         return new SoapClient(
  76.             $wsdl,
  77.             array('user' => $user'password' => $password)
  78.         );
  79.     }
  80.     /**
  81.      * Decodes json
  82.      *
  83.      * @param $json
  84.      *
  85.      * @return array
  86.      */
  87.     public function jsonDecode($json): array
  88.     {
  89.         $data json_decode($jsontrue);
  90.         if (JSON_ERROR_NONE !== json_last_error()) {
  91.             $errorMessage['message'] = 'JSON is invalid';
  92.             $errorMessage['json_last_error_msg'] = json_last_error_msg();
  93.             $errorMessage['json'] = $json;
  94.             $this->logger->critical(sprintf('%s: error %s'__METHOD__json_encode($errorMessageJSON_PRETTY_PRINT)));
  95.         }
  96.         return $data === null ? [] : $data;
  97.     }
  98. }