diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77aae3d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea +/composer.lock +/vendor diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index aa0535f..2aeafff 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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() diff --git a/DependencyInjection/IsometriksSpamExtension.php b/DependencyInjection/IsometriksSpamExtension.php index b2ad811..8e9a2a7 100644 --- a/DependencyInjection/IsometriksSpamExtension.php +++ b/DependencyInjection/IsometriksSpamExtension.php @@ -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); @@ -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; @@ -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; diff --git a/Form/Extension/Spam/EventListener/HoneypotValidationListener.php b/Form/Extension/Spam/EventListener/HoneypotValidationListener.php index 47ff627..564d69c 100644 --- a/Form/Extension/Spam/EventListener/HoneypotValidationListener.php +++ b/Form/Extension/Spam/EventListener/HoneypotValidationListener.php @@ -10,15 +10,17 @@ 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; @@ -26,7 +28,7 @@ public function __construct(TranslatorInterface $translator = null, $this->errorMessage = $errorMessage; } - public function preSubmit(FormEvent $event) + public function preSubmit(FormEvent $event): void { $form = $event->getForm(); $data = $event->getData(); @@ -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', diff --git a/Form/Extension/Spam/EventListener/TimedSpamValidationListener.php b/Form/Extension/Spam/EventListener/TimedSpamValidationListener.php index cfcbe2f..d0a7d78 100644 --- a/Form/Extension/Spam/EventListener/TimedSpamValidationListener.php +++ b/Form/Extension/Spam/EventListener/TimedSpamValidationListener.php @@ -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; @@ -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(); @@ -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', diff --git a/Form/Extension/Spam/Provider/SessionTimedSpamProvider.php b/Form/Extension/Spam/Provider/SessionTimedSpamProvider.php index 4b878bf..d2ad6ce 100644 --- a/Form/Extension/Spam/Provider/SessionTimedSpamProvider.php +++ b/Form/Extension/Spam/Provider/SessionTimedSpamProvider.php @@ -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); @@ -35,7 +37,7 @@ public function isFormTimeValid($name, array $options) return false; } - $currentTime = new \DateTime(); + $currentTime = new DateTime(); /* * Check against a minimum time @@ -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(); + } } diff --git a/Form/Extension/Spam/Provider/TimedSpamProviderInterface.php b/Form/Extension/Spam/Provider/TimedSpamProviderInterface.php index 29ab581..1c757c4 100644 --- a/Form/Extension/Spam/Provider/TimedSpamProviderInterface.php +++ b/Form/Extension/Spam/Provider/TimedSpamProviderInterface.php @@ -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. @@ -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; } diff --git a/Form/Extension/Spam/Type/FormTypeHoneypotExtension.php b/Form/Extension/Spam/Type/FormTypeHoneypotExtension.php index c84f582..22dbda4 100644 --- a/Form/Extension/Spam/Type/FormTypeHoneypotExtension.php +++ b/Form/Extension/Spam/Type/FormTypeHoneypotExtension.php @@ -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; @@ -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'])) { @@ -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'], @@ -92,7 +94,7 @@ public static function getExtendedTypes(): iterable return [FormType::class]; } - public function getExtendedType() + public function getExtendedType(): string { return FormType::class; } diff --git a/Form/Extension/Spam/Type/FormTypeTimedSpamExtension.php b/Form/Extension/Spam/Type/FormTypeTimedSpamExtension.php index 309207a..9a62658 100644 --- a/Form/Extension/Spam/Type/FormTypeTimedSpamExtension.php +++ b/Form/Extension/Spam/Type/FormTypeTimedSpamExtension.php @@ -14,15 +14,17 @@ class FormTypeTimedSpamExtension extends AbstractTypeExtension { - private $timeProvider; - private $translator; - private $translationDomain; - private $defaults; + private TimedSpamProviderInterface $timeProvider; + private ?TranslatorInterface $translator; + private string $translationDomain; + private array $defaults; - public function __construct(TimedSpamProviderInterface $timeProvider, - TranslatorInterface $translator = null, - $translationDomain, - array $defaults) + public function __construct( + TimedSpamProviderInterface $timeProvider, + ?TranslatorInterface $translator, + string $translationDomain, + array $defaults + ) { $this->timeProvider = $timeProvider; $this->translator = $translator; @@ -30,7 +32,7 @@ public function __construct(TimedSpamProviderInterface $timeProvider, $this->defaults = $defaults; } - public function buildForm(FormBuilderInterface $builder, array $options) + public function buildForm(FormBuilderInterface $builder, array $options): void { if (!$options['timed_spam']) { return; @@ -51,7 +53,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['timed_spam'] && !$view->parent && $options['compound']) { $this->timeProvider->generateFormTime($form->getName()); @@ -76,7 +78,7 @@ public static function getExtendedTypes(): iterable return [FormType::class]; } - public function getExtendedType() + public function getExtendedType(): string { return FormType::class; } diff --git a/Resources/config/timed.xml b/Resources/config/timed.xml index cbc718f..d94942a 100644 --- a/Resources/config/timed.xml +++ b/Resources/config/timed.xml @@ -6,7 +6,7 @@ - + diff --git a/composer.json b/composer.json index ae084ae..2aa9fdf 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,15 @@ "description": "Provides spam protection for Symfony3 forms", "keywords": ["spam", "symfony3"], "require": { - "symfony/form": "~3.0|~4.0|~5.0" + "php": ">=7.4", + "symfony/config": "^4.4 || ^5.4 || ^6.0", + "symfony/dependency-injection": "^4.4 || ^5.4 || ^6.0", + "symfony/event-dispatcher": "^4.4 || ^5.4 || ^6.0", + "symfony/form": "^4.4 || ^5.4 || ^6.0", + "symfony/http-foundation": "^4.4 || ^5.4 || ^6.0", + "symfony/http-kernel": "^4.4 || ^5.4 || ^6.0", + "symfony/options-resolver": "^4.4 || ^5.4 || ^6.0", + "symfony/translation-contracts": "1.0 || ^2.0" }, "authors": [ {