99
1010class ProBINDImportZone extends Command
1111{
12- /**
13- * The name and signature of the console command.
14- *
15- * @var string
16- */
12+ const SUCCESS_CODE = 0 ;
13+ const ERROR_PARSING_FILE_CODE = 1 ;
14+ const ERROR_EXISTING_ZONE_CODE = 2 ;
15+
1716 protected $ signature = 'probind:import
1817 {--domain= : The zone domain name to create}
19- {--file= : The file name to import}
20- {--force : Delete existing zone before import} ' ;
21-
22- /**
23- * The console command description.
24- *
25- * @var string
26- */
27- protected $ description = 'Import a BIND zone file to ProBIND ' ;
18+ {--file= : The file name to import} ' ;
2819
29- /**
30- * Create a new command instance.
31- */
32- public function __construct ()
33- {
34- parent ::__construct ();
35- }
20+ protected $ description = 'Imports a BIND zone file to ProBIND ' ;
3621
37- /**
38- * Execute the console command.
39- */
40- public function handle (): void
22+ public function handle (): int
4123 {
42- // Cast supplied arguments and options.
43- $ domain = (string ) $ this ->option ('domain ' );
44- $ filename = (string ) $ this ->option ('file ' );
24+ $ domain = $ this ->ensureFQDN ($ this ->option ('domain ' ));
4525
46- // Adds the ending '.' (dot) to the zone name.
47- $ domain = ( substr ( $ domain , - 1 ) != ' . ' ) ? $ domain . ' . ' : $ domain ;
26+ if (Zone:: where ( ' domain ' , $ domain )-> first ()) {
27+ $ this -> error ( ' Zone can not be imported. A zone for the provided domain already exists. ' ) ;
4828
49- if (! $ this ->option ('force ' )) {
50- // Check if Zone exists on database.
51- $ existingZone = Zone::where ('domain ' , $ domain )->first ();
29+ return self ::ERROR_EXISTING_ZONE_CODE ;
30+ }
5231
53- if ($ existingZone ) {
54- $ this ->error ('Zone \'' . $ existingZone ->domain . '\' exists on ProBIND. Use \'--force \' option if you want to import this zone. ' );
32+ try {
33+ $ zoneData = $ this ->parseFile ($ domain , $ this ->option ('file ' ));
34+ } catch (\Throwable $ exception ) {
35+ $ this ->error ('The provided file could not be parsed. ' );
5536
56- return ;
57- }
37+ return self ::ERROR_PARSING_FILE_CODE ;
5838 }
5939
60- // Delete zone, if exists on database.
61- $ this ->deleteZoneIfExists ($ domain );
62-
63- $ zoneData = $ this ->parseFile ($ domain , $ filename );
40+ /** @var Zone $zone */
6441 $ zone = Zone::create ([
65- 'custom_settings ' => true ,
6642 'domain ' => $ domain ,
6743 'reverse_zone ' => Zone::isReverseZoneName ($ domain ),
6844 ]);
@@ -79,6 +55,7 @@ public function handle(): void
7955 ]);
8056 continue ;
8157 }
58+
8259 $ zone ->records ()->create ([
8360 'name ' => $ record ->getName (),
8461 'ttl ' => $ record ->getTtl (),
@@ -88,43 +65,28 @@ public function handle(): void
8865 $ createdRecordsCount ++;
8966 }
9067
91- $ this ->info ('Import zone \'' . $ domain . '\' has created with ' . $ createdRecordsCount . ' imported records. ' );
92- activity ()->log ('Import zone <strong> ' . $ zone ->domain . '</strong> has created <strong> ' . $ createdRecordsCount . '</strong> records. ' );
68+ $ this ->info ('A zone for ' . $ domain . ' domain has been created. ' . $ createdRecordsCount . ' records has been imported. ' );
69+ activity ()->log ('Created zone <strong> ' . $ zone ->domain . '</strong> by importing <strong> ' . $ createdRecordsCount . '</strong> records. ' );
70+
71+ return self ::SUCCESS_CODE ;
9372 }
9473
95- /**
96- * Delete the specified zone by domain search if exists.
97- *
98- * @param string $domain
99- */
100- private function deleteZoneIfExists (string $ domain ): void
74+ private function ensureFQDN (string $ domain ): string
10175 {
102- // Check if Zone exists on database, including trashed zones.
103- $ existingZone = Zone::withTrashed ()
104- ->where ('domain ' , $ domain )->first ();
105-
106- if ($ existingZone ) {
107- $ existingZone ->forceDelete ();
108- }
76+ return (substr ($ domain , -1 ) != '. ' ) ? $ domain . '. ' : $ domain ;
10977 }
11078
11179 /**
11280 * Parses a DNS zone file and returns its content.
11381 *
114- * @param string $domain
115- * @param string $filename
82+ * @param string $domain
83+ * @param string $filename
11684 *
11785 * @return \Badcow\DNS\Zone
118- * @throws \ErrorException
86+ * @throws \Badcow\DNS\Parser\ParseException
11987 */
12088 private function parseFile (string $ domain , string $ filename ): \Badcow \DNS \Zone
12189 {
122- try {
123- $ file = file_get_contents ($ filename );
124-
125- return Parser \Parser::parse ($ domain , $ file );
126- } catch (\Exception $ exception ) {
127- throw new \ErrorException ();
128- }
90+ return Parser \Parser::parse ($ domain , file_get_contents ($ filename ));
12991 }
13092}
0 commit comments