Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow registration of custom transformers and pass whole event to get… #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Warxcell
Copy link

I'm making a command inside existing symfony project which creates anonymized dump and sends it over email. I needed to have custom transformers. Also one of them needed to know which table it anonymize because of password hashing. (which could be different per table). I'm using Symfony and ORM - so I need to know table - from table I finds the Entity class, and from entity class I find the correct encoder.

<?php

namespace AppBundle;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use machbarmacher\GdprDump\ColumnTransformer\ColumnTransformer;
use machbarmacher\GdprDump\ColumnTransformer\ColumnTransformEvent;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;

class SymfonyPasswordColumnTransformer extends ColumnTransformer
{
    /** @var string */
    private $plainPassword;

    /** @var EncoderFactoryInterface */
    private $encoderFactory;

    /** @var EntityManagerInterface */
    private $entityManager;

    public function __construct(
        string $plainPassword,
        EncoderFactoryInterface $encoderFactory,
        EntityManagerInterface $entityManager
    ) {
        $this->plainPassword = $plainPassword;
        $this->encoderFactory = $encoderFactory;
        $this->entityManager = $entityManager;
    }

    public function getValue(ColumnTransformEvent $event)
    {
        $table = $event->getTable();

        foreach ($this->entityManager->getMetadataFactory()->getAllMetadata() as $metadata) {
            /** @var $metadata ClassMetadata */
            if ($table === $metadata->getTableName()) {
                $encoder = $this->encoderFactory->getEncoder($metadata->getReflectionClass()->getName());

                return $encoder->encodePassword($this->plainPassword, null);
            }
        }
        throw new \LogicException('Table '.$table.' not found in supported entities');
    }

    protected function getSupportedFormatters()
    {
        return ['symfony_password_transformer'];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant