diff options
author | Johannes Schmitt <schmittjoh@gmail.com> | 2011-02-14 17:47:56 +0100 |
---|---|---|
committer | Fabien Potencier <fabien.potencier@gmail.com> | 2011-02-14 20:55:06 +0100 |
commit | 1accc593337aeaf814c203165e5b50521a9a3d22 (patch) | |
tree | ae239093fadc611791d4609152391a2c09518aa5 | |
parent | 2f8cd3cca342f5f4b6a01477c0123bf12592275f (diff) | |
download | symfony-security-1accc593337aeaf814c203165e5b50521a9a3d22.zip symfony-security-1accc593337aeaf814c203165e5b50521a9a3d22.tar.gz symfony-security-1accc593337aeaf814c203165e5b50521a9a3d22.tar.bz2 |
[Security] simplified encoder factory implementation
-rw-r--r-- | Core/Encoder/EncoderFactory.php | 50 |
1 files changed, 21 insertions, 29 deletions
diff --git a/Core/Encoder/EncoderFactory.php b/Core/Encoder/EncoderFactory.php index 0f218fe..bc6df06 100644 --- a/Core/Encoder/EncoderFactory.php +++ b/Core/Encoder/EncoderFactory.php @@ -21,12 +21,10 @@ use Symfony\Component\Security\Core\User\AccountInterface; class EncoderFactory implements EncoderFactoryInterface { protected $encoders; - protected $encoderMap; - public function __construct(array $encoderMap) + public function __construct(array $encoders) { - $this->encoders = array(); - $this->encoderMap = $encoderMap; + $this->encoders = $encoders; } /** @@ -35,43 +33,37 @@ class EncoderFactory implements EncoderFactoryInterface public function getEncoder(AccountInterface $account) { foreach ($this->encoders as $class => $encoder) { - if ($account instanceof $class) { - return $encoder; + if (!$account instanceof $class) { + continue; } - } - return $this->createEncoder($account); - } + if (!$encoder instanceof PasswordEncoderInterface) { + return $this->encoders[$class] = $this->createEncoder($encoder); + } - /** - * Adds an encoder instance to the factory - * - * @param string $class - * @param PasswordEncoderInterface $encoder - * @return void - */ - public function addEncoder($class, PasswordEncoderInterface $encoder) - { - $this->encoders[$class] = $encoder; + return $this->encoders[$class]; + } + + throw new \RuntimeException(sprintf('No encoder has been configured for account "%s".', get_class($account))); } /** * Creates the actual encoder instance * - * @param AccountInterface $account + * @param array $config * @return PasswordEncoderInterface */ - protected function createEncoder($account) + protected function createEncoder(array $config) { - foreach ($this->encoderMap as $class => $config) { - if ($account instanceof $class) { - $reflection = new \ReflectionClass($config['class']); - $this->encoders[$class] = $reflection->newInstanceArgs($config['arguments']); - - return $this->encoders[$class]; - } + if (!isset($config['class'])) { + throw new \InvalidArgumentException(sprintf('"class" must be set in %s.', json_encode($config))); + } + if (!isset($config['arguments'])) { + throw new \InvalidArgumentException(sprintf('"arguments" must be set in %s.', json_encode($config))); } - throw new \InvalidArgumentException(sprintf('No encoder has been configured for account "%s".', get_class($account))); + $reflection = new \ReflectionClass($config['class']); + + return $reflection->newInstanceArgs($config['arguments']); } }
\ No newline at end of file |