<commit_before><?php

namespace Wsh\LapiBackendBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Components\CssSelector\Parser;
use Symfony\Component\DomCrawler\Crawler;
use Wsh\LapiBackendBundle\Form\OfferImportType;
use Wsh\LapiBundle\Entity\Offer;
use Wsh\LapiBundle\OfferProvider\Qtravel\Provider as Qtravel;

class AdminController extends Controller
{

    /**
     * @Route("/import-offer")
     * @Template()
     */
    public function importOfferAction(Request $request)
    {
        $offer = new Offer();
        $form = $this->createForm(new OfferImportType());

        $form->handleRequest($request);

        if($form->isValid()) {
            $data = $form->getData();
            // parse the url
            $provider = new Qtravel($this->container);
            $offerId = $provider->parseUrl($data['offerUrl']);

            $em = $this->getDoctrine()->getManager();
            // check if this offer allready exists
            $repo = $em->getRepository('WshLapiBundle:Offer');
            $offer = $repo->findOneByQTravelOfferId($offerId);
            if(!$offer) {
                set_time_limit(0);

                $content = $provider->sendRequest($data['offerUrl']);
                $crawler = new Crawler($content);
                $crawler = $crawler->filter('a.book-term')->first()->attr('href');

                $content = $provider->sendRequest($crawler);
                $crawler = new Crawler($content);
                $offerCode = $crawler->filter('div.code strong')->text();

                $offer = new Offer();

                $offerJson = $provider->findOfferById($offerCode);
                $offer = $provider->transformSingleOfferToEntity($offerJson, $data['isFeatured'], $data['isHotDeal']);

            }
            $em->persist($offer);
            $em->flush();
            $this->get('session')->getFlashBag()->add(
                'notice',
                'Nowa oferta została zaimportowana'
            );

            return $this->redirect($this->generateUrl('admin_wsh_lapi_offer_list'));
        }

        return array(
            'form' => $form->createView(),
            'admin_pool'      => $this->container->get('sonata.admin.pool'),
        );
    }
}<commit_msg>Fixes in offer import action in backend.<commit_after><?php

namespace Wsh\LapiBackendBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Components\CssSelector\Parser;
use Symfony\Component\DomCrawler\Crawler;
use Wsh\LapiBackendBundle\Form\OfferImportType;
use Wsh\LapiBundle\Entity\Offer;
use Wsh\LapiBundle\OfferProvider\Qtravel\Provider as Qtravel;

class AdminController extends Controller
{

    /**
     * @Route("/import-offer")
     * @Template()
     */
    public function importOfferAction(Request $request)
    {
        $offer = new Offer();
        $form = $this->createForm(new OfferImportType());

        $form->handleRequest($request);

        if($form->isValid()) {
            $data = $form->getData();
            // parse the url
            $provider = new Qtravel($this->container);
            $offerId = $provider->parseUrl($data['offerUrl']);

            $em = $this->getDoctrine()->getManager();
            // check if this offer allready exists
            $repo = $em->getRepository('WshLapiBundle:Offer');
            $offer = $repo->findOneByQTravelOfferId($offerId);
            if(!$offer) {
                set_time_limit(0);

                $content = $provider->sendRequest($data['offerUrl']);
                $crawler = new Crawler($content);
                $crawler = $crawler->filter('a.book-term')->first()->attr('href');

                $content = $provider->sendRequest($crawler);
                $crawler = new Crawler($content);
                $offerCode = $crawler->filter('div.code strong')->text();

                $offer = $repo->findOneByQTravelOfferId($offerCode);
                if(!$offer) {
                    $offer = new Offer();
                    $offerJson = $provider->findOfferById($offerCode);
                    $offer = $provider->transformSingleOfferToEntity($offerJson, $data['isFeatured'], $data['isHotDeal']);
                } else {
                    $this->get('session')->getFlashBag()->add(
                        'sonata_flash_error',
                        'Podana oferta istnieje już w bazie.'
                    );
                    return $this->redirect($this->generateUrl('admin_wsh_lapi_offer_list'));
                }
            }
            $em->persist($offer);
            $em->flush();
            $this->get('session')->getFlashBag()->add(
                'sonata_flash_success',
                'Nowa oferta została zaimportowana'
            );

            return $this->redirect($this->generateUrl('admin_wsh_lapi_offer_list'));
        }

        return array(
            'form' => $form->createView(),
            'admin_pool'      => $this->container->get('sonata.admin.pool'),
        );
    }
}
{'fraction_non_alphanumeric': 0.14373601789709173, 'fraction_numerical': 0.0003728560775540641, 'mean_word_length': 2.2534869617950273, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, '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': '4226748', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1481, 'n_words': 230}