<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\ClassificationBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\ClassificationBundle\Model\ContextAwareInterface;
use Sonata\ClassificationBundle\Model\ContextManagerInterface;

/**
 * @phpstan-template T of ContextAwareInterface
 * @phpstan-extends AbstractAdmin<T>
 */
abstract class ContextAwareAdmin extends AbstractAdmin
{
    protected ContextManagerInterface $contextManager;

    /**
     * NEXT_MAJOR: Change signature to (ContextManagerInterface).
     *
     * @param ContextManagerInterface|string $contextManager
     *
     * @phpstan-param class-string<T>|null $deprecatedClass
     */
    public function __construct(
        $contextManager,
        ?string $deprecatedClass = null,
        ?string $deprecatedBaseControllerName = null,
        ?ContextManagerInterface $deprecatedContextManager = null
    ) {
        // NEXT_MAJOR: Keep the if part.
        if ($contextManager instanceof ContextManagerInterface) {
            parent::__construct();

            $this->contextManager = $contextManager;
        } else {
            \assert(\is_string($deprecatedClass));
            \assert(\is_string($deprecatedBaseControllerName));
            \assert(null !== $deprecatedContextManager);

            parent::__construct($contextManager, $deprecatedClass, $deprecatedBaseControllerName);

            $this->contextManager = $deprecatedContextManager;
        }
    }

    protected function alterNewInstance(object $object): void
    {
        $contextId = $this->getPersistentParameter('context', '');
        if ('' !== $contextId) {
            $context = $this->contextManager->find($contextId);

            if (null !== $context) {
                $context = $this->contextManager->create();
                $context->setEnabled(true);
                $context->setId($contextId);
                $context->setName($contextId);

                $this->contextManager->save($context);
            }

            $object->setContext($context);
        }
    }

    protected function configurePersistentParameters(): array
    {
        $parameters = [
            'context' => '',
            'hide_context' => $this->hasRequest() ? $this->getRequest()->query->getInt('hide_context', 0) : 0,
        ];

        if ($this->hasSubject()) {
            $context = $this->getSubject()->getContext();

            $parameters['context'] = null !== $context ? $context->getId() : '';<commit_msg>Fix the creation of a new context<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Sonata\ClassificationBundle\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\ClassificationBundle\Model\ContextAwareInterface;
use Sonata\ClassificationBundle\Model\ContextManagerInterface;

/**
 * @phpstan-template T of ContextAwareInterface
 * @phpstan-extends AbstractAdmin<T>
 */
abstract class ContextAwareAdmin extends AbstractAdmin
{
    protected ContextManagerInterface $contextManager;

    /**
     * NEXT_MAJOR: Change signature to (ContextManagerInterface).
     *
     * @param ContextManagerInterface|string $contextManager
     *
     * @phpstan-param class-string<T>|null $deprecatedClass
     */
    public function __construct(
        $contextManager,
        ?string $deprecatedClass = null,
        ?string $deprecatedBaseControllerName = null,
        ?ContextManagerInterface $deprecatedContextManager = null
    ) {
        // NEXT_MAJOR: Keep the if part.
        if ($contextManager instanceof ContextManagerInterface) {
            parent::__construct();

            $this->contextManager = $contextManager;
        } else {
            \assert(\is_string($deprecatedClass));
            \assert(\is_string($deprecatedBaseControllerName));
            \assert(null !== $deprecatedContextManager);

            parent::__construct($contextManager, $deprecatedClass, $deprecatedBaseControllerName);

            $this->contextManager = $deprecatedContextManager;
        }
    }

    protected function alterNewInstance(object $object): void
    {
        $contextId = $this->getPersistentParameter('context', '');
        if ('' !== $contextId) {
            $context = $this->contextManager->find($contextId);

            if (null === $context) {
                $context = $this->contextManager->create();
                $context->setEnabled(true);
                $context->setId($contextId);
                $context->setName($contextId);

                $this->contextManager->save($context);
            }

            $object->setContext($context);
        }
    }

    protected function configurePersistentParameters(): array
    {
        $parameters = [
            'context' => '',
            'hide_context' => $this->hasRequest() ? $this->getRequest()->query->getInt('hide_context', 0) : 0,
        ];

        if ($this->hasSubject()) {
            $context = $this->getSubject()->getContext();

            $parameters['context'] = null !== $context ? $context->getId() : '';
{'fraction_non_alphanumeric': 0.12369651204602661, 'fraction_numerical': 0.0010787486515641855, 'mean_word_length': 2.797269624573379, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 59, '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': '6063270', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1432, 'n_words': 292}