VoipImportDataCommand.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace CablemodemBundle\Command;
  3. use mysqli;
  4. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Style\SymfonyStyle;
  9. use Symfony\Component\Console\Helper\ProgressBar;
  10. use Symfony\Component\Console\Question\ConfirmationQuestion;
  11. use Doctrine\DBAL\DriverManager;
  12. use Symfony\Component\Yaml\Yaml;
  13. class VoipImportDataCommand extends ContainerAwareCommand
  14. {
  15. protected $em;
  16. protected function configure()
  17. {
  18. $this
  19. ->setName('voip:import:data')
  20. ->setDescription('Imports voip data from Flowdat 2')
  21. ->setHelp('Imports Voip data from Flowdat 2.')
  22. ->addOption('dbname', null, InputOption::VALUE_OPTIONAL, 'Origin Database name. Default: flowdat2')
  23. ->addOption('user', null, InputOption::VALUE_OPTIONAL, 'User name for origin database. Default: root')
  24. ->addOption('password', null, InputOption::VALUE_OPTIONAL, 'Password for origin database. Default: "empty_value"')
  25. ->addOption('host', null, InputOption::VALUE_OPTIONAL, 'Host of origin database. Default: localhost')
  26. ;
  27. }
  28. protected function execute(InputInterface $input, OutputInterface $output)
  29. {
  30. $container = $this->getContainer();
  31. $this->em = $container->get('doctrine.orm.entity_manager');
  32. // Get data
  33. $host = $input->getOption('host');
  34. $dbname = $input->getOption('dbname');
  35. $user = $input->getOption('user');
  36. $password = $input->getOption('password');
  37. // Set defaults
  38. if (!$dbname) { $dbname = 'flowdat2'; }
  39. if (!$user) { $user = 'root'; }
  40. if (!$password) { $password = ''; }
  41. if (!$host) { $host = 'localhost'; }
  42. // Presentation
  43. $io = new SymfonyStyle($input, $output);
  44. $io->title('Flowdat3 VoIP Data Importer');
  45. // Ask for confirmation
  46. if (!$input->getOption('no-interaction')) {
  47. $helper = $this->getHelper('question');
  48. $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);
  49. if (!$helper->ask($input, $output, $question)) {
  50. return;
  51. }
  52. }
  53. $io->newLine();
  54. $conn = new mysqli($host, $user, $password, $dbname);
  55. $sql = "SELECT * FROM Voip;";
  56. $rows = $conn->query($sql);
  57. // Progress bar
  58. $progressBar = new ProgressBar($output, $rows->num_rows);
  59. $errors = [];
  60. while ($row = $rows->fetch_assoc()) {
  61. if ($row['cablemodem_id'] != null) {
  62. $object = $this->em->getRepository('CablemodemBundle:Cablemodem')->findOneById($row['cablemodem_id']);
  63. if (!is_null($object)) {
  64. $voip = $object->getVoip();
  65. if (is_null($voip)) { $voip = array(); }
  66. $found = null;
  67. // Look for an item containing the number
  68. foreach ($voip as $key => $item) {
  69. if ($item['number'] == $row['number']) {
  70. $found = $key;
  71. }
  72. }
  73. if (is_null($found)) { // If number wasn't found, append.
  74. $voip[] = array(
  75. 'number' => $row['number'],
  76. 'password' => $row['password']
  77. );
  78. } else { // If number was found, update password.
  79. $voip[$found]['password'] = $row['password'];
  80. }
  81. // Persist
  82. $object->setVoip($voip);
  83. $this->em->persist($object);
  84. $this->em->flush();
  85. // Detach the object from the entity manager
  86. $this->em->detach($object);
  87. }
  88. } else {
  89. $errors[] = "<error>[{$row['id']}] Unable to find a Cablemodem with ID {$row['cablemodem_id']}. Element has been skipped.</error>";
  90. }
  91. $progressBar->advance();
  92. }
  93. $output->writeln(['']);
  94. $output->writeln($errors);
  95. $output->writeln(['', 'Done!']);
  96. }
  97. }