55namespace SchedulerBundle \Command ;
66
77use SchedulerBundle \Export \ExporterRegistryInterface ;
8+ use SchedulerBundle \SchedulerInterface ;
9+ use SchedulerBundle \Task \TaskInterface ;
810use Symfony \Component \Console \Command \Command ;
911use Symfony \Component \Console \Input \InputInterface ;
12+ use Symfony \Component \Console \Input \InputOption ;
1013use Symfony \Component \Console \Output \OutputInterface ;
1114use Symfony \Component \Console \Style \SymfonyStyle ;
15+ use Throwable ;
1216
1317/**
1418 * @author Guillaume Loulier <[email protected] > 1519 */
1620final class ExportCommand extends Command
1721{
1822 private ExporterRegistryInterface $ exporterRegistry ;
23+ private SchedulerInterface $ scheduler ;
1924
2025 protected static $ defaultName = 'scheduler:export ' ;
2126
22- public function __construct (ExporterRegistryInterface $ exporterRegistry )
23- {
27+ public function __construct (
28+ ExporterRegistryInterface $ exporterRegistry ,
29+ SchedulerInterface $ scheduler
30+ ) {
2431 $ this ->exporterRegistry = $ exporterRegistry ;
32+ $ this ->scheduler = $ scheduler ;
2533
2634 parent ::__construct ();
2735 }
@@ -33,6 +41,10 @@ protected function configure(): void
3341 {
3442 $ this
3543 ->setDescription ('Export tasks to a specific format ' )
44+ ->setDefinition ([
45+ new InputOption ('format ' , null , InputOption::VALUE_OPTIONAL , 'The format used to export tasks ' , 'crontab ' ),
46+ new InputOption ('filename ' , null , InputOption::VALUE_OPTIONAL , 'The name of the filename used to export tasks ' , 'crontab ' ),
47+ ])
3648 ;
3749 }
3850
@@ -43,7 +55,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4355 {
4456 $ style = new SymfonyStyle ($ input , $ output );
4557
46- // TODO
58+ try {
59+ $ tasks = $ this ->scheduler ->getTasks ();
60+ if (0 === $ tasks ->count ()) {
61+ $ style ->warning ('No tasks found ' );
62+
63+ return Command::FAILURE ;
64+ }
65+
66+ $ filename = $ input ->getOption ('filename ' );
67+ $ exporter = $ this ->exporterRegistry ->find ($ input ->getOption ('format ' ));
68+
69+ $ tasks ->walk (function (TaskInterface $ task ) use ($ exporter , $ filename ): void {
70+ $ exporter ->export ($ filename , $ task );
71+ });
72+ } catch (Throwable $ throwable ) {
73+ $ style ->error ([
74+ 'An error occurred when exporting tasks ' ,
75+ $ throwable ->getMessage (),
76+ ]);
77+
78+ return Command::FAILURE ;
79+ }
80+
81+ $ style ->success ('The export has succeed ' );
4782
4883 return Command::SUCCESS ;
4984 }
0 commit comments