Skip to content

Commit

Permalink
Add Symfony 6 support and drop unsupported PHP and Symfony versions (#29
Browse files Browse the repository at this point in the history
)
  • Loading branch information
javer authored Dec 14, 2021
1 parent 87e51c8 commit 8a87b61
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 81 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
/composer.lock
/vendor
11 changes: 3 additions & 8 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@ class Configuration implements ConfigurationInterface
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('isometriks_spam');
if (\method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
// BC layer for symfony/config 4.1 and older
$rootNode = $treeBuilder->root('isometriks_spam');
}

$rootNode
$treeBuilder
->getRootNode()
->children()
->arrayNode('timed')
->canBeDisabled()
Expand Down
6 changes: 3 additions & 3 deletions DependencyInjection/IsometriksSpamExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class IsometriksSpamExtension extends Extension
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
Expand All @@ -27,7 +27,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->processHoneypotConfig($config['honeypot'], $container, $loader);
}

private function processTimedConfig(array $config, ContainerBuilder $container, XmlFileLoader $loader)
private function processTimedConfig(array $config, ContainerBuilder $container, XmlFileLoader $loader): void
{
if (!$this->isConfigEnabled($container, $config)) {
return;
Expand All @@ -44,7 +44,7 @@ private function processTimedConfig(array $config, ContainerBuilder $container,
));
}

private function processHoneypotConfig(array $config, ContainerBuilder $container, XmlFileLoader $loader)
private function processHoneypotConfig(array $config, ContainerBuilder $container, XmlFileLoader $loader): void
{
if (!$this->isConfigEnabled($container, $config)) {
return;
Expand Down
24 changes: 13 additions & 11 deletions Form/Extension/Spam/EventListener/HoneypotValidationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@

class HoneypotValidationListener implements EventSubscriberInterface
{
private $translator;
private $translationDomain;
private $fieldName;
private $errorMessage;

public function __construct(TranslatorInterface $translator = null,
$translationDomain,
$fieldName,
$errorMessage)
private ?TranslatorInterface $translator;
private string $translationDomain;
private string $fieldName;
private string $errorMessage;

public function __construct(
?TranslatorInterface $translator,
string $translationDomain,
string $fieldName,
string $errorMessage
)
{
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->fieldName = $fieldName;
$this->errorMessage = $errorMessage;
}

public function preSubmit(FormEvent $event)
public function preSubmit(FormEvent $event): void
{
$form = $event->getForm();
$data = $event->getData();
Expand All @@ -50,7 +52,7 @@ public function preSubmit(FormEvent $event)
$event->setData($data);
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return array(
FormEvents::PRE_SUBMIT => 'preSubmit',
Expand Down
28 changes: 15 additions & 13 deletions Form/Extension/Spam/EventListener/TimedSpamValidationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@

class TimedSpamValidationListener implements EventSubscriberInterface
{
private $timeProvider;
private $errorMessage;
private $translator;
private $translationDomain;
private $options;

public function __construct(TimedSpamProviderInterface $timeProvider,
TranslatorInterface $translator = null,
$translationDomain,
$errorMessage,
$options)
private TimedSpamProviderInterface $timeProvider;
private string $errorMessage;
private ?TranslatorInterface $translator;
private string $translationDomain;
private array $options;

public function __construct(
TimedSpamProviderInterface $timeProvider,
?TranslatorInterface $translator,
string $translationDomain,
string $errorMessage,
array $options
)
{
$this->timeProvider = $timeProvider;
$this->translator = $translator;
Expand All @@ -30,7 +32,7 @@ public function __construct(TimedSpamProviderInterface $timeProvider,
$this->options = $options;
}

public function preSubmit(FormEvent $event)
public function preSubmit(FormEvent $event): void
{
$form = $event->getForm();

Expand All @@ -52,7 +54,7 @@ public function preSubmit(FormEvent $event)
$this->timeProvider->removeFormTime($form->getName());
}

public static function getSubscribedEvents()
public static function getSubscribedEvents(): array
{
return array(
FormEvents::PRE_SUBMIT => 'preSubmit',
Expand Down
39 changes: 23 additions & 16 deletions Form/Extension/Spam/Provider/SessionTimedSpamProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@

namespace Isometriks\Bundle\SpamBundle\Form\Extension\Spam\Provider;

use Symfony\Component\HttpFoundation\Session\Session;
use DateTime;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class SessionTimedSpamProvider implements TimedSpamProviderInterface
{
protected $session;
private RequestStack $requestStack;

public function __construct(Session $session)
public function __construct(RequestStack $requestStack)
{
$this->session = $session;
$this->requestStack = $requestStack;
}

public function generateFormTime($name)
public function generateFormTime(string $name): DateTime
{
$startTime = new \DateTime();
$startTime = new DateTime();
$key = $this->getSessionKey($name);

$this->session->set($key, $startTime);
$this->getSession()->set($key, $startTime);

return $startTime;
}

public function isFormTimeValid($name, array $options)
public function isFormTimeValid(string $name, array $options): bool
{
$valid = true;
$startTime = $this->getFormTime($name);
Expand All @@ -35,7 +37,7 @@ public function isFormTimeValid($name, array $options)
return false;
}

$currentTime = new \DateTime();
$currentTime = new DateTime();

/*
* Check against a minimum time
Expand All @@ -60,33 +62,38 @@ public function isFormTimeValid($name, array $options)
return $valid;
}

public function hasFormTime($name)
public function hasFormTime(string $name): bool
{
$key = $this->getSessionKey($name);

return $this->session->has($key);
return $this->getSession()->has($key);
}

public function getFormTime($name)
public function getFormTime(string $name)
{
$key = $this->getSessionKey($name);

if ($this->hasFormTime($name)) {
return $this->session->get($key);
return $this->getSession()->get($key);
}

return false;
}

public function removeFormTime($name)
public function removeFormTime(string $name): void
{
$key = $this->getSessionKey($name);

$this->session->remove($key);
$this->getSession()->remove($key);
}

protected function getSessionKey($name)
protected function getSessionKey(string $name): string
{
return 'timedSpam/'.$name;
}

protected function getSession(): SessionInterface
{
return $this->requestStack->getCurrentRequest()->getSession();
}
}
20 changes: 13 additions & 7 deletions Form/Extension/Spam/Provider/TimedSpamProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@

namespace Isometriks\Bundle\SpamBundle\Form\Extension\Spam\Provider;

use DateTime;

interface TimedSpamProviderInterface
{
/**
* @return \DateTime $startTime
* Generate form time.
*
* @param string $name
*
* @return DateTime
*/
public function generateFormTime($name);
public function generateFormTime(string $name): DateTime;

/**
* Check if form has time.
*
* @param string $name
*/
public function hasFormTime($name);
public function hasFormTime(string $name): bool;

/**
* Gets the form time for specified form.
*
* @param $name Name of form to get
* @param string $name Name of form to get
*/
public function getFormTime($name);
public function getFormTime(string $name);

/**
* Removes a form time name.
*
* @param string $name
*/
public function removeFormTime($name);
public function removeFormTime(string $name): void;

/**
* Check if form time is valid.
Expand All @@ -38,5 +44,5 @@ public function removeFormTime($name);
*
* @return bool $valid
*/
public function isFormTimeValid($name, array $options);
public function isFormTimeValid(string $name, array $options): bool;
}
22 changes: 12 additions & 10 deletions Form/Extension/Spam/Type/FormTypeHoneypotExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@

class FormTypeHoneypotExtension extends AbstractTypeExtension
{
private $translator;
private $translationDomain;
private $defaults;
private ?TranslatorInterface $translator;
private string $translationDomain;
private array $defaults;

public function __construct(TranslatorInterface $translator = null,
$translationDomain,
array $defaults)
public function __construct(
?TranslatorInterface $translator,
string $translationDomain,
array $defaults
)
{
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->defaults = $defaults;
}

public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if (!$options['honeypot']) {
return;
Expand All @@ -43,7 +45,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
));
}

public function finishView(FormView $view, FormInterface $form, array $options)
public function finishView(FormView $view, FormInterface $form, array $options): void
{
if ($options['honeypot'] && !$view->parent && $options['compound']) {
if ($form->has($options['honeypot_field'])) {
Expand Down Expand Up @@ -73,7 +75,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
}
}

public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(array(
'honeypot' => $this->defaults['global'],
Expand All @@ -92,7 +94,7 @@ public static function getExtendedTypes(): iterable
return [FormType::class];
}

public function getExtendedType()
public function getExtendedType(): string
{
return FormType::class;
}
Expand Down
Loading

0 comments on commit 8a87b61

Please sign in to comment.