Skip to content

Commit

Permalink
Add CI
Browse files Browse the repository at this point in the history
  • Loading branch information
maelanleborgne committed Jan 8, 2024
1 parent 2ae5a41 commit ad6a91e
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 98 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: CI

on:
pull_request: ~
push:
branches:
- main
release:
types:
- created
schedule:
- cron: '43 21 * * *'

jobs:
test:
name: "Build and Test - PHP ${{ matrix.php }} Symfony:${{ matrix.symfony-version }} ${{ matrix.deps }}"
runs-on: ubuntu-latest
env:
SYMFONY_REQUIRE: ${{ matrix.symfony-version }}
strategy:
matrix:
include:
- symfony-version: 5.4
php: 7.4
- symfony-version: 6.4
php: 8.1
- symfony-version: 7.0
php: 8.2
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
coverage: none
php-version: ${{ matrix.php }}

- name: Install dependencies
run: composer install

- name: Run tests
run: |
./vendor/bin/simple-phpunit
php-cs-fixer:
name: PHP CS Fixer
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
tools: php-cs-fixer

- name: Run PHP-CS-Fixer
run:
php-cs-fixer fix --dry-run --diff
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/composer.lock
/vendor
/.phpunit.result.cache
/.php-cs-fixer.cache
25 changes: 25 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

$dirs = [
'DependencyInjection',
'Form',
'tests',
];

foreach ($dirs as $dir) {
if (!file_exists($dir)) {
exit(0);
}
}

$finder = (new \PhpCsFixer\Finder())
->in($dirs);

return (new \PhpCsFixer\Config())
->setRules(array(
'@Symfony' => true,
'@Symfony:risky' => true,
'phpdoc_to_comment' => false,
))
->setRiskyAllowed(true)
->setFinder($finder);
3 changes: 0 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('isometriks_spam');
Expand Down
15 changes: 6 additions & 9 deletions DependencyInjection/IsometriksSpamExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Isometriks\Bundle\SpamBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* This is the class that loads and manages your bundle configuration.
Expand All @@ -14,9 +14,6 @@
*/
class IsometriksSpamExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
Expand All @@ -36,12 +33,12 @@ private function processTimedConfig(array $config, ContainerBuilder $container,
$loader->load('timed.xml');

$definition = $container->getDefinition('isometriks_spam.form.extension.type.timed_spam');
$definition->addArgument(array(
$definition->addArgument([
'min' => $config['min'],
'max' => $config['max'],
'global' => $config['global'],
'message' => $config['message'],
));
]);
}

private function processHoneypotConfig(array $config, ContainerBuilder $container, XmlFileLoader $loader): void
Expand All @@ -53,12 +50,12 @@ private function processHoneypotConfig(array $config, ContainerBuilder $containe
$loader->load('honeypot.xml');

$definition = $container->getDefinition('isometriks_spam.form.extension.type.honeypot');
$definition->addArgument(array(
$definition->addArgument([
'field' => $config['field'],
'use_class' => $config['use_class'],
'hide_class' => $config['hide_class'],
'global' => $config['global'],
'message' => $config['message'],
));
]);
}
}
11 changes: 5 additions & 6 deletions Form/Extension/Spam/EventListener/HoneypotValidationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public function __construct(
string $translationDomain,
string $fieldName,
string $errorMessage
)
{
) {
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->fieldName = $fieldName;
Expand All @@ -38,13 +37,13 @@ public function preSubmit(FormEvent $event): void
$errorMessage = $this->errorMessage;

if (null !== $this->translator) {
$errorMessage = $this->translator->trans($errorMessage, array(), $this->translationDomain);
$errorMessage = $this->translator->trans($errorMessage, [], $this->translationDomain);
}

$form->addError(new FormError($errorMessage));
}

if (is_array($data)) {
if (\is_array($data)) {
unset($data[$this->fieldName]);
}
}
Expand All @@ -54,8 +53,8 @@ public function preSubmit(FormEvent $event): void

public static function getSubscribedEvents(): array
{
return array(
return [
FormEvents::PRE_SUBMIT => 'preSubmit',
);
];
}
}
21 changes: 10 additions & 11 deletions Form/Extension/Spam/EventListener/TimedSpamValidationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public function __construct(
string $translationDomain,
string $errorMessage,
array $options
)
{
) {
$this->timeProvider = $timeProvider;
$this->translator = $translator;
$this->translationDomain = $translationDomain;
Expand All @@ -36,13 +35,13 @@ public function preSubmit(FormEvent $event): void
{
$form = $event->getForm();

if ($form->isRoot() &&
$form->getConfig()->getOption('compound') &&
!$this->timeProvider->isFormTimeValid($form->getName(), $this->options)) {
if ($form->isRoot()
&& $form->getConfig()->getOption('compound')
&& !$this->timeProvider->isFormTimeValid($form->getName(), $this->options)) {
$errorMessage = $this->errorMessage;

if (null !== $this->translator) {
$errorMessage = $this->translator->trans($errorMessage, array(), $this->translationDomain);
$errorMessage = $this->translator->trans($errorMessage, [], $this->translationDomain);
}

$form->addError(new FormError($errorMessage));
Expand All @@ -58,19 +57,19 @@ public function postSubmit(FormEvent $event): void
{
$form = $event->getForm();

if ($form->isRoot() &&
$form->getConfig()->getOption('compound') &&
!$form->isValid()) {
if ($form->isRoot()
&& $form->getConfig()->getOption('compound')
&& !$form->isValid()) {
// If the form has errors, set the time again
$this->timeProvider->generateFormTime($form->getName());
}
}

public static function getSubscribedEvents(): array
{
return array(
return [
FormEvents::PRE_SUBMIT => 'preSubmit',
FormEvents::POST_SUBMIT => 'postSubmit',
);
];
}
}
13 changes: 6 additions & 7 deletions Form/Extension/Spam/Provider/SessionTimedSpamProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

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

Expand All @@ -15,9 +14,9 @@ public function __construct(RequestStack $requestStack)
$this->requestStack = $requestStack;
}

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

$this->getSession()->set($key, $startTime);
Expand All @@ -33,16 +32,16 @@ public function isFormTimeValid(string $name, array $options): bool
/*
* No value stored, so this can't be valid or session expired.
*/
if ($startTime === false) {
if (false === $startTime) {
return false;
}

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

/*
* Check against a minimum time
*/
if ($options['min'] !== null) {
if (null !== $options['min']) {
$minTime = clone $startTime;
$minTime->modify(sprintf('+%d seconds', $options['min']));

Expand All @@ -52,7 +51,7 @@ public function isFormTimeValid(string $name, array $options): bool
/*
* Check against a maximum time
*/
if ($options['max'] !== null) {
if (null !== $options['max']) {
$maxTime = clone $startTime;
$maxTime->modify(sprintf('+%d seconds', $options['max']));

Expand Down
15 changes: 1 addition & 14 deletions Form/Extension/Spam/Provider/TimedSpamProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@

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

use DateTime;

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

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

Expand All @@ -31,17 +23,12 @@ public function getFormTime(string $name);

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

/**
* Check if form time is valid.
*
* @param string $name
* @param array $options
*
* @return bool $valid
*/
public function isFormTimeValid(string $name, array $options): bool;
Expand Down
22 changes: 9 additions & 13 deletions Form/Extension/Spam/Type/FormTypeHoneypotExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public function __construct(
?TranslatorInterface $translator,
string $translationDomain,
array $defaults
)
{
) {
$this->translator = $translator;
$this->translationDomain = $translationDomain;
$this->defaults = $defaults;
Expand Down Expand Up @@ -52,20 +51,20 @@ public function finishView(FormView $view, FormInterface $form, array $options):
throw new \RuntimeException(sprintf('Honeypot field "%s" is already in use.', $options['honeypot_field']));
}

$formOptions = array(
$formOptions = [
'mapped' => false,
'label' => false,
'required' => false,
);
];

if ($options['honeypot_use_class']) {
$formOptions['attr'] = array(
$formOptions['attr'] = [
'class' => $options['honeypot_hide_class'],
);
];
} else {
$formOptions['attr'] = array(
$formOptions['attr'] = [
'style' => 'display:none',
);
];
}

$factory = $form->getConfig()->getAttribute('honeypot_factory');
Expand All @@ -77,18 +76,15 @@ public function finishView(FormView $view, FormInterface $form, array $options):

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(array(
$resolver->setDefaults([
'honeypot' => $this->defaults['global'],
'honeypot_use_class' => $this->defaults['use_class'],
'honeypot_hide_class' => $this->defaults['hide_class'],
'honeypot_field' => $this->defaults['field'],
'honeypot_message' => $this->defaults['message'],
));
]);
}

/**
* @inheritdoc
*/
public static function getExtendedTypes(): iterable
{
return [FormType::class];
Expand Down
Loading

0 comments on commit ad6a91e

Please sign in to comment.