<commit_before><?php

declare(strict_types=1);

namespace whatwedo\CrudBundle\View;

use Doctrine\Common\Annotations\Reader;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormRegistryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Http\AccessMap;
use Twig\Environment;
use whatwedo\CoreBundle\Action\Action;
use whatwedo\CrudBundle\Block\Block;
use whatwedo\CrudBundle\Collection\BlockCollection;
use whatwedo\CrudBundle\Content\AbstractContent;
use whatwedo\CrudBundle\Content\Content;
use whatwedo\CrudBundle\Definition\AbstractDefinition;
use whatwedo\CrudBundle\Definition\DefinitionInterface;
use whatwedo\CrudBundle\Enum\Page;
use whatwedo\CrudBundle\Form\Type\EntityAjaxType;
use whatwedo\CrudBundle\Form\Type\EntityHiddenType;
use whatwedo\CrudBundle\Form\Type\EntityPreselectType;
use whatwedo\CrudBundle\Manager\DefinitionManager;

class DefinitionView
{
    protected ?object $data = null;

    protected Page $route;

    protected ?FormInterface $form = null;

    protected DefinitionInterface $definition;

    protected \ReflectionObject $reflectionObject;

    public function __construct(
        protected DefinitionManager $definitionManager,
        protected Environment $templating,
        protected FormRegistryInterface $formRegistry,
        protected FormFactoryInterface $formFactory,
        protected RouterInterface $router,
        protected RequestStack $requestStack,
        protected AuthorizationCheckerInterface $authorizationChecker,
        protected Reader $annotationReader,
    ) {
    }

    public function create(DefinitionInterface $definition, Page $route, ?object $data = null): self
    {
        $view = clone $this;
        $view->setDefinition($definition);
        $view->setData($data);
        $view->setRoute($route);

        return $view;
    }

    public function setDefinition(DefinitionInterface $definition)
    {
        $this->definition = $definition;
    }

    public function setData($data)
    {
        $this->data = $data;
    }

    /**
     * @return object
     */
    public function getData()
    {
        return $this->data;
    }

    public function getRoute(): Page
    {
        return $this->route;
    }

    public function setRoute(Page $route): self
    {
        $this->route = $route;

        return $this;
    }

    public function getActions(): iterable
    {
        return array_filter(
            $this->definition->getActions(),
            fn (Action $action) => in_array($this->route, $action->getOption('visibility'), true)
        );
    }

    /**
     * @return BlockCollection|Block[]
     */
    public function getBlocks(?Page $page = null)
    {
        return $page
            ? $this->definition->getBuilder()->getBlocks()->filterVisibility($page)
            : $this->definition->getBuilder()->getBlocks();
    }

    public function render(): string
    {
        return $this->templating->render($this->getTemplatePath('_boxes/' . $this->getRoute() . '.html.twig'), [
            'data' => $this->data,
            'helper' => $this,
        ]);
    }

    /**
     * @param string $value text to be rendered
     *
     * @return string html
     */
    public function linkIt($value, Content $content)
    {
        return (string) $value;
        // TODO refactor
        $entity = $content->getContents($this->data);
        $def = $this->definitionManager->getDefinitionByEntity($entity);

        if ($def !== null) {
            if ($this->authorizationChecker->isGranted(Page::SHOW, $entity)
                && $def::hasCapability(Page::SHOW)) {
                $path = $this->router->generate($def::getRouteName(Page::SHOW), [
                    'id' => $entity->getId(),
                ]);

                $granted = false;
                if (! $this->authorizationChecker->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {
                    // if the user is not authenticated, we link it because a
                    // login form is shown if the user tries to access the resource
                    // otherwise there would happens a InsufficientAuthenticationException
                    $fakeRequest = Request::create($path, 'GET');
                    [$roles, $channel] = (new AccessMap())->getPatterns($fakeRequest);
                    foreach ($roles as $role) {
                        $granted = $granted || $this->authorizationChecker->isGranted($role);
                    }
                } else {
                    $granted = true;
                }

                if ($granted) {
                    return sprintf('<a href="%s">%s</a>', $path, $value);
                }
            }
        }

        return $value;
    }

    /**
     * @param array $params
     *
     * @return string
     */
    public function getPath(Page $route, $params = [])
    {
        if ($this->definition->hasCapability($route)) {<commit_msg>removed unused link it<commit_after><?php

declare(strict_types=1);

namespace whatwedo\CrudBundle\View;

use Doctrine\Common\Annotations\Reader;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormRegistryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Http\AccessMap;
use Twig\Environment;
use whatwedo\CoreBundle\Action\Action;
use whatwedo\CrudBundle\Block\Block;
use whatwedo\CrudBundle\Collection\BlockCollection;
use whatwedo\CrudBundle\Content\AbstractContent;
use whatwedo\CrudBundle\Content\Content;
use whatwedo\CrudBundle\Definition\AbstractDefinition;
use whatwedo\CrudBundle\Definition\DefinitionInterface;
use whatwedo\CrudBundle\Enum\Page;
use whatwedo\CrudBundle\Form\Type\EntityAjaxType;
use whatwedo\CrudBundle\Form\Type\EntityHiddenType;
use whatwedo\CrudBundle\Form\Type\EntityPreselectType;
use whatwedo\CrudBundle\Manager\DefinitionManager;

class DefinitionView
{
    protected ?object $data = null;

    protected Page $route;

    protected ?FormInterface $form = null;

    protected DefinitionInterface $definition;

    protected \ReflectionObject $reflectionObject;

    public function __construct(
        protected DefinitionManager $definitionManager,
        protected Environment $templating,
        protected FormRegistryInterface $formRegistry,
        protected FormFactoryInterface $formFactory,
        protected RouterInterface $router,
        protected RequestStack $requestStack,
        protected AuthorizationCheckerInterface $authorizationChecker,
        protected Reader $annotationReader,
    ) {
    }

    public function create(DefinitionInterface $definition, Page $route, ?object $data = null): self
    {
        $view = clone $this;
        $view->setDefinition($definition);
        $view->setData($data);
        $view->setRoute($route);

        return $view;
    }

    public function setDefinition(DefinitionInterface $definition)
    {
        $this->definition = $definition;
    }

    public function setData($data)
    {
        $this->data = $data;
    }

    /**
     * @return object
     */
    public function getData()
    {
        return $this->data;
    }

    public function getRoute(): Page
    {
        return $this->route;
    }

    public function setRoute(Page $route): self
    {
        $this->route = $route;

        return $this;
    }

    public function getActions(): iterable
    {
        return array_filter(
            $this->definition->getActions(),
            fn (Action $action) => in_array($this->route, $action->getOption('visibility'), true)
        );
    }

    /**
     * @return BlockCollection|Block[]
     */
    public function getBlocks(?Page $page = null)
    {
        return $page
            ? $this->definition->getBuilder()->getBlocks()->filterVisibility($page)
            : $this->definition->getBuilder()->getBlocks();
    }

    public function render(): string
    {
        return $this->templating->render($this->getTemplatePath('_boxes/' . $this->getRoute() . '.html.twig'), [
            'data' => $this->data,
            'helper' => $this,
        ]);
    }

    /**
     * @param array $params
     *
     * @return string
     */
    public function getPath(Page $route, $params = [])
    {
        if ($this->definition->hasCapability($route)) {
{'fraction_non_alphanumeric': 0.11606638816755109, 'fraction_numerical': 0.00022581009371118888, 'mean_word_length': 2.7438715131022824, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 78, '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': '4710450', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2393, 'n_words': 487}