* @copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL * @license https://opensource.org/licenses/MIT The MIT License * * Don't forget to prefix your containers with your own identifier * to avoid any conflicts with others containers. */ class RetailcrmReferenceMiddleware implements RetailcrmMiddlewareInterface { /** * {@inheritDoc} */ public function __invoke(RetailcrmApiRequest $request, callable $next = null) { /** @var RetailcrmApiResponse $response */ $response = $next($request); if (null !== $response && $response->isSuccessful() && ( 'ordersCreate' === $request->getMethod() || 'ordersEdit' === $request->getMethod() ) ) { $receiveOrderNumber = (bool) (Configuration::get(RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING)); $crmOrder = $response->order; if ($receiveOrderNumber && isset($crmOrder['externalId'], $crmOrder['number']) ) { $object = new Order($crmOrder['externalId']); $object->reference = $crmOrder['number']; $object->update(); } } return $response; } }