chore (Symfony) use Symfony 5 + strict typing
Update Symfony version to 5 Use strict typing
This commit is contained in:
@ -1,10 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gregwar\CaptchaBundle\Generator;
|
||||
|
||||
use Gregwar\Captcha\CaptchaBuilder;
|
||||
use Gregwar\Captcha\PhraseBuilder;
|
||||
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
|
||||
use Gregwar\Captcha\CaptchaBuilderInterface;
|
||||
@ -18,24 +19,16 @@ use Gregwar\Captcha\PhraseBuilderInterface;
|
||||
*/
|
||||
class CaptchaGenerator
|
||||
{
|
||||
/**
|
||||
* @var RouterInterface
|
||||
*/
|
||||
/** @var RouterInterface */
|
||||
protected $router;
|
||||
|
||||
/**
|
||||
* @var CaptchaBuilder
|
||||
*/
|
||||
/** @var CaptchaBuilder */
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* @var PhraseBuilder
|
||||
*/
|
||||
/** @var PhraseBuilder */
|
||||
protected $phraseBuilder;
|
||||
|
||||
/**
|
||||
* @var ImageFileHandler
|
||||
*/
|
||||
/** @var ImageFileHandler */
|
||||
protected $imageFileHandler;
|
||||
|
||||
/**
|
||||
@ -56,14 +49,7 @@ class CaptchaGenerator
|
||||
$this->imageFileHandler = $imageFileHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the captcha URL, stream, or filename that will go in the image's src attribute
|
||||
*
|
||||
* @param array $options
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCaptchaCode(array &$options)
|
||||
public function getCaptchaCode(array &$options): string
|
||||
{
|
||||
$this->builder->setPhrase($this->getPhrase($options));
|
||||
|
||||
@ -83,20 +69,12 @@ class CaptchaGenerator
|
||||
return 'data:image/jpeg;base64,' . base64_encode($this->generate($options));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the phrase to the builder
|
||||
*/
|
||||
public function setPhrase($phrase)
|
||||
public function setPhrase(string $phrase): void
|
||||
{
|
||||
$this->builder->setPhrase($phrase);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate(array &$options)
|
||||
public function generate(array &$options): string
|
||||
{
|
||||
$this->builder->setDistortion($options['distortion']);
|
||||
|
||||
@ -149,12 +127,7 @@ class CaptchaGenerator
|
||||
return $this->imageFileHandler->saveAsFile($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPhrase(array &$options)
|
||||
public function getPhrase(array &$options): string
|
||||
{
|
||||
// Get the phrase that we'll use for this image
|
||||
if ($options['keep_value'] && isset($options['phrase'])) {
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Gregwar\CaptchaBundle\Generator;
|
||||
|
||||
use Symfony\Component\Finder\Finder;
|
||||
@ -37,27 +39,20 @@ class ImageFileHandler
|
||||
protected $expiration;
|
||||
|
||||
/**
|
||||
* @param $imageFolder
|
||||
* @param $webPath
|
||||
* @param $gcFreq
|
||||
* @param $expiration
|
||||
* @param string $imageFolder
|
||||
* @param string $webPath
|
||||
* @param string $gcFreq
|
||||
* @param string $expiration
|
||||
*/
|
||||
public function __construct($imageFolder, $webPath, $gcFreq, $expiration)
|
||||
public function __construct(string $imageFolder, string $webPath, string $gcFreq, string $expiration)
|
||||
{
|
||||
$this->imageFolder = $imageFolder;
|
||||
$this->webPath = $webPath;
|
||||
$this->gcFreq = $gcFreq;
|
||||
$this->expiration = $expiration;
|
||||
$this->imageFolder = $imageFolder;
|
||||
$this->webPath= $webPath;
|
||||
$this->gcFreq = $gcFreq;
|
||||
$this->expiration = $expiration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the provided image content as a file
|
||||
*
|
||||
* @param string $contents
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function saveAsFile($contents)
|
||||
public function saveAsFile($contents):string
|
||||
{
|
||||
$this->createFolderIfMissing();
|
||||
|
||||
@ -68,12 +63,7 @@ class ImageFileHandler
|
||||
return '/' . $this->imageFolder . '/' . $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Randomly runs garbage collection on the image directory
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function collectGarbage()
|
||||
public function collectGarbage(): bool
|
||||
{
|
||||
if (!mt_rand(1, $this->gcFreq) == 1) {
|
||||
return false;
|
||||
@ -93,10 +83,7 @@ class ImageFileHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the folder if it doesn't exist
|
||||
*/
|
||||
protected function createFolderIfMissing()
|
||||
protected function createFolderIfMissing(): void
|
||||
{
|
||||
if (!file_exists($this->webPath . '/' . $this->imageFolder)) {
|
||||
mkdir($this->webPath . '/' . $this->imageFolder, 0755);
|
||||
|
Reference in New Issue
Block a user