123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?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 voip data from Flowdat 2')
- ->setHelp('Imports Voip 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();
- // Detach the object from the entity manager
- $this->em->detach($object);
- }
- } else {
- $errors[] = "<error>[{$row['id']}] Unable to find a Cablemodem with ID {$row['cablemodem_id']}. Element has been skipped.</error>";
- }
- $progressBar->advance();
- }
- $output->writeln(['']);
- $output->writeln($errors);
- $output->writeln(['', 'Done!']);
- }
- }
|