99use CodeRhapsodie \DataflowBundle \Registry \DataflowTypeRegistryInterface ;
1010use CodeRhapsodie \DataflowBundle \Repository \ScheduledDataflowRepository ;
1111use Symfony \Component \Console \Attribute \AsCommand ;
12- use Symfony \Component \Console \Command \Command ;
13- use Symfony \Component \Console \Input \InputInterface ;
14- use Symfony \Component \Console \Input \InputOption ;
15- use Symfony \Component \Console \Output \OutputInterface ;
12+ use Symfony \Component \Console \Attribute \Option ;
1613use Symfony \Component \Console \Style \SymfonyStyle ;
1714use Symfony \Component \Validator \Validator \ValidatorInterface ;
1815
2219#[AsCommand('code-rhapsodie:dataflow:schedule:add ' , 'Create a scheduled dataflow ' , help: <<<'TXT'
2320The <info>%command.name%</info> allows you to create a new scheduled dataflow.
2421TXT)]
25- class AddScheduledDataflowCommand extends Command
22+ final readonly class AddScheduledDataflowCommand
2623{
27- public function __construct (private readonly DataflowTypeRegistryInterface $ registry , private readonly ScheduledDataflowRepository $ scheduledDataflowRepository , private readonly ValidatorInterface $ validator , private readonly ConnectionFactory $ connectionFactory )
24+ public function __construct (private DataflowTypeRegistryInterface $ registry , private ScheduledDataflowRepository $ scheduledDataflowRepository , private ValidatorInterface $ validator , private ConnectionFactory $ connectionFactory )
2825 {
29- parent ::__construct ();
3026 }
3127
32- protected function configure (): void
33- {
34- $ this
35- ->addOption ('label ' , null , InputOption::VALUE_REQUIRED , 'Label of the scheduled dataflow ' )
36- ->addOption ('type ' , null , InputOption::VALUE_REQUIRED , 'Type of the scheduled dataflow (FQCN) ' )
37- ->addOption (
38- 'options ' ,
39- null ,
40- InputOption::VALUE_OPTIONAL ,
41- 'Options of the scheduled dataflow (ex: {"option1": "value1", "option2": "value2"}) '
42- )
43- ->addOption ('frequency ' , null , InputOption::VALUE_REQUIRED , 'Frequency of the scheduled dataflow ' )
44- ->addOption ('first_run ' , null , InputOption::VALUE_REQUIRED , 'Date for the first run of the scheduled dataflow (Y-m-d H:i:s) ' )
45- ->addOption ('enabled ' , null , InputOption::VALUE_REQUIRED , 'State of the scheduled dataflow ' )
46- ->addOption ('connection ' , null , InputOption::VALUE_REQUIRED , 'Define the DBAL connection to use ' );
47- }
48-
49- protected function execute (InputInterface $ input , OutputInterface $ output ): int
50- {
51- if ($ input ->getOption ('connection ' ) !== null ) {
52- $ this ->connectionFactory ->setConnectionName ($ input ->getOption ('connection ' ));
28+ public function __invoke (
29+ SymfonyStyle $ io ,
30+ #[Option('Label of the scheduled dataflow ' )] ?string $ label = null ,
31+ #[Option('Type of the scheduled dataflow (FQCN) ' )] ?string $ type = null ,
32+ #[Option('Options of the scheduled dataflow (ex: {"option1": "value1", "option2": "value2"}) ' )] ?string $ options = null ,
33+ #[Option('Frequency of the scheduled dataflow ' )] ?string $ frequency = null ,
34+ #[Option('Date for the first run of the scheduled dataflow (Y-m-d H:i:s) ' )] ?string $ firstRun = null ,
35+ #[Option('State of the scheduled dataflow ' )] ?bool $ enabled = null ,
36+ #[Option('Define the DBAL connection to use ' )] ?string $ connection = null ,
37+ ): int {
38+ if ($ connection !== null ) {
39+ $ this ->connectionFactory ->setConnectionName ($ connection );
5340 }
5441 $ choices = [];
5542 $ typeMapping = [];
@@ -58,36 +45,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5845 $ typeMapping [$ dataflowType ->getLabel ()] = $ fqcn ;
5946 }
6047
61- $ io = new SymfonyStyle ($ input , $ output );
62- $ label = $ input ->getOption ('label ' );
6348 if (!$ label ) {
6449 $ label = $ io ->ask ('What is the scheduled dataflow label? ' );
6550 }
66- $ type = $ input ->getOption ('type ' );
6751 if (!$ type ) {
6852 $ selectedType = $ io ->choice ('What is the scheduled dataflow type? ' , $ choices );
6953 $ type = $ typeMapping [$ selectedType ];
7054 }
71- $ options = $ input ->getOption ('options ' );
7255 if (!$ options ) {
7356 $ options = $ io ->ask (
7457 'What are the launch options for the scheduled dataflow? (ex: {"option1": "value1", "option2": "value2"}) ' ,
7558 json_encode ([])
7659 );
7760 }
78- $ frequency = $ input ->getOption ('frequency ' );
7961 if (!$ frequency ) {
8062 $ frequency = $ io ->choice (
8163 'What is the frequency for the scheduled dataflow? ' ,
8264 ScheduledDataflow::AVAILABLE_FREQUENCIES
8365 );
8466 }
85- $ firstRun = $ input ->getOption ('first_run ' );
8667 if (!$ firstRun ) {
8768 $ firstRun = $ io ->ask ('When is the first execution of the scheduled dataflow (format: Y-m-d H:i:s)? ' );
8869 }
89- $ enabled = $ input ->getOption ('enabled ' );
90- if (!$ enabled ) {
70+ if ($ enabled === null ) {
9171 $ enabled = $ io ->confirm ('Enable the scheduled dataflow? ' );
9272 }
9373
0 commit comments