[CaptchaType] Cleaning options management (fixes #15)

This commit is contained in:
Gregwar 2012-02-08 19:53:22 +01:00
parent f9c57e6970
commit 0453ae2e0e
1 changed files with 25 additions and 120 deletions

View File

@ -23,83 +23,10 @@ use Gregwar\CaptchaBundle\DataTransformer\EmptyTransformer;
class CaptchaType extends AbstractType
{
/**
* The image width
* @var integer
* Options
* @var array
*/
protected $width;
/**
* The image height
* @var integer
*/
protected $height;
/**
* The code length
* @var integer
*/
protected $length;
/**
* Generate image or data
* @var boolean
*/
protected $asFile;
/**
* Keep value between two requests ?
* @var boolean
*/
protected $keepValue;
/**
* Charset used
* @var string
*/
protected $charset;
/**
* Folder to save captcha images in,
* relative to public web folder
* @var string
*/
protected $imageFolder;
/**
* Public web folder
* @var string
*/
protected $webPath;
/**
* Frequence of garbage collection in fractions of 1
* @var int
*/
protected $gcFreq;
/**
* Captcha font
* @var string
*/
protected $font;
/**
* Captcha quality
* @var int
*/
protected $quality;
/**
* Maximum age of images in minutes
* @var int
*/
protected $expiration;
/**
* The session
* @var Symfony\Component\HttpFoundation\Session
*/
protected $session;
private $options = array();
/**
* Session key
@ -107,23 +34,10 @@ class CaptchaType extends AbstractType
*/
private $key = 'captcha';
public function __construct(Session $session, $config)
{
$this->session = $session;
$this->width = $config['width'];
$this->height = $config['height'];
$this->length = $config['length'];
$this->charset = $config['charset'];
$this->keepValue = $config['keep_value'];
$this->asFile = $config['as_file'];
$this->imageFolder = $config['image_folder'];
$this->webPath = $config['web_path'];
$this->gcFreq = $config['gc_freq'];
$this->expiration = $config['expiration'];
$this->font = $config['font'];
$this->quality = $config['quality'];
$this->options = $config;
}
public function buildForm(FormBuilder $builder, array $options)
@ -139,21 +53,28 @@ class CaptchaType extends AbstractType
{
$fingerprint = null;
if ($this->session->has($this->key.'_fingerprint')) {
if ($this->options['keep_value'] && $this->session->has($this->key.'_fingerprint')) {
$fingerprint = $this->session->get($this->key.'_fingerprint');
}
$generator = new CaptchaGenerator($this->generateCaptchaValue(), $this->imageFolder, $this->webPath, $this->gcFreq, $this->expiration, $this->font, $fingerprint, $this->quality);
$generator = new CaptchaGenerator($this->generateCaptchaValue(),
$this->options['image_folder'],
$this->options['web_path'],
$this->options['gc_freq'],
$this->options['expiration'],
$this->options['font'],
$fingerprint,
$this->options['quality']);
if ($this->asFile) {
$view->set('captcha_code', $generator->getFile($this->width, $this->height));
if ($this->options['as_file']) {
$view->set('captcha_code', $generator->getFile($this->options['width'], $this->options['height']));
} else {
$view->set('captcha_code', $generator->getCode($this->width, $this->height));
$view->set('captcha_code', $generator->getCode($this->options['width'], $this->options['height']));
}
$view->set('captcha_width', $this->width);
$view->set('captcha_height', $this->height);
$view->set('captcha_width', $this->options['width']);
$view->set('captcha_height', $this->options['height']);
if ($this->keepValue) {
if ($this->options['keep_value']) {
$this->session->set($this->key.'_fingerprint', $generator->getFingerprint());
}
@ -162,26 +83,10 @@ class CaptchaType extends AbstractType
public function getDefaultOptions(array $options = array())
{
if (isset($options['width'])) {
$this->width = $options['width'];
}
if (isset($options['height'])) {
$this->height = $options['height'];
}
if (isset($options['length'])) {
$this->length = $options['length'];
}
if (isset($options['as_file'])) {
$this->asFile = $options['as_file'];
}
$this->options = array_replace($this->options, $options);
$this->options['property_path'] = false;
return array(
'width' => $this->width,
'height' => $this->height,
'length' => $this->length,
'as_file' => $this->asFile,
'property_path' => false,
);
return $this->options;
}
public function getParent(array $options)
@ -196,11 +101,11 @@ class CaptchaType extends AbstractType
private function generateCaptchaValue()
{
if (!$this->keepValue || !$this->session->has($this->key)) {
if (!$this->options['keep_value'] || !$this->session->has($this->key)) {
$value = '';
$chars = str_split($this->charset);
$chars = str_split($this->options['charset']);
for ($i=0; $i<$this->length; $i++) {
for ($i=0; $i<$this->options['length']; $i++) {
$value.= $chars[array_rand($chars)];
}