UnsetCookie.php 1.24 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Persistent\Controller\Index;

use Magento\Persistent\Controller\Index;
use Magento\Framework\Controller\ResultFactory;

class UnsetCookie extends Index
{
    /**
     * Unset persistent cookie action
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     */
    public function execute()
    {
        if ($this->sessionHelper->isPersistent()) {
            $this->cleanup();
        }
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setPath('customer/account/login');
        return $resultRedirect;
    }

    /**
     * Revert all persistent data
     *
     * @return $this
     */
    protected function cleanup()
    {
        $this->_eventManager->dispatch('persistent_session_expired');
        $this->customerSession->setCustomerId(null)->setCustomerGroupId(null);
        if ($this->clearCheckoutSession) {
            $this->checkoutSession->clearStorage();
        }
        $this->sessionHelper->getSession()->removePersistentCookie();
        return $this;
    }
}