1+ <?php
2+
3+ namespace App \Command ;
4+
5+ use Symfony \Component \Console \Attribute \AsCommand ;
6+ use Symfony \Component \Console \Command \Command ;
7+ use Symfony \Component \Console \Input \InputArgument ;
8+ use Symfony \Component \Console \Input \InputInterface ;
9+ use Symfony \Component \Console \Output \OutputInterface ;
10+ use Symfony \Component \Console \Style \SymfonyStyle ;
11+ use Doctrine \ORM \EntityManagerInterface ;
12+ use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
13+ use App \Entity \Datasource ;
14+
15+
16+ #[AsCommand(
17+ name: 'app:datasource:create ' ,
18+ description: 'Create a new ingest datasource in the backend application ' ,
19+ )]
20+ class DatasourceCreateCommand extends Command
21+ {
22+ private $ entityManager ;
23+ private $ urlGenerator ;
24+
25+ public function __construct (EntityManagerInterface $ entityManager , UrlGeneratorInterface $ urlGenerator )
26+ {
27+ parent ::__construct ();
28+ $ this ->entityManager = $ entityManager ;
29+ $ this ->urlGenerator = $ urlGenerator ;
30+ }
31+
32+ protected function configure (): void
33+ {
34+ $ this
35+ ->addArgument ('name ' , InputArgument::REQUIRED , 'Name of the datasource ' )
36+ ->addArgument ('index ' , InputArgument::REQUIRED , 'Target index in Elasticsearch ' )
37+ ->addArgument ('validFrom ' , InputArgument::OPTIONAL , '(optional) Valid from (YYYY-MM-DD) ' )
38+ ->addArgument ('validTo ' , InputArgument::OPTIONAL , '(optional) Valid to (YYYY-MM-DD) ' );
39+ }
40+
41+ protected function execute (InputInterface $ input , OutputInterface $ output ): int
42+ {
43+ $ io = new SymfonyStyle ($ input , $ output );
44+ $ name = $ input ->getArgument ('name ' );
45+ $ index = $ input ->getArgument ('index ' );
46+ $ validFrom = $ input ->getArgument ('validFrom ' );
47+ $ validTo = $ input ->getArgument ('validTo ' );
48+
49+ $ validFromDate = null ;
50+ $ validToDate = null ;
51+
52+ if ($ validFrom ) {
53+ try {
54+ $ validFromDate = new \DateTime ($ validFrom );
55+ }catch (\Exception $ e ){
56+ $ io ->error ('Invalid date format for validFrom. Please use YYYY-MM-DD. ' );
57+ return Command::FAILURE ;
58+ }
59+ }
60+
61+ if ($ validTo ) {
62+ try {
63+ $ validToDate = new \DateTime ($ validTo );
64+ }catch (\Exception $ e ){
65+ $ io ->error ('Invalid date format for validTo. Please use YYYY-MM-DD. ' );
66+ return Command::FAILURE ;
67+ }
68+ }
69+
70+ $ existingDatasource = $ this ->entityManager ->getRepository (Datasource::class)->findOneBy (['name ' => $ name ]);
71+
72+ if ($ existingDatasource ) {
73+ $ io ->error (sprintf ('Datasource "%s" already exists. ' , $ name ));
74+ return Command::FAILURE ;
75+ }
76+
77+ $ datasource = new Datasource ();
78+ $ output ->writeln (sprintf ('%s - %s ' , $ name , $ index ));
79+ $ datasource ->setName ($ name );
80+ $ datasource ->setTargetIndex ($ index );
81+ $ datasource ->setValidFrom ($ validFromDate );
82+ $ datasource ->setValidTo ($ validToDate );
83+
84+ try {
85+ $ this ->entityManager ->persist ($ datasource );
86+ $ this ->entityManager ->flush ();
87+ } catch (\Exception $ e ) {
88+ $ io ->error ('An error occurred while creating the datasource: ' . $ e ->getMessage ());
89+ return Command::FAILURE ;
90+ }
91+
92+ $ io ->success (sprintf ('Datasource "%s" created successfully ' , $ name ));
93+ $ io ->writeln (sprintf ('Valid from %s ' , $ validFrom ? $ validFromDate ->format ('Y-m-d ' ) : 'N/A ' ));
94+ $ io ->writeln (sprintf ('Valid to %s ' , $ validTo ? $ validToDate ->format ('Y-m-d ' ) : 'N/A ' ));
95+ $ io ->writeln (sprintf ('Ingest key (header X-Ingest-Key): %s ' , $ datasource ->getIngestKey ()));
96+ $ io ->writeln (sprintf ('Forwarder URL: %s ' , $ this ->urlGenerator ->generate ('app_ingest_json ' , [], UrlGeneratorInterface::ABSOLUTE_URL )));
97+
98+ return Command::SUCCESS ;
99+ }
100+ }
0 commit comments