/**
  * Execute the command.
  *
  * @param InputInterface $input
  * @param OutputInterface $output
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // We don't need moodle for getting value.
     if ($value = $input->getOption('value')) {
         switch ($value) {
             case 'version':
                 $output->writeln(\moodlehq\behat_generator\util::get_tool_version());
                 break;
             case 'moodlepath':
                 $output->writeln(util::get_moodle_path());
                 break;
             case 'datapath':
                 $output->writeln(util::get_data_path());
                 break;
             default:
                 $output->writeln('<error>Not a valid option.</error><info> Should be wither version|moodlepath|datapath</info>');
                 return 1;
                 break;
         }
         return 0;
     }
     // Include moodle config.
     define('PERFORMANCE_SITE_GENERATOR', true);
     define('CLI_SCRIPT', true);
     define('NO_OUTPUT_BUFFERING', true);
     define('IGNORE_COMPONENT_CACHE', true);
     // Load moodle config and all classes.
     if (!($moodlepath = util::get_moodle_path())) {
         util::performance_exception("Moodlepath should have been set by now.");
     }
     // Autoload files and ensure we load moodle config, as we will be using moodle code for behat context.
     require_once $moodlepath . '/config.php';
     require_once __DIR__ . '/inc.php';
     raise_memory_limit(MEMORY_HUGE);
     $status = false;
     // Do action.
     $sitedatagenerator = new \moodlehq\behat_generator\generator();
     if ($input->getOption('drop')) {
         // Don't check for any option, just try drop site..
         $force = $input->getOption('force') ? true : false;
         $status = $sitedatagenerator->run_drop($output, $force);
     } else {
         if ($input->getOption('dropsite')) {
             // Don't check for any option, just try drop site..
             $force = $input->getOption('force') ? true : false;
             $status = $sitedatagenerator->run_dropsite($output, $force);
         }
     }
     // Check if site enable/disable is needed.
     if ($input->getOption('enable')) {
         $status = $sitedatagenerator->run_enable($output, $input->getOption('enable'));
     } else {
         if ($input->getOption('disable')) {
             $status = $sitedatagenerator->run_disable($output);
         }
     }
     if ($input->getOption('install')) {
         $status = $sitedatagenerator->run_install($output);
     }
     // Check if testdata needs to be generated.
     if ($input->getOption('testdata')) {
         $status = $sitedatagenerator->run_testdata($output, $input->getOption('testdata'), $input->getOption('force'));
     }
     // Finally, check if backup/restore is needed.
     if ($input->getOption('backup')) {
         $status = $sitedatagenerator->run_backup($output, $input->getOption('backup'));
     } else {
         if ($input->getOption('restore')) {
             $status = $sitedatagenerator->run_restore($output, $input->getOption('restore'));
         }
     }
     if ($status === false) {
         $output->write($this->getHelp(), true);
     }
     return $status;
 }
 /**
  * Generate site data.
  *
  * @param OutputInterface $output output interface for writting.
  * @param string $sitesize Site size.
  * @param bool $force try generating data, even if there is data present.
  * @return int|bool error code, false to show help message, for invalid option passed.
  */
 public function run_testdata(OutputInterface $output, $sitesize, $force = false)
 {
     if (!util::get_moodle_path() || !util::get_data_path()) {
         $output->writeln("<error>Moodelpath and datapath not set, check behatgenerator.json</error> ");
         return 1;
     }
     // Check site status and then perform action.
     $sitestatus = installer::get_site_status($output);
     // If site is not configured then just exit with error.
     if ($sitestatus == installer::SITE_ERROR_CONFIG) {
         $output->writeln("<error>Error: Ensure you set \$CFG->* vars in config.php " . "and you ran vendor/bin/moodle_behat_generator --install</error>");
         return 1;
     }
     if ($sitestatus !== installer::SITE_INSTALLED && !$force) {
         echo "No site installed, so can't generate performance site data." . PHP_EOL;
         echo "Run \n   - vendor/bin/moodle_behat_generator --install" . PHP_EOL;
         return 1;
     }
     $sitesize = $this->check_valid_site_size($sitesize);
     if (empty($this->execute('enable', $sitesize))) {
         // Don't generate site if already installed.
         if ($sitedata = get_config('core', 'performancesitedata')) {
             if ($force) {
                 $this->execute('enable', $sitesize);
                 set_config('performancesitedata', $sitesize . ',' . util::get_tool_version());
             } else {
                 echo "Site data is already generated for site size: " . $sitedata . PHP_EOL;
                 echo "Drop site and try again:\n - vendor/bin/moodle_behat_generator --drop" . PHP_EOL;
                 exit(1);
             }
         } else {
             set_config('performancesitedata', $sitesize . ',' . util::get_tool_version());
         }
         $this->generate();
     } else {
         return 1;
     }
     // Success.
     return 0;
 }