<?php

namespace KupShop\ContentBundle\Controller;

use KupShop\ContentBundle\Util\CartMerge;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class CartMergeController extends AbstractController
{
    /**
     * @Route("/cart-merge/")
     */
    public function cartMergeAction(Request $request, CartMerge $cartMerge)
    {
        // Temporary log
        /** @var LoggerInterface $logger */
        $logger = ServiceContainer::getService('logger');
        $logger->notice('Cart merge url', ['url' => $request->getUri(), 'referer' => $request->headers->get('referer')]);

        $cartId = $request->get('cartId');

        if ($cartId) {
            $cartMerge->merge($cartId);
        }

        return new RedirectResponse(path('kupshop_content_cart_cart_1'));
    }
}
