Command/UpdateCorrectAddressDataCommand.php
protected function execute(InputInterface $input, OutputInterface $output)
    {
        try {
            $output->writeln('<info>Starting Expirian data update.</info>');

            /** @var IntegrationHelper $integrationHelper */
            $integrationHelper = $this->getContainer()->get('mautic.helper.integration');
            $enhancerHelper    = new EnhancerHelper($integrationHelper);
            /** @var \MauticPlugin\MauticEnhancerBundle\Helper\EnhancerHelper $correctAddress */
            $correctAddress = $enhancerHelper->getIntegration('CorrectAddress');
            $settings       = $correctAddress->getIntegrationSettings()->getFeatureSettings();
            $keys           = $correctAddress->getKeys();

            if (function_exists('ssh2_connect')) {
                $sconn = call_user_func('ssh2_connect', $settings[CAI::CA_REMOTE_HOST]);
                call_user_func('ssh2_auth_password', $sconn, $keys[CAI::CA_REMOTE_USER], $keys[CAI::CA_REMOTE_PSWD]);
                $sftp = call_user_func('ssh2_sftp', $sconn);
            } else {
                throw new \Exception(
                    'Required ssh2 extension is not installed',
                    -1
                );
            }
            $output->writeln('<info>SFTP connection established, downloading data file</info>');

            $source = 'ssh2.sftp://'.intval($sftp).$settings[CAI::CA_REMOTE_PATH].'/'.$settings[CAI::CA_REMOTE_FILE];

            $buffer = tempnam(sys_get_temp_dir(), 'ca_'.\date('Y-m-d'));
            if (file_exists($buffer)) {
                unlink($buffer);
            }
            $dest = $buffer.'.zip';
            $rfp  = fopen($source, 'r');
            $wfp  = fopen($dest, 'w');

            $reads = 0;
            do {
                if (!fwrite($wfp, fread($rfp, 8388608))) {
                    break;
                }
                ++$reads;
                if (0 === ($reads % 100)) {
                    $output->write('.');
                }
            } while (true);
            $output->writeln('<info>Copied data archive to '.$dest.' on local filesystem.</info>');

            //extract the new files
            $extractor = new ZipArchive();
            $extractor->open($dest, ZipArchive::CHECKCONS);
            $extractor->extractTo($buffer);
            if ($extractor->close() && is_dir($buffer)) {
                unlink($dest);
                $output->writeln('<info>Archive extracted to '.$buffer.'.</info>');

                if (is_dir($settings[CAI::CA_CORRECTA_DATA].'_bak')) {
                    $this->cleanDir($settings[CAI::CA_CORRECTA_DATA].'_bak');
                    $output->writeln('<info>'.$settings[CAI::CA_CORRECTA_DATA].'_bak cleaned.</info>');
                }
                rename($settings[CAI::CA_CORRECTA_DATA], $settings[CAI::CA_CORRECTA_DATA].'_bak');

                if (rename($buffer, $settings[CAI::CA_CORRECTA_DATA])) {
                    $output->writeln('<info>Expirian data update complete.</info>');

                    return 0;
                } else {
                    $output->writeln(
                        '<error>Failed to move '.$buffer.' to '.$settings[CAI::CA_CORRECTA_DATA].'</error>'
                    );

                    return 1;
                }
            } else {
                $output->writeln('<error>Failed to unzip '.$dest.' to '.$buffer.'</error>');

                return 1;
            }
        } catch (\Exception $e) {
            $output->writeln('<error>Failed to update data: '.$e->getMessage().'</error>');
            $output->write($e->getTraceAsString());
            $output->writeln('');

            return $e->getCode();
        }
    }
protected function execute(InputInterface $input, OutputInterface $output)
    {
        try {
            $output->writeln('<info>Starting Expirian data update.</info>');

            /** @var IntegrationHelper $integrationHelper */
            $integrationHelper = $this->getContainer()->get('mautic.helper.integration');
            $enhancerHelper    = new EnhancerHelper($integrationHelper);
            /** @var \MauticPlugin\MauticEnhancerBundle\Helper\EnhancerHelper $correctAddress */
            $correctAddress = $enhancerHelper->getIntegration('CorrectAddress');
            $settings       = $correctAddress->getIntegrationSettings()->getFeatureSettings();
            $keys           = $correctAddress->getKeys();

            if (function_exists('ssh2_connect')) {
                $sconn = call_user_func('ssh2_connect', $settings[CAI::CA_REMOTE_HOST]);
                call_user_func('ssh2_auth_password', $sconn, $keys[CAI::CA_REMOTE_USER], $keys[CAI::CA_REMOTE_PSWD]);
                $sftp = call_user_func('ssh2_sftp', $sconn);
            } else {
                throw new \Exception(
                    'Required ssh2 extension is not installed',
                    -1
                );
            }
            $output->writeln('<info>SFTP connection established, downloading data file</info>');

            $source = 'ssh2.sftp://'.intval($sftp).$settings[CAI::CA_REMOTE_PATH].'/'.$settings[CAI::CA_REMOTE_FILE];

            $buffer = tempnam(sys_get_temp_dir(), 'ca_'.\date('Y-m-d'));
            if (file_exists($buffer)) {
                unlink($buffer);
            }
            $dest = $buffer.'.zip';
            $rfp  = fopen($source, 'r');
            $wfp  = fopen($dest, 'w');

            $reads = 0;
            do {
                if (!fwrite($wfp, fread($rfp, 8388608))) {
                    break;
                }
                ++$reads;
                if (0 === ($reads % 100)) {
                    $output->write('.');
                }
            } while (true);
            $output->writeln('<info>Copied data archive to '.$dest.' on local filesystem.</info>');

            //extract the new files
            $extractor = new ZipArchive();
            $extractor->open($dest, ZipArchive::CHECKCONS);
            $extractor->extractTo($buffer);
            if ($extractor->close() && is_dir($buffer)) {
                unlink($dest);
                $output->writeln('<info>Archive extracted to '.$buffer.'.</info>');

                if (is_dir($settings[CAI::CA_CORRECTA_DATA].'_bak')) {
                    $this->cleanDir($settings[CAI::CA_CORRECTA_DATA].'_bak');
                    $output->writeln('<info>'.$settings[CAI::CA_CORRECTA_DATA].'_bak cleaned.</info>');
                }
                rename($settings[CAI::CA_CORRECTA_DATA], $settings[CAI::CA_CORRECTA_DATA].'_bak');

                if (rename($buffer, $settings[CAI::CA_CORRECTA_DATA])) {
                    $output->writeln('<info>Expirian data update complete.</info>');

                    return 0;
                } else {
                    $output->writeln(
                        '<error>Failed to move '.$buffer.' to '.$settings[CAI::CA_CORRECTA_DATA].'</error>'
                    );

                    return 1;
                }
            } else {
                $output->writeln('<error>Failed to unzip '.$dest.' to '.$buffer.'</error>');

                return 1;
            }
        } catch (\Exception $e) {
            $output->writeln('<error>Failed to update data: '.$e->getMessage().'</error>');
            $output->write($e->getTraceAsString());
            $output->writeln('');

            return $e->getCode();
        }
    }
@param InputInterface  $input
@param OutputInterface $output

@return int