From 3794a12e800125b7f8f1f220bbd9c80f96666d3c Mon Sep 17 00:00:00 2001 From: Gregwar Date: Tue, 4 Dec 2012 11:51:47 +0100 Subject: [PATCH] [Generator] Renaming, and separating the PhraseBuilder --- .../{ImageBuilder.php => CaptchaBuilder.php} | 2 +- Generator/CaptchaGenerator.php | 20 +++++++------- Generator/PhraseBuilder.php | 27 +++++++++++++++++++ Resources/config/services.yml | 10 ++++--- 4 files changed, 45 insertions(+), 14 deletions(-) rename Generator/{ImageBuilder.php => CaptchaBuilder.php} (99%) create mode 100644 Generator/PhraseBuilder.php diff --git a/Generator/ImageBuilder.php b/Generator/CaptchaBuilder.php similarity index 99% rename from Generator/ImageBuilder.php rename to Generator/CaptchaBuilder.php index f94006d..f016607 100644 --- a/Generator/ImageBuilder.php +++ b/Generator/CaptchaBuilder.php @@ -11,7 +11,7 @@ use Symfony\Component\Finder\Finder; * @author Gregwar * @author Jeremy Livingston */ -class ImageBuilder +class CaptchaBuilder { /** * @var array diff --git a/Generator/CaptchaGenerator.php b/Generator/CaptchaGenerator.php index 5801be6..8b98328 100644 --- a/Generator/CaptchaGenerator.php +++ b/Generator/CaptchaGenerator.php @@ -31,10 +31,15 @@ class CaptchaGenerator protected $router; /** - * @var ImageBuilder + * @var CaptchaBuilder */ protected $builder; + /** + * @var PhraseBuilder + */ + protected $phraseBuilder; + /** * @var ImageFileHandler */ @@ -43,15 +48,16 @@ class CaptchaGenerator /** * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session * @param \Symfony\Component\Routing\RouterInterface $router - * @param ImageBuilder $builder + * @param CaptchaBuilder $builder * @param ImageFileHandler $imageFileHandler * @param string $whitelistKey */ - public function __construct(SessionInterface $session, RouterInterface $router, ImageBuilder $builder, ImageFileHandler $imageFileHandler, $whitelistKey) + public function __construct(SessionInterface $session, RouterInterface $router, CaptchaBuilder $builder, PhraseBuilder $phraseBuilder, ImageFileHandler $imageFileHandler, $whitelistKey) { $this->session = $session; $this->router = $router; $this->builder = $builder; + $this->phraseBuilder = $phraseBuilder; $this->imageFileHandler = $imageFileHandler; $this->whitelistKey = $whitelistKey; } @@ -131,13 +137,7 @@ class CaptchaGenerator return $this->session->get($key); } - $phrase = ''; - $chars = str_split($options['charset']); - - for ($i = 0; $i < $options['length']; $i++) { - $phrase .= $chars[array_rand($chars)]; - } - + $phrase = $this->phraseBuilder->build($options['length'], $options['charset']); $this->session->set($key, $phrase); return $phrase; diff --git a/Generator/PhraseBuilder.php b/Generator/PhraseBuilder.php new file mode 100644 index 0000000..d7418f2 --- /dev/null +++ b/Generator/PhraseBuilder.php @@ -0,0 +1,27 @@ + + */ +class PhraseBuilder +{ + public function build($length = 5, $charset = 'abcdefghijklmnopqrstuvwxyz0123456789') + { + $phrase = ''; + $chars = str_split($charset); + + for ($i = 0; $i < $length; $i++) { + $phrase .= $chars[array_rand($chars)]; + } + + return $phrase; + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index ea44ce4..eced814 100755 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -13,7 +13,8 @@ services: arguments: - @session - @router - - @gregwar_captcha.image_builder + - @gregwar_captcha.captcha_builder + - @gregwar_captcha.phrase_builder - @gregwar_captcha.image_file_handler - %gregwar_captcha.config.whitelist_key% @@ -25,5 +26,8 @@ services: - %gregwar_captcha.config.gc_freq% - %gregwar_captcha.config.expiration% - gregwar_captcha.image_builder: - class: Gregwar\CaptchaBundle\Generator\ImageBuilder + gregwar_captcha.captcha_builder: + class: Gregwar\CaptchaBundle\Generator\CaptchaBuilder + + gregwar_captcha.phrase_builder: + class: Gregwar\CaptchaBundle\Generator\PhraseBuilder