Quellcode durchsuchen

FD3-633 Importador Exclusivo de VoIP

Daniel Libonati vor 6 Jahren
Ursprung
Commit
7b11771c59
1 geänderte Dateien mit 122 neuen und 0 gelöschten Zeilen
  1. 122 0
      src/CablemodemBundle/Command/VoipImportDataCommand.php

+ 122 - 0
src/CablemodemBundle/Command/VoipImportDataCommand.php

@@ -0,0 +1,122 @@
+<?php
+
+namespace CablemodemBundle\Command;
+
+use mysqli;
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+use Symfony\Component\Console\Helper\ProgressBar;
+use Symfony\Component\Console\Question\ConfirmationQuestion;
+use Doctrine\DBAL\DriverManager;
+use Symfony\Component\Yaml\Yaml;
+
+
+class VoipImportDataCommand extends ContainerAwareCommand
+{
+
+    protected $em;
+
+    protected function configure()
+    {
+        $this
+            ->setName('voip:import:data')
+            ->setDescription('Imports client data from Flowdat 2')
+            ->setHelp('Imports Client data from Flowdat 2.')
+            ->addOption('dbname', null, InputOption::VALUE_OPTIONAL, 'Origin Database name. Default: flowdat2')
+            ->addOption('user', null, InputOption::VALUE_OPTIONAL, 'User name for origin database. Default: root')
+            ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'Password for origin database. Default: "empty_value"')
+            ->addOption('host', null, InputOption::VALUE_OPTIONAL, 'Host of origin database. Default: localhost')
+        ;
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+
+        $container = $this->getContainer();
+        $this->em = $container->get('doctrine.orm.entity_manager');
+
+        // Get data
+        $host =     $input->getOption('host');
+        $dbname =   $input->getOption('dbname');
+        $user =     $input->getOption('user');
+        $password = $input->getOption('password');
+
+        // Set defaults
+        if (!$dbname)   { $dbname = 'flowdat2'; }
+        if (!$user)     { $user = 'root'; }
+        if (!$password) { $password = ''; }
+        if (!$host)     { $host = 'localhost'; }
+
+        // Presentation
+        $io = new SymfonyStyle($input, $output);
+        $io->title('Flowdat3 VoIP Data Importer');
+
+        // Ask for confirmation
+        if (!$input->getOption('no-interaction')) {
+            $helper = $this->getHelper('question');
+            $question = new ConfirmationQuestion("This will IMPORT DATA FROM a Flowdat2 database. The CABLEMODEM TABLE will be ALTERED by OVERWRITING existing data, leading to potential DATA LOSS. Are you really sure you want to proceed? ", false);
+            if (!$helper->ask($input, $output, $question)) {
+                return;
+            }
+        }
+
+        $io->newLine();
+
+        $conn = new mysqli($host, $user, $password, $dbname);
+        $sql =  "SELECT * FROM Voip;";
+        $rows = $conn->query($sql);
+
+        // Progress bar
+        $progressBar = new ProgressBar($output, $rows->num_rows);
+        $errors = [];
+
+        while ($row = $rows->fetch_assoc()) {
+            
+            if ($row['cablemodem_id'] != null) {
+                
+                $object = $this->em->getRepository('CablemodemBundle:Cablemodem')->findOneById($row['cablemodem_id']);
+
+                if (!is_null($object)) {
+
+                    $voip = $object->getVoip();
+                    
+                    if (is_null($voip)) { $voip = array(); }
+    
+                    $found = null;
+    
+                    // Look for an item containing the number
+                    foreach ($voip as $key=>$item) {
+                        if ($item['number'] == $row['number']) {
+                            $found = $key;
+                        }
+                    }
+    
+                    if (is_null($found)) {      // If number wasn't found, append.
+                        $voip[] = array(
+                            'number' => $row['number'],
+                            'password' => $row['password']
+                        );
+                    } else {                    // If number was found, update password.
+                        $voip[$found]['password'] = $row['password'];
+                    }
+                    
+                    // Persist
+                    $object->setVoip($voip);
+                    $this->em->persist($object);
+                    $this->em->flush();
+                }    
+            } else {
+                $errors[] = "<error>[{$row['id']}] Unable to find a Cablemodem witih ID {$row['cablemodem_id']}. Element has been skipped.</error>";
+            }
+
+            $progressBar->advance();
+        }
+        
+        $output->writeln(['']);
+        $output->writeln($errors);
+        $output->writeln(['', 'Done!']);
+    }
+}