<?php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
class CreateVacancyCommand extends Command
{
private $input;
private $output;
private $pais;
private $faker;
private $title = '';
private $description = '';
private $idOfCompany = 1;
private $vacante;
protected function configure()
{
$this
->setName('app:create-vacancy')
->setDescription('Creates new vacancies')
->setHelp("This command allows you to create new vacancies into test.jobomas.com")
->addOption('countryAbrev',
'c',
InputOption::VALUE_REQUIRED,
'Set the country Abreviation for the companies')
->addOption('numberOfCompany',
'e',
InputArgument::OPTIONAL,
'Set the company id to create a vacancy')
->addOption('title',
't',
InputArgument::OPTIONAL,
'Set the title for the vacancy')
->addOption('description',
'd',
InputArgument::OPTIONAL,
'Set the description for the vacancy');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$io = new SymfonyStyle($input, $output);
$this->idOfCompany = '';
$this->title = '';
$this->description = '';
$countryAbrev = $this->getCountryAbrev();
$company = $this->getCompany();
$noInteraction = $this->getNoInteractiveMode();
if (!empty($noInteraction)) {
$this->idOfCompany = $this->getIdCompany();
$this->title = $this->getTitleArg();
$this->description = $this->getDescriptionArg();
} else {
list($countryAbrev, $company, $this->idOfCompany, $this->title, $this->description)
= $this->getInteractiveMode($input, $output, $io);
}
if (empty($countryAbrev)) {
$io->caution('You must set the countryAbrev option');
return;
}
$results = [];
$pais = $this->getLocalInformation($countryAbrev);
$infoCompany = $this->getInformationCompanies($company);
$vac = $this->createVacante($pais, $infoCompany, $this->title, $this->description);
if ($vac) {
$results[] = 'Name of Vacante: ' . $this->title . ' Description: ' . $this->description;
}
if (!empty($results)) {
$io->success($results);
}
}
private function getLocalInformation($countryAbrev)
{
$this->pais = PaisEmpresa::find_by_abreviatura($countryAbrev);
if (!empty($this->pais)) {
$lang = strtolower($this->pais->lang);
$lang = $lang == 'sp' ? 'es' : $lang;
$lang = $lang == 'po' ? 'pt' : $lang;
$lang = $lang . '_' . strtoupper($lang);
$this->faker = Faker\Factory::create($lang);
} else {
throw new Exception("Invalid country Abreviation", 1);
}
return $this->pais;
}
private function getInformationCompanies($company)
{
$this->idOfCompany = Empresa::find_by_id($company);
if (!empty($this->idOfCompany)) {
$this->vacante = Vacante::nuevoRegistro();
} else {
throw new Exception("Company Non-existent", 1);
}
return $this->idOfCompany;
}
private function getInteractiveMode($input, $output, $io)
{
$helper = $this->getHelper('question');
$io->title('<options=bold;fg=blue>PAIS DE LA INFORMACION A PROPORCIONAR</>');
$countryAbrevQuestion = new Question('Set the country for the company[mx]: ',
'mx');
$countryAbrev = trim($helper->ask($input, $output, $countryAbrevQuestion));
$io->title('<options=bold;fg=blue>CREACION DE VACANTE</>');
$idOfCompanyQuestion = new Question('Set Id of Company to create Vacancy: ',
1, '/^[0-9]{1,}$/');
$idOfCompany = $helper->ask($input, $output, $idOfCompanyQuestion);
$titleQuestion = new Question('Set a title for the vacancy: ', '');
$title = $helper->ask($input, $output, $titleQuestion);
$descriptionQuestion = new Question('Set a description for the vacancy: ', '');
$description = $helper->ask($input, $output, $descriptionQuestion);
return [$countryAbrev, $idOfCompany, $title, $description];
}
private function createVacante()
{
//$this->vacante = new Vacante();
$this->title = trim($this->title);
$this->description = trim($this->description);
$numero = range(1, 10);
$edad = range(18, 60);
$this->vacante->nombre_del_puesto = !empty($this->title) ? $this->title : $this->faker->jobTitle;
$this->vacante->idempresas = $this->idOfCompany;
$this->vacante->idpaises = $this->pais->id;
$this->vacante->empresa = Empresa::find_by_id('nombre');
//$vacante->idprovincia = PaisEmpresa::getProvinciasByIdPais($this->pais->id);
$this->vacante->vacantes = $numero[array_rand($numero)];
$this->vacante->descripcion_del_aviso = !empty($this->description) ? $this->description : $this->faker->realText;
$this->vacante->email = $this->faker->email;
$this->vacante->fecha_de_publicacion = date('Y-m-d');
$this->vacante->edad_requerida = $edad[array_rand($edad)];
$this->vacante->lugar_de_residencia_requerido = utf8_decode($this->faker->city);
$this->vacante->orden = utf8_decode($this->faker->word);
try {
$this->vacante->save(false);
$success = $this->vacante;
} catch (Exception $e) {
trigger_error($e->getMessage(), E_USER_ERROR);
}
return $success;
}
private function getIdCompany()
{
return $this->input->getOption('numberOfCompany');
}
private function getNoInteractiveMode()
{
return $this->input->getOption("no-interaction");
}
private function getCountryAbrev()
{
return $this->input->getOption('countryAbrev');
}
private function getCompany()
{
return $this->input->getOption('numberOfCompany');
}
private function getTitleArg()
{
return $this->input->getOption('title');
}
private function getDescriptionArg()
{
return $this->input->getOption('description');
}
}