<commit_before><?php
/**
 * This file is part of the Networking package.
 *
 * (c) net working AG <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Networking\InitCmsBundle\Controller;

use Networking\InitCmsBundle\Admin\Model\PageAdmin;
use Networking\InitCmsBundle\Cache\PageCacheInterface;
use Networking\InitCmsBundle\Helper\PageHelper;
use Networking\InitCmsBundle\Model\ContentRouteManager;
use Networking\InitCmsBundle\Model\Page;
use Networking\InitCmsBundle\Model\PageManagerInterface;
use Sonata\AdminBundle\Admin\Pool;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Networking\InitCmsBundle\Model\PageInterface;
use Networking\InitCmsBundle\Model\PageSnapshot;
use Networking\InitCmsBundle\Model\PageSnapshotInterface;
use Networking\InitCmsBundle\Helper\LanguageSwitcherHelper;

/**
 * Class FrontendPageController.
 *
 * @author net working AG <<EMAIL>>
 */
class FrontendPageController extends AbstractController
{
    /**
     * @var PageCacheInterface
     */
    protected $pageCache;

    /**
     * @var TokenStorageInterface
     */
    protected $tokenStorage;

    /**
     * @var AuthorizationCheckerInterface
     */
    protected $authorizationChecker;

    /**
     * @var Pool
     */
    protected $pool;

    /**
     * @var LanguageSwitcherHelper
     */
    protected $languageSwitcherHelper;

    /**
     * @var PageManagerInterface
     */
    protected $pageManager;

    /**
     * @var PageHelper
     */
    protected $pageHelper;

    /**
     * FrontendPageController constructor.
     * @param PageCacheInterface $pageCache
     * @param TokenStorageInterface $tokenStorage
     * @param AuthorizationCheckerInterface $authorizationChecker
     * @param Pool $pool
     * @param LanguageSwitcherHelper $languageSwitcherHelper
     * @param PageManagerInterface $pageManager
     * @param PageHelper $pageHelper
     */
    public function __construct(
        PageCacheInterface $pageCache,
        TokenStorageInterface $tokenStorage,
        AuthorizationCheckerInterface $authorizationChecker,
        Pool $pool,
        LanguageSwitcherHelper $languageSwitcherHelper,
        PageManagerInterface $pageManager,
        PageHelper $pageHelper
    ) {
        $this->pageCache = $pageCache;
        $this->tokenStorage = $tokenStorage;
        $this->authorizationChecker = $authorizationChecker;
        $this->pool = $pool;
        $this->languageSwitcherHelper = $languageSwitcherHelper;
        $this->pageManager = $pageManager;
        $this->pageHelper = $pageHelper;

    }

    /**
     * @return bool|\Sonata\AdminBundle\Admin\Pool
     */
    protected function getAdminPool()
    {
        if ($this->tokenStorage->getToken() && $this->authorizationChecker->isGranted(
                'ROLE_SONATA_ADMIN'
            )
        ) {
            return $this->pool;
        }

        return false;
    }

    /**
     * @param Request $request
     * @return array|bool|mixed|string|RedirectResponse|Response|null
     * @throws \Psr\Cache\InvalidArgumentException
     */
    public function indexAction(Request $request)
    {
        /** @var PageSnapshotInterface $page */
        $page = $request->get('_content');

        $template = $request->get('_template');
        if ($template instanceof \Sensio\Bundle\FrameworkExtraBundle\Configuration\Template) {
            $template = $template->getTemplate();
        }
        $user = $this->getUser();

        if ($this->pageCache->isCacheable($request, $user) && $page instanceof PageSnapshotInterface) {
            if (!$this->isSnapshotActive($page)) {
                throw new NotFoundHttpException();
            }

            if ($this->getSnapshotVisibility($page) != PageInterface::VISIBILITY_PUBLIC) {
                if (false === $this->authorizationChecker->isGranted('ROLE_USER')) {
                    throw new AccessDeniedException();
                }
            }

            $updatedAt = $this->pageCache->get(sprintf('page_%s_created_at', $page->getId()));
            $cacheKey = $request->getLocale().$request->getPathInfo();

            if ($updatedAt != $page->getSnapshotDate()) {
                $this->pageCache->delete($cacheKey);
            }

            $response = $this->pageCache->get($request->getLocale().$request->getPathInfo());

            if (!$response || !$response instanceof Response) {
                $params = $this->getPageParameters($request);

                if ($params instanceof RedirectResponse) {
                    return $params;
                }
                $html = $this->renderView(
                    $template,
                    $params
                );<commit_msg>fix cache problem with MD5 hash of de/<commit_after><?php
/**
 * This file is part of the Networking package.
 *
 * (c) net working AG <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Networking\InitCmsBundle\Controller;

use Networking\InitCmsBundle\Admin\Model\PageAdmin;
use Networking\InitCmsBundle\Cache\PageCacheInterface;
use Networking\InitCmsBundle\Helper\PageHelper;
use Networking\InitCmsBundle\Model\ContentRouteManager;
use Networking\InitCmsBundle\Model\Page;
use Networking\InitCmsBundle\Model\PageManagerInterface;
use Sonata\AdminBundle\Admin\Pool;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Networking\InitCmsBundle\Model\PageInterface;
use Networking\InitCmsBundle\Model\PageSnapshot;
use Networking\InitCmsBundle\Model\PageSnapshotInterface;
use Networking\InitCmsBundle\Helper\LanguageSwitcherHelper;

/**
 * Class FrontendPageController.
 *
 * @author net working AG <<EMAIL>>
 */
class FrontendPageController extends AbstractController
{
    /**
     * @var PageCacheInterface
     */
    protected $pageCache;

    /**
     * @var TokenStorageInterface
     */
    protected $tokenStorage;

    /**
     * @var AuthorizationCheckerInterface
     */
    protected $authorizationChecker;

    /**
     * @var Pool
     */
    protected $pool;

    /**
     * @var LanguageSwitcherHelper
     */
    protected $languageSwitcherHelper;

    /**
     * @var PageManagerInterface
     */
    protected $pageManager;

    /**
     * @var PageHelper
     */
    protected $pageHelper;

    /**
     * FrontendPageController constructor.
     * @param PageCacheInterface $pageCache
     * @param TokenStorageInterface $tokenStorage
     * @param AuthorizationCheckerInterface $authorizationChecker
     * @param Pool $pool
     * @param LanguageSwitcherHelper $languageSwitcherHelper
     * @param PageManagerInterface $pageManager
     * @param PageHelper $pageHelper
     */
    public function __construct(
        PageCacheInterface $pageCache,
        TokenStorageInterface $tokenStorage,
        AuthorizationCheckerInterface $authorizationChecker,
        Pool $pool,
        LanguageSwitcherHelper $languageSwitcherHelper,
        PageManagerInterface $pageManager,
        PageHelper $pageHelper
    ) {
        $this->pageCache = $pageCache;
        $this->tokenStorage = $tokenStorage;
        $this->authorizationChecker = $authorizationChecker;
        $this->pool = $pool;
        $this->languageSwitcherHelper = $languageSwitcherHelper;
        $this->pageManager = $pageManager;
        $this->pageHelper = $pageHelper;

    }

    /**
     * @return bool|\Sonata\AdminBundle\Admin\Pool
     */
    protected function getAdminPool()
    {
        if ($this->tokenStorage->getToken() && $this->authorizationChecker->isGranted(
                'ROLE_SONATA_ADMIN'
            )
        ) {
            return $this->pool;
        }

        return false;
    }

    /**
     * @param Request $request
     * @return array|bool|mixed|string|RedirectResponse|Response|null
     * @throws \Psr\Cache\InvalidArgumentException
     */
    public function indexAction(Request $request)
    {
        /** @var PageSnapshotInterface $page */
        $page = $request->get('_content');

        $template = $request->get('_template');
        if ($template instanceof \Sensio\Bundle\FrameworkExtraBundle\Configuration\Template) {
            $template = $template->getTemplate();
        }
        $user = $this->getUser();

        if ($this->pageCache->isCacheable($request, $user) && $page instanceof PageSnapshotInterface) {
            if (!$this->isSnapshotActive($page)) {
                throw new NotFoundHttpException();
            }

            if ($this->getSnapshotVisibility($page) != PageInterface::VISIBILITY_PUBLIC) {
                if (false === $this->authorizationChecker->isGranted('ROLE_USER')) {
                    throw new AccessDeniedException();
                }
            }

            $updatedAt = $this->pageCache->get(sprintf('page_%s_created_at', $page->getId()));
            $cacheKey = 'page_'.$request->getLocale().$request->getPathInfo();

            if ($updatedAt != $page->getSnapshotDate()) {
                $this->pageCache->delete($cacheKey);
            }

            $response = $this->pageCache->get($cacheKey);

            if (!$response || !$response instanceof Response) {
                $params = $this->getPageParameters($request);

                if ($params instanceof RedirectResponse) {
                    return $params;
                }
                $html = $this->renderView(
                    $template,
                    $params
                );
{'fraction_non_alphanumeric': 0.10869365647663637, 'fraction_numerical': 9.180207472688883e-05, 'mean_word_length': 2.881011756323477, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 81, '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': '3094167', 'n_tokens_mistral': 2880, 'n_tokens_neox': 2856, 'n_words': 529}