diff --git a/Components/CurrentCustomer.php b/Components/CurrentCustomer.php index 68f04637..0f569516 100644 --- a/Components/CurrentCustomer.php +++ b/Components/CurrentCustomer.php @@ -2,6 +2,11 @@ namespace MollieShopware\Components; +use ArrayObject; +use Psr\Log\LoggerInterface; +use Shopware\Models\Customer\Customer; +use function sprintf; + class CurrentCustomer { /** @var \Enlight_Components_Session_Namespace */ @@ -10,12 +15,17 @@ class CurrentCustomer /** @var \Shopware\Components\Model\ModelManager */ protected $modelManager; + /** @var LoggerInterface */ + private $logger; + public function __construct( \Enlight_Components_Session_Namespace $session, - \Shopware\Components\Model\ModelManager $modelManager + \Shopware\Components\Model\ModelManager $modelManager, + LoggerInterface $logger ) { $this->session = $session; $this->modelManager = $modelManager; + $this->logger = $logger; } /** @@ -25,7 +35,46 @@ public function __construct( */ public function getCurrentId() { - return !empty($this->session->sUserId) ? $this->session->sUserId : $this->session->offsetGet('auto-user'); + /** @var null|numeric-string $customerId */ + $customerId = $this->session->offsetGet('sUserId'); + if (!empty($customerId)) { + return (int)$customerId; + } + + $this->logger->error('sUserId not set in session'); + + /** @var null|int $customerId */ + $customerId = $this->session->offsetGet('auto-user'); + if (!empty($customerId)) { + return (int)$customerId; + } + + $this->logger->error('auto-user not set in session'); + + /** @var null|ArrayObject $sOrderVariables */ + $sOrderVariables = $this->session->offsetGet('sOrderVariables'); + if (!($sOrderVariables instanceof ArrayObject)) { + $this->logger->error('sOrderVariables not set in session'); + + return 0; + } + + /** @var null|array $sUserData */ + $sUserData = $sOrderVariables->offsetGet('sUserData'); + if ($sUserData === null) { + $this->logger->error('sUserData not set in session'); + + return 0; + } + + $customerId = isset($sUserData['additional']['user']['id']) ? $sUserData['additional']['user']['id'] : 0; + if (empty($customerId)) { + $this->logger->error('sUserData does not contain a user id'); + + return 0; + } + + return (int)$customerId; } /** @@ -35,17 +84,26 @@ public function getCurrentId() */ public function getCurrent() { - $userId = $this->getCurrentId(); + /** @var int $customerId */ + $customerId = $this->getCurrentId(); + + if ($customerId === 0) { + $this->logger->error('no customer id found'); - if (empty($userId)) { return null; } - /** @var \Shopware\Models\Customer\Customer $customer */ + /** @var null|\Shopware\Models\Customer\Customer $customer */ $customer = $this->modelManager->getRepository( \Shopware\Models\Customer\Customer::class - )->find($userId); + )->find($customerId); + + if ($customer instanceof Customer) { + return $customer; + } + + $this->logger->error(sprintf('customer with id "%d" not found', $customerId)); - return $customer; + return null; } } diff --git a/Components/Order/OrderCancellation.php b/Components/Order/OrderCancellation.php index fe95fc72..7b1b9667 100644 --- a/Components/Order/OrderCancellation.php +++ b/Components/Order/OrderCancellation.php @@ -103,7 +103,11 @@ public function cancelAndRestoreByOrder($swOrder) # it's important to restore the order before the placed order is cancelled # otherwise the original quantity of the line items can't be restored - $currentCustomer = new CurrentCustomer(Shopware()->Session(), Shopware()->Models()); + $currentCustomer = new CurrentCustomer( + Shopware()->Session(), + Shopware()->Models(), + Shopware()->Container()->get('mollie_shopware.components.logger') + ); if ((int)$currentCustomer->getCurrentId() === (int)$swOrder->getCustomer()->getId()) { $this->restoreCartFromOrder($swOrder); } diff --git a/Facades/CheckoutSession/CheckoutSessionFacade.php b/Facades/CheckoutSession/CheckoutSessionFacade.php index ae197d66..92bc0fdf 100644 --- a/Facades/CheckoutSession/CheckoutSessionFacade.php +++ b/Facades/CheckoutSession/CheckoutSessionFacade.php @@ -308,7 +308,11 @@ public function startCheckoutSession($basketUserId, $paymentShortName, $basketSi */ private function buildTransaction($basketSignature, $currency) { - $currentCustomerClass = new CurrentCustomer(Shopware()->Session(), Shopware()->Models()); + $currentCustomerClass = new CurrentCustomer( + Shopware()->Session(), + Shopware()->Models(), + Shopware()->Container()->get('mollie_shopware.components.logger') + ); $customer = $currentCustomerClass->getCurrent(); $locale = $this->localeFinder->getPaymentLocale(Shopware()->Shop()->getLocale()->getLocale()); diff --git a/Resources/services/components/mixed.xml b/Resources/services/components/mixed.xml index 3313066a..32fa26b2 100644 --- a/Resources/services/components/mixed.xml +++ b/Resources/services/components/mixed.xml @@ -147,6 +147,7 @@ + - \ No newline at end of file +