Skip to content

Commit

Permalink
[CHANGE] Enforce a stricter code style (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoHaase authored Jan 23, 2024
1 parent 76fa4db commit 9c643da
Show file tree
Hide file tree
Showing 31 changed files with 198 additions and 137 deletions.
16 changes: 9 additions & 7 deletions Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* All rights reserved.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\Command;

use Rewieer\TaskSchedulerBundle\Task\Scheduler;
Expand All @@ -28,14 +30,14 @@ public function __construct(Scheduler $scheduler)
protected function configure(): void
{
$this
->setName("ts:list")
->setDescription("List the existing tasks")
->setHelp("This command display the list of registered tasks.")
->setName('ts:list')
->setDescription('List the existing tasks')
->setHelp('This command display the list of registered tasks.')
->addOption(
"show-run-dates",
'show-run-dates',
null,
InputOption::VALUE_OPTIONAL,
"Show next run dates (default value: " . self::NUMBER_OF_RUN_DATES . ")",
'Show next run dates (default value: ' . self::NUMBER_OF_RUN_DATES . ')',
false
);
}
Expand All @@ -46,10 +48,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$showRunDates = $numberOfRunDates !== false;

$table = new Table($output);
$tableHeaders = ["ID", "Class"];
$tableHeaders = ['ID', 'Class'];

if ($showRunDates) {
$tableHeaders[] = "Next " . $numberOfRunDates . " run dates";
$tableHeaders[] = 'Next ' . $numberOfRunDates . ' run dates';
}

$table->setHeaders($tableHeaders);
Expand Down
18 changes: 10 additions & 8 deletions Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* All rights reserved.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\Command;

use Exception;
Expand All @@ -28,21 +30,21 @@ public function __construct(Scheduler $scheduler)
protected function configure(): void
{
$this
->setName("ts:run")
->setDescription("Run due tasks")
->setName('ts:run')
->setDescription('Run due tasks')
->setHelp(<<<'EOF'
This command actually run the tasks that are due at the moment the command is called.
This command should not be called manually. Check the documentation to learn how to set CRON jobs.
EOF
)
->addArgument("id", InputArgument::OPTIONAL, "The ID of the task. Check ts:list for IDs")
->addOption("class", "c", InputOption::VALUE_OPTIONAL, "the class name of the task (without namespace)");
->addArgument('id', InputArgument::OPTIONAL, 'The ID of the task. Check ts:list for IDs')
->addOption('class', 'c', InputOption::VALUE_OPTIONAL, 'the class name of the task (without namespace)');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$id = $input->getArgument("id");
$class = $input->getOption("class");
$id = $input->getArgument('id');
$class = $input->getOption('class');

if (!$id && !$class) {
$this->scheduler->run();
Expand All @@ -54,13 +56,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return self::SUCCESS;
}
}
throw new Exception("There are no tasks corresponding to this class name");
throw new Exception('There are no tasks corresponding to this class name');
} else {
$tasks = $this->scheduler->getTasks();
$id = (int)$id;

if (array_key_exists($id - 1, $tasks) === false) {
throw new Exception("There are no tasks corresponding to this ID");
throw new Exception('There are no tasks corresponding to this ID');
}

$this->scheduler->runTask($tasks[$id - 1]);
Expand Down
6 changes: 4 additions & 2 deletions DependencyInjection/Compiler/EventDispatcherPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand All @@ -16,7 +18,7 @@
* Class TaskPass
* @package Rewieer\TaskSchedulerBundle\DependencyInjection\Compiler
*
* Adds services tagged with "ts.task" to the scheduler
* Adds services tagged with 'ts.task' to the scheduler
*/
class EventDispatcherPass implements CompilerPassInterface
{
Expand All @@ -26,7 +28,7 @@ public function process(ContainerBuilder $container): void
$tasks = $container->findTaggedServiceIds('ts.event_subscriber');

foreach ($tasks as $id => $tags) {
$definition->addMethodCall("addSubscriber", [new Reference($id)]);
$definition->addMethodCall('addSubscriber', [new Reference($id)]);
}
}
}
6 changes: 4 additions & 2 deletions DependencyInjection/Compiler/TaskPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand All @@ -16,7 +18,7 @@
* Class TaskPass
* @package Rewieer\TaskSchedulerBundle\DependencyInjection\Compiler
*
* Adds services tagged with "ts.task" to the scheduler
* Adds services tagged with 'ts.task' to the scheduler
*/
class TaskPass implements CompilerPassInterface
{
Expand All @@ -26,7 +28,7 @@ public function process(ContainerBuilder $container): void
$tasks = $container->findTaggedServiceIds('ts.task');

foreach ($tasks as $id => $tags) {
$definition->addMethodCall("addTask", [new Reference($id)]);
$definition->addMethodCall('addTask', [new Reference($id)]);
}
}
}
2 changes: 2 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand Down
2 changes: 2 additions & 0 deletions DependencyInjection/RewieerTaskSchedulerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\DependencyInjection;

use Rewieer\TaskSchedulerBundle\Task\TaskInterface;
Expand Down
2 changes: 2 additions & 0 deletions Event/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\Event;

class EventDispatcher
Expand Down
1 change: 1 addition & 0 deletions Event/EventSubscriberInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\Event;

Expand Down
2 changes: 2 additions & 0 deletions RewieerTaskSchedulerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle;

use Rewieer\TaskSchedulerBundle\DependencyInjection\Compiler\EventDispatcherPass;
Expand Down
22 changes: 12 additions & 10 deletions Services/SchedulerLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\Services;

use DateTime;
Expand Down Expand Up @@ -33,40 +35,40 @@ public function __construct(LoggerInterface $logger)
public function onStart(): void
{
$this->start = microtime(true);
$this->logger->info(sprintf("[%s] Starting...", (new Datetime())->format("d/m/y H:i:s")));
$this->logger->info(sprintf('[%s] Starting...', (new Datetime())->format('d/m/y H:i:s')));
}

public function beforeTaskRuns(TaskInterface $task): void
{
$this->current = microtime(true);
$this->logger->info(sprintf("Running %s", get_class($task)));
$this->logger->info(sprintf('Running %s', get_class($task)));
}

public function afterTaskRuns(TaskInterface $task): void
{
$time = microtime(true) - $this->current;
$this->logger->info(sprintf("Finished %s in %fs", get_class($task), $time));
$this->logger->info(sprintf('Finished %s in %fs', get_class($task), $time));
}

public function onEnd(): void
{
$time = microtime(true) - $this->start;
$this->logger->info(sprintf("Finished ! Took %fs", $time));
$this->logger->info(sprintf('Finished ! Took %fs', $time));
}

public function onSkip(TaskInterface $task): void
{
$this->logger->info(sprintf("Skipped %s", get_class($task)));
$this->logger->info(sprintf('Skipped %s', get_class($task)));
}

public static function getEvents(): array
{
return [
SchedulerEvents::ON_START => "onStart",
SchedulerEvents::BEFORE_TASK_RUNS => "beforeTaskRuns",
SchedulerEvents::AFTER_TASK_RUNS => "afterTaskRuns",
SchedulerEvents::ON_SKIP => "onSkip",
SchedulerEvents::ON_END => "onEnd",
SchedulerEvents::ON_START => 'onStart',
SchedulerEvents::BEFORE_TASK_RUNS => 'beforeTaskRuns',
SchedulerEvents::AFTER_TASK_RUNS => 'afterTaskRuns',
SchedulerEvents::ON_SKIP => 'onSkip',
SchedulerEvents::ON_END => 'onEnd',
];
}
}
2 changes: 2 additions & 0 deletions Task/AbstractScheduledTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\Task;

use DateTimeInterface;
Expand Down
16 changes: 9 additions & 7 deletions Task/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\Task;

use Cron\CronExpression;
Expand All @@ -25,7 +27,7 @@ class Schedule
* Schedule constructor.
* @param string $expr the default cron
*/
public function __construct(string $expr = "* * * * *")
public function __construct(string $expr = '* * * * *')
{
$this->cron = new CronExpression($expr, new FieldFactory());
}
Expand Down Expand Up @@ -69,7 +71,7 @@ public function daysOfMonth($days): self
*/
public function daily(): self
{
$this->cron->setPart(CronExpression::DAY, "*");
$this->cron->setPart(CronExpression::DAY, '*');
return $this;
}

Expand Down Expand Up @@ -112,15 +114,15 @@ public function everyHours(int $hours = 1): self
}

/**
* Generic function to update a cron part as an "everyX" pattern
* such as "every 3 hours" or "every 10 minutes"
* Generic function to update a cron part as an 'everyX' pattern
* such as 'every 3 hours' or 'every 10 minutes'
*/
public function everyX(int $time = 1, int $part = CronExpression::MINUTE): self
{
if ($time === 0 || $time === 1) {
$expr = "*";
$expr = '*';
} else {
$expr = "*/" . (string)$time;
$expr = '*/' . (string)$time;
}

$this->cron->setPart($part, $expr);
Expand All @@ -138,7 +140,7 @@ public function getExpression(): string
}

/**
* Allows setting entire expression in string format like "0 * 2,7,12 * 7"
* Allows setting entire expression in string format like '0 * 2,7,12 * 7'
* Exposes CronExpressions method directly
*/
public function setExpression(string $value): self
Expand Down
5 changes: 3 additions & 2 deletions Task/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\Task;

use DateTimeInterface;
use Rewieer\TaskSchedulerBundle\Event\EventDispatcher;

class Scheduler
Expand All @@ -34,7 +35,7 @@ public function addTask(TaskInterface $task): void
$this->tasks[] = $task;
}

public function run($currentTime = "now"): void
public function run($currentTime = 'now'): void
{
$this->dispatcher->dispatch(SchedulerEvents::ON_START);

Expand Down
12 changes: 7 additions & 5 deletions Task/SchedulerEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\Task;

class SchedulerEvents
{
public const ON_START = "onStart"; // called when the scheduler starts to loop through tasks
public const BEFORE_TASK_RUNS = "beforeTaskRuns"; // called before a task gets executed
public const AFTER_TASK_RUNS = "afterTaskRuns"; // called after a task gets executed
public const ON_SKIP = "onSkip"; // called when the task is skipped
public const ON_END = "onEnd"; // called after the scheduler runs all the tasks
public const ON_START = 'onStart'; // called when the scheduler starts to loop through tasks
public const BEFORE_TASK_RUNS = 'beforeTaskRuns'; // called before a task gets executed
public const AFTER_TASK_RUNS = 'afterTaskRuns'; // called after a task gets executed
public const ON_SKIP = 'onSkip'; // called when the task is skipped
public const ON_END = 'onEnd'; // called after the scheduler runs all the tasks
}
2 changes: 2 additions & 0 deletions Task/TaskInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Rewieer\TaskSchedulerBundle\Task;

use DateTimeInterface;
Expand Down
Loading

0 comments on commit 9c643da

Please sign in to comment.