[Generator] Renaming, and separating the PhraseBuilder

This commit is contained in:
Gregwar 2012-12-04 11:51:47 +01:00
parent 516046a6b5
commit 3794a12e80
4 changed files with 45 additions and 14 deletions

View File

@ -11,7 +11,7 @@ use Symfony\Component\Finder\Finder;
* @author Gregwar <g.passault@gmail.com> * @author Gregwar <g.passault@gmail.com>
* @author Jeremy Livingston <jeremy.j.livingston@gmail.com> * @author Jeremy Livingston <jeremy.j.livingston@gmail.com>
*/ */
class ImageBuilder class CaptchaBuilder
{ {
/** /**
* @var array * @var array

View File

@ -31,10 +31,15 @@ class CaptchaGenerator
protected $router; protected $router;
/** /**
* @var ImageBuilder * @var CaptchaBuilder
*/ */
protected $builder; protected $builder;
/**
* @var PhraseBuilder
*/
protected $phraseBuilder;
/** /**
* @var ImageFileHandler * @var ImageFileHandler
*/ */
@ -43,15 +48,16 @@ class CaptchaGenerator
/** /**
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @param \Symfony\Component\Routing\RouterInterface $router * @param \Symfony\Component\Routing\RouterInterface $router
* @param ImageBuilder $builder * @param CaptchaBuilder $builder
* @param ImageFileHandler $imageFileHandler * @param ImageFileHandler $imageFileHandler
* @param string $whitelistKey * @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->session = $session;
$this->router = $router; $this->router = $router;
$this->builder = $builder; $this->builder = $builder;
$this->phraseBuilder = $phraseBuilder;
$this->imageFileHandler = $imageFileHandler; $this->imageFileHandler = $imageFileHandler;
$this->whitelistKey = $whitelistKey; $this->whitelistKey = $whitelistKey;
} }
@ -131,13 +137,7 @@ class CaptchaGenerator
return $this->session->get($key); return $this->session->get($key);
} }
$phrase = ''; $phrase = $this->phraseBuilder->build($options['length'], $options['charset']);
$chars = str_split($options['charset']);
for ($i = 0; $i < $options['length']; $i++) {
$phrase .= $chars[array_rand($chars)];
}
$this->session->set($key, $phrase); $this->session->set($key, $phrase);
return $phrase; return $phrase;

View File

@ -0,0 +1,27 @@
<?php
namespace Gregwar\CaptchaBundle\Generator;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
/**
* Generates random phrase
*
* @author Gregwar <g.passault@gmail.com>
*/
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;
}
}

View File

@ -13,7 +13,8 @@ services:
arguments: arguments:
- @session - @session
- @router - @router
- @gregwar_captcha.image_builder - @gregwar_captcha.captcha_builder
- @gregwar_captcha.phrase_builder
- @gregwar_captcha.image_file_handler - @gregwar_captcha.image_file_handler
- %gregwar_captcha.config.whitelist_key% - %gregwar_captcha.config.whitelist_key%
@ -25,5 +26,8 @@ services:
- %gregwar_captcha.config.gc_freq% - %gregwar_captcha.config.gc_freq%
- %gregwar_captcha.config.expiration% - %gregwar_captcha.config.expiration%
gregwar_captcha.image_builder: gregwar_captcha.captcha_builder:
class: Gregwar\CaptchaBundle\Generator\ImageBuilder class: Gregwar\CaptchaBundle\Generator\CaptchaBuilder
gregwar_captcha.phrase_builder:
class: Gregwar\CaptchaBundle\Generator\PhraseBuilder