Skip to content

Commit

Permalink
SchedulerExtension: schema refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Jan 19, 2025
1 parent 800d061 commit 6a6960f
Showing 1 changed file with 23 additions and 46 deletions.
69 changes: 23 additions & 46 deletions src/DI/SchedulerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Nette\DI\ContainerBuilder;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Elements\AnyOf;
use Nette\Schema\Elements\Type;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use OriNette\DI\Definitions\DefinitionsLoader;
Expand Down Expand Up @@ -41,6 +43,21 @@
final class SchedulerExtension extends CompilerExtension
{

private function createCallbackSchema(): AnyOf
{
return Expect::anyOf(
Expect::string(),
/* @infection-ignore-all */
Expect::array()->min(2)->max(2),
Expect::type(Statement::class),
);
}

private function createJobEventsSchema(): Type
{
return Expect::listOf($this->createCallbackSchema());
}

public function getConfigSchema(): Schema
{
return Expect::structure([
Expand All @@ -57,46 +74,11 @@ public function getConfigSchema(): Schema
'runJobCommand' => Expect::string()->default('scheduler:run-job'),
]),
'events' => Expect::structure([
'beforeRun' => Expect::listOf(
Expect::anyOf(
Expect::string(),
/* @infection-ignore-all */
Expect::array()->min(2)->max(2),
Expect::type(Statement::class),
),
),
'afterRun' => Expect::listOf(
Expect::anyOf(
Expect::string(),
/* @infection-ignore-all */
Expect::array()->min(2)->max(2),
Expect::type(Statement::class),
),
),
'lockedJob' => Expect::listOf(
Expect::anyOf(
Expect::string(),
/* @infection-ignore-all */
Expect::array()->min(2)->max(2),
Expect::type(Statement::class),
),
),
'beforeJob' => Expect::listOf(
Expect::anyOf(
Expect::string(),
/* @infection-ignore-all */
Expect::array()->min(2)->max(2),
Expect::type(Statement::class),
),
),
'afterJob' => Expect::listOf(
Expect::anyOf(
Expect::string(),
/* @infection-ignore-all */
Expect::array()->min(2)->max(2),
Expect::type(Statement::class),
),
),
'beforeRun' => $this->createJobEventsSchema(),
'afterRun' => $this->createJobEventsSchema(),
'lockedJob' => $this->createJobEventsSchema(),
'beforeJob' => $this->createJobEventsSchema(),
'afterJob' => $this->createJobEventsSchema(),
]),
'jobs' => Expect::arrayOf(
Expect::structure([
Expand All @@ -114,12 +96,7 @@ static function (string $value): bool {
},
'Valid cron expression',
),
'callback' => Expect::anyOf(
Expect::string(),
/* @infection-ignore-all */
Expect::array()->min(2)->max(2),
Expect::type(Statement::class),
)->default(null),
'callback' => $this->createCallbackSchema()->default(null),
'job' => DefinitionsLoader::schema()->default(null),
'repeatAfterSeconds' => Expect::int(0)
->min(0)
Expand Down

0 comments on commit 6a6960f

Please sign in to comment.