<commit_before><?php

/*
 * This file is part of jdj.
 *
 * (c) Mobizel
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace AppBundle\Controller;

use AppBundle\Entity\ProductList;
use AppBundle\Entity\ProductListItem;
use AppBundle\Factory\ProductListFactory;
use FOS\RestBundle\View\View;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Component\Resource\ResourceActions;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * @author <NAME> <<EMAIL>>
 */
class ProductListController extends ResourceController
{
    /**
     * TODO handle directly productListItemController
     *
     * @param Request $request
     * @param int $productId
     *
     * @return Response
     */
    public function addProductAction(Request $request, $productId)
    {
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
        /** @var ProductList $list */
        $list = $this->singleResourceProvider->get($configuration, $this->repository);

        if (null === $list) {
            /** @var ProductListFactory $factory */
            $factory = $this->factory;
            $list = $factory->createForCode($request->get('code'));
            $this->manager->persist($list);
        }


        $product = $this->get('sylius.repository.product')->find($productId);

        if (null === $product) {
            throw new NotFoundHttpException();
        }

        /** @var ProductListItem $item */
        $item = $this->get('app.factory.product_list_item')->createNew();
        $item
            ->setProduct($product)
            ->setList($list);

        $list->addItem($item);
        $this->manager->flush();

        if (!$configuration->isHtmlRequest()) {
            return $this->viewHandler->handle($configuration, View::create($list, Response::HTTP_CREATED));
        }

        $view = View::create()
            ->setData([
                'configuration' => $configuration,
                'metadata' => $this->metadata,
                'resource' => $list,
                $this->metadata->getName() => $list,
            ])
            ->setTemplate($configuration->getTemplate(ResourceActions::UPDATE . '.html'));

        return $this->viewHandler->handle($configuration, $view);
    }

    /**
     * TODO handle directly productListItemController
     *
     * @param Request $request
     * @param $productId
     *
     * @return Response
     */
    public function removeProductAction(Request $request, $productId)
    {
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
        /** @var ProductList $list */
        $list = $this->findOr404($configuration);

        $product = $this->get('sylius.repository.product')->find($productId);

        if (null === $product) {
            throw new NotFoundHttpException();
        }

        $item = $this->get('app.repository.product_list_item')->findOneBy(['product' => $product, 'list' => $list]);

        if (null === $item) {
            throw new NotFoundHttpException();
        }

        $list->removeItem($item);
        $this->manager->remove($item);
        $this->manager->flush();

        if (!$configuration->isHtmlRequest()) {
            return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
        }

        $view = View::create()
            ->setData([
                'configuration' => $configuration,<commit_msg>fix adding or removing a product to a list<commit_after><?php

/*
 * This file is part of jdj.
 *
 * (c) Mobizel
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace AppBundle\Controller;

use AppBundle\Entity\ProductList;
use AppBundle\Entity\ProductListItem;
use AppBundle\Factory\ProductListFactory;
use FOS\RestBundle\View\View;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Component\Product\Model\ProductInterface;
use Sylius\Component\Resource\ResourceActions;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * @author <NAME> <<EMAIL>>
 */
class ProductListController extends ResourceController
{
    /**
     * TODO handle directly productListItemController
     *
     * @param Request $request
     * @param int $productId
     *
     * @return Response
     */
    public function addProductAction(Request $request, $productId)
    {
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
        /** @var ProductList $list */
        $list = $this->singleResourceProvider->get($configuration, $this->repository);

        if (null === $list) {
            /** @var ProductListFactory $factory */
            $factory = $this->factory;
            $list = $factory->createForCode($request->get('code'));
            $this->manager->persist($list);
        }

        /** @var ProductInterface $product */
        $product = $this->get('sylius.repository.product')->find($productId);

        if (null === $product) {
            throw new NotFoundHttpException();
        }

        /** @var ProductListItem $item */
        $item = $this->get('app.factory.product_list_item')->createNew();
        $item->setProduct($product);
        $item->setList($list);

        $list->addItem($item);
        $this->manager->flush();

        if (!$configuration->isHtmlRequest()) {
            return $this->viewHandler->handle($configuration, View::create($list, Response::HTTP_CREATED));
        }

        $view = View::create()
            ->setData([
                'configuration' => $configuration,
                'metadata' => $this->metadata,
                'resource' => $list,
                $this->metadata->getName() => $list,
            ])
            ->setTemplate($configuration->getTemplate(ResourceActions::UPDATE . '.html'));

        return $this->viewHandler->handle($configuration, $view);
    }

    /**
     * TODO handle directly productListItemController
     *
     * @param Request $request
     * @param $productId
     *
     * @return Response
     */
    public function removeProductAction(Request $request, $productId)
    {
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

        $this->isGrantedOr403($configuration, ResourceActions::UPDATE);
        /** @var ProductList $list */
        $list = $this->findOr404($configuration);

        $product = $this->get('sylius.repository.product')->find($productId);

        if (null === $product) {
            throw new NotFoundHttpException();
        }

        /** @var ProductListItem $item */
        $item = $this->get('app.repository.product_list_item')->findOneBy(['product' => $product, 'list' => $list]);

        if (null === $item) {
            throw new NotFoundHttpException();
        }

        $list->removeItem($item);
        $this->manager->remove($item);
        $this->manager->flush();

        if (!$configuration->isHtmlRequest()) {
            return $this->viewHandler->handle($configuration, View::create(null, Response::HTTP_NO_CONTENT));
        }

        $view = View::create()
            ->setData([
                'configuration' => $configuration,
{'fraction_non_alphanumeric': 0.14643223819301848, 'fraction_numerical': 0.0023100616016427105, 'mean_word_length': 2.716261325703386, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3743274', 'n_tokens_mistral': 2090, 'n_tokens_neox': 2030, 'n_words': 400}