<commit_before><?php

namespace Alpixel\Bundle\CMSBundle\Controller;

use Alpixel\Bundle\CMSBundle\Entity\Node;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Debug\Exception\ContextErrorException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Router;

class NodeController extends Controller
{
    /**
     * @Method({"GET", "POST"})
     *
     * @param Request $request
     * @param         $slug
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function dispatchAction(Request $request, $slug)
    {
        $entityManager = $this->get('doctrine.orm.entity_manager');
        $node = $entityManager->getRepository('AlpixelCMSBundle:Node')
            ->findOneBySlugAndLocale($slug, $request->getLocale());

        if ($node !== null) {
            if ($node->getPublished() === false && !$this->isAuthenticated($request)) {
                throw $this->createNotFoundException();
            }

            $contentType = $this->get('alpixel_cms.helper.cms')->getContentTypeFromNodeElementClass($node);
            $controller = explode('::', $contentType['controller']);

            try {
                if (count($controller) !== 2) {
                    throw new \LogicException(
                        'The parameter controller must be a valid callable controller, like "My\Namespace\Controller\Class::method"'
                    );
                } elseif (!class_exists($controller[0]) || !method_exists($controller[0], $controller[1])) {
                    throw new \LogicException(
                        sprintf(
                            'Unable to find the "%s" controller or the method "%s" doesn\'t exist.',
                            $controller[0],
                            $controller[1]
                        )
                    );
                }

                /** Generating the alternate link for SEO */
                $seoHelper = $this->get('sonata.seo.page.default');
                $translatedPages = $entityManager->getRepository('AlpixelCMSBundle:Node')->findTranslations($node);

                $router = $this->get('router');
                foreach ($translatedPages as $translation) {
                    $seoHelper->addLangAlternate(
                        $router->generate(
                            "alpixel_cms",
                            [
                                'slug'    => $translation->getSlug(),
                                '_locale' => $translation->getLocale(),
                            ],
                            Router::ABSOLUTE_URL
                        ),
                        $translation->getLocale()
                    );
                }

                return $this->forward(
                    $contentType['controller'],
                    [
                        '_route'        => $request->attributes->get('_route'),
                        '_route_params' => $request->attributes->get('_route_params'),
                        'object'        => $node,
                    ]
                );
            } catch (\LogicException $e) {
                if (!$this->container->get('kernel')->isDebug()) {
                    $logger = $this->get('logger');
                    $logger->error($e->getMessage());
                } else {
                    throw $e;
                }
            }
        } else {
            //Trying to find another node with this slug, in another language
            $node = $entityManager->getRepository('AlpixelCMSBundle:Node')
                ->findOnePublishedBySlug($slug);

            if ($node !== null) {
                $translation = $entityManager->getRepository('AlpixelCMSBundle:Node')
                    ->findTranslation($node, $request->getLocale());
                if ($translation !== null) {
                    return $this->redirect(
                        $this->generateUrl(
                            'alpixel_cms',
                            [
                                'slug'    => $translation->getSlug(),
                                '_locale' => $translation->getLocale(),
                            ]
                        )
                    );
                }
            }
        }

        throw $this->createNotFoundException();
    }

    /**
     * @param $node
     *
     * @return Response
     */
    public function displayNodeAdminBarAction(Request $request, $node)
    {
        $entityManager = $this->get('doctrine.orm.entity_manager');
        $node = $entityManager->getRepository('AlpixelCMSBundle:Node')->find($node);

        $response = new Response();
        $response->setPrivate();
        $response->setMaxAge(900);

        if ($this->isAuthenticated($request)) {
            $content = $this->renderView(
                'AlpixelCMSBundle:admin:blocks/admin_bar_page.html.twig',
                [<commit_msg>Update NodeController.php<commit_after><?php

namespace Alpixel\Bundle\CMSBundle\Controller;

use Alpixel\Bundle\CMSBundle\Entity\Node;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Debug\Exception\ContextErrorException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Router;

class NodeController extends Controller
{
    /**
     * @Method({"GET", "POST"})
     *
     * @param Request $request
     * @param         $slug
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function dispatchAction(Request $request, $slug)
    {
        $entityManager = $this->get('doctrine.orm.entity_manager');
        $node = $entityManager->getRepository('AlpixelCMSBundle:Node')
            ->findOneBySlugAndLocale($slug, $request->getLocale());

        if ($node !== null) {
            if ($node->getPublished() === false && !$this->isAuthenticated($request)) {
                throw $this->createNotFoundException();
            }

            $contentType = $this->get('alpixel_cms.helper.cms')->getContentTypeFromNodeElementClass($node);
            $controller = explode('::', $contentType['controller']);

            try {
                if (count($controller) !== 2) {
                    throw new \LogicException(
                        'The parameter controller must be a valid callable controller, like "My\Namespace\Controller\Class::method"'
                    );
                } elseif (!class_exists($controller[0]) || !method_exists($controller[0], $controller[1])) {
                    throw new \LogicException(
                        sprintf(
                            'Unable to find the "%s" controller or the method "%s" doesn\'t exist.',
                            $controller[0],
                            $controller[1]
                        )
                    );
                }

                /** Generating the alternate link for SEO */
                $seoHelper = $this->get('sonata.seo.page.default');
                $translatedPages = $entityManager->getRepository('AlpixelCMSBundle:Node')->findTranslations($node);

                $router = $this->get('router');
                foreach ($translatedPages as $translation) {
                    $seoHelper->addLangAlternate(
                        $router->generate(
                            "alpixel_cms",
                            [
                                'slug'    => $translation->getSlug(),
                                '_locale' => $translation->getLocale(),
                            ],
                            Router::ABSOLUTE_URL
                        ),
                        $translation->getLocale()
                    );
                }

                return $this->forward(
                    $contentType['controller'],
                    [
                        '_route'        => $request->attributes->get('_route'),
                        '_route_params' => $request->attributes->get('_route_params'),
                        'object'        => $node,
                    ]
                );
            } catch (\LogicException $e) {
                if (!$this->container->get('kernel')->isDebug()) {
                    $logger = $this->get('logger');
                    $logger->error($e->getMessage());
                } else {
                    throw $e;
                }
            }
        } else {
            //Trying to find another node with this slug, in another language
            $node = $entityManager->getRepository('AlpixelCMSBundle:Node')
                ->findOnePublishedBySlug($slug);

            if ($node !== null) {
                $translation = $entityManager->getRepository('AlpixelCMSBundle:Node')
                    ->findTranslation($node, $request->getLocale());
                if ($translation !== null) {
                    return $this->redirect(
                        $this->generateUrl(
                            'alpixel_cms',
                            [
                                'slug'    => $translation->getSlug(),
                                '_locale' => $translation->getLocale(),
                            ]
                        ), 301
                    );
                }
            }
        }

        throw $this->createNotFoundException();
    }

    /**
     * @param $node
     *
     * @return Response
     */
    public function displayNodeAdminBarAction(Request $request, $node)
    {
        $entityManager = $this->get('doctrine.orm.entity_manager');
        $node = $entityManager->getRepository('AlpixelCMSBundle:Node')->find($node);

        $response = new Response();
        $response->setPrivate();
        $response->setMaxAge(900);

        if ($this->isAuthenticated($request)) {
            $content = $this->renderView(
                'AlpixelCMSBundle:admin:blocks/admin_bar_page.html.twig',
                [
{'fraction_non_alphanumeric': 0.12471855115026921, 'fraction_numerical': 0.0020558002936857563, 'mean_word_length': 1.4534101825168109, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 109, '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': '2858641', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2373, 'n_words': 402}