<?php
/**
 * Magetop Store
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */

namespace Magetop\Quotesystem\Plugin\Customer\Controller\Account;

use Magento\Customer\Model\Session;

class CreatePost
{
    /**
     * @var Session
     */
    protected $customerSession;

    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutsession;
    
    /**
     * @param Session                         $customerSession
     * @param \Magento\Checkout\Model\Session $checkoutsession
     */
    public function __construct(
        Session $customerSession,
        \Magento\Checkout\Model\Session $checkoutsession
    ) {
        $this->session = $customerSession;
        $this->checkoutSession = $checkoutsession;
    }

    /**
     * Change redirect after login
     *
     * @param \Magento\Customer\Controller\Account\CreatePost $subject
     * @param \Magento\Framework\Controller\Result\Redirect   $result
     */
    public function afterExecute(
        \Magento\Customer\Controller\Account\CreatePost $subject,
        $result
    ) {
        $customerId = $this->session->getCustomer()->getId();
        $refererUrl = $this->checkoutSession->getReferUrl();
        if ($refererUrl && $customerId) {
            $result->setPath($refererUrl);
        }
        return $result;
    }
}
