<commit_before><?php
namespace App\Form;

use App\Acl;
use App\Entity;
use App\Http\ServerRequest;
use App\Radio\Configuration;
use App\Settings;
use App\Sync\Task\Media;
use Azura\Config;
use DeepCopy;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManager;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class StationCloneForm extends StationForm
{
    protected Configuration $configuration;

    protected Media $media_sync;

    public function __construct(
        EntityManager $em,
        Serializer $serializer,
        ValidatorInterface $validator,
        Entity\Repository\StationRepository $station_repo,
        Acl $acl,
        Configuration $configuration,
        Media $media_sync,
        Config $config
    ) {
        parent::__construct($em, $serializer, $validator, $station_repo, $acl, $config);

        $form_config = $config->get('forms/station_clone');
        $this->configure($form_config);

        $this->configuration = $configuration;
        $this->media_sync = $media_sync;
    }

    public function process(ServerRequest $request, $record = null)
    {
        if (!$record instanceof Entity\Station) {
            throw new InvalidArgumentException('Record must be a station.');
        }

        $this->populate([
            'name' => $record->getName() . ' - Copy',
            'description' => $record->getDescription(),
        ]);

        if ('POST' === $request->getMethod() && $this->isValid($request->getParsedBody())) {
            $data = $this->getValues();

            $copier = new DeepCopy\DeepCopy;
            $copier->addFilter(new DeepCopy\Filter\Doctrine\DoctrineProxyFilter,
                new DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher);
            $copier->addFilter(new DeepCopy\Filter\KeepFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\StationMedia::class, 'song'));
            $copier->addFilter(new DeepCopy\Filter\KeepFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\RolePermission::class, 'role'));
            $copier->addFilter(new DeepCopy\Filter\KeepFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\StationMediaCustomField::class, 'field'));
            $copier->addFilter(
                new DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\Station::class, 'history')
            );
            $copier->addFilter(
                new DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\StationPlaylist::class, 'media_items')
            );

            // Unset some properties across all copied record types.
            $global_unsets = [
                'id',
                'station_id',
                'media_id',
                'playlist_id',
                'field_id',
            ];
            foreach ($global_unsets as $prop) {
                $copier->addFilter(new DeepCopy\Filter\SetNullFilter, new DeepCopy\Matcher\PropertyNameMatcher($prop));
            }

            // Unset some values only on Station entities.
            $unset_values = [
                'short_name',
                'radio_base_dir',
                'adapter_api_key',
                'nowplaying',
                'nowplaying_timestamp',
                'current_streamer_id',<commit_msg>#2276 -- Fix cloning with existing SFTP users.<commit_after><?php
namespace App\Form;

use App\Acl;
use App\Entity;
use App\Http\ServerRequest;
use App\Radio\Configuration;
use App\Settings;
use App\Sync\Task\Media;
use Azura\Config;
use DeepCopy;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManager;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Validator\Validator\ValidatorInterface;

class StationCloneForm extends StationForm
{
    protected Configuration $configuration;

    protected Media $media_sync;

    public function __construct(
        EntityManager $em,
        Serializer $serializer,
        ValidatorInterface $validator,
        Entity\Repository\StationRepository $station_repo,
        Acl $acl,
        Configuration $configuration,
        Media $media_sync,
        Config $config
    ) {
        parent::__construct($em, $serializer, $validator, $station_repo, $acl, $config);

        $form_config = $config->get('forms/station_clone');
        $this->configure($form_config);

        $this->configuration = $configuration;
        $this->media_sync = $media_sync;
    }

    public function process(ServerRequest $request, $record = null)
    {
        if (!$record instanceof Entity\Station) {
            throw new InvalidArgumentException('Record must be a station.');
        }

        $this->populate([
            'name' => $record->getName() . ' - Copy',
            'description' => $record->getDescription(),
        ]);

        if ('POST' === $request->getMethod() && $this->isValid($request->getParsedBody())) {
            $data = $this->getValues();

            $copier = new DeepCopy\DeepCopy;
            $copier->addFilter(new DeepCopy\Filter\Doctrine\DoctrineProxyFilter,
                new DeepCopy\Matcher\Doctrine\DoctrineProxyMatcher);
            $copier->addFilter(new DeepCopy\Filter\KeepFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\StationMedia::class, 'song'));
            $copier->addFilter(new DeepCopy\Filter\KeepFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\RolePermission::class, 'role'));
            $copier->addFilter(new DeepCopy\Filter\KeepFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\StationMediaCustomField::class, 'field'));

            $copier->addFilter(
                new DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\Station::class, 'history')
            );
            $copier->addFilter(
                new DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\Station::class, 'sftpUsers')
            );

            $copier->addFilter(
                new DeepCopy\Filter\Doctrine\DoctrineEmptyCollectionFilter,
                new DeepCopy\Matcher\PropertyMatcher(Entity\StationPlaylist::class, 'media_items')
            );

            // Unset some properties across all copied record types.
            $global_unsets = [
                'id',
                'station_id',
                'media_id',
                'playlist_id',
                'field_id',
            ];
            foreach ($global_unsets as $prop) {
                $copier->addFilter(new DeepCopy\Filter\SetNullFilter, new DeepCopy\Matcher\PropertyNameMatcher($prop));
            }

            // Unset some values only on Station entities.
            $unset_values = [
                'short_name',
                'radio_base_dir',
                'adapter_api_key',
                'nowplaying',
                'nowplaying_timestamp',
                'current_streamer_id',
{'fraction_non_alphanumeric': 0.10882474796298854, 'fraction_numerical': 0.0005524098881369977, 'mean_word_length': 2.3450346420323327, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, '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': '5369939', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1848, 'n_words': 355}