/** * Saves an item into a txt file. The file content has a ini structure. * * @param String $file_name * The name of the file which the retrieved content will be saved into. */ private function save_retrieved_result($file_name, $file_extension = 'txt') { $file_full_path = $this->folders['dados'] . "/consulta/{$file_name}.{$file_extension}"; $item = json_decode($this->response_body_json, true); $item['http_code'] = $this->response_code; try { $writer = $this->container['ini_writer']; $writer->toFile($file_full_path, (array) $item, $this->container['ini_writer_lock']); tools::print_green("Consulta foi salva em %file_full_path", array('%file_full_path' => $file_full_path)); } catch (Exception $e) { $this->log("Nao foi possivel salvar a consulta em {$file_full_path}.", 'red'); } }
public function run() { $command_type = $this->command_type; $paramenters = $this->command_parameters; $arguments = $this->command_arguments; $throw_exception = FALSE; switch ($command_type) { case 'config': // Load config.ini. $root_folder = pathinfo(__DIR__); $root_folder = $this->root_folder = dirname($root_folder['dirname']); $config_file = "{$root_folder}/config.ini"; $config = $this->container['ini_reader']->fromFile($config_file); switch ($paramenters) { case 'ambiente': $this->check_command_arguments(); $valid_values = array('sandbox', 'producao'); $argument = $arguments[0]; if (!in_array($argument, $valid_values)) { throw new Exception(tools::print_yellow("{$argument} é inválido. Os valores esperados são producao ou sandbox.")); } $config['ambiente'] = $argument; break; case 'pastas dados': case 'pastas tmp': $this->check_command_arguments(); $argument = $arguments[0]; if (!is_dir($argument)) { throw new Exception(tools::print_yellow("A pasta {$argument} não existe.")); } if (!is_writable($argument)) { throw new Exception(tools::print_yellow("O usuário atual não tem permissão para gravar na pasta {$argument}.")); } $field_name = explode('pastas ', $paramenters); $field_name = $field_name[1]; $config['pastas'][$field_name] = $argument; break; case 'credenciais username': case 'credenciais password': $this->check_command_arguments(); $argument = $arguments[0]; $field_name = explode('credenciais ', $paramenters); $field_name = $field_name[1]; $config['credenciais'][$field_name] = $argument; break; case 'smtp username': case 'smtp password': $this->check_command_arguments(); $argument = $arguments[0]; $field_name = explode('smtp ', $paramenters); $field_name = $field_name[1]; if ($field_name == 'username' && !filter_var($argument, FILTER_VALIDATE_EMAIL)) { throw new Exception(tools::print_red("{$argument} precisa ser um endereço de email válido da gmail.")); } if ($field_name == 'username' && strpos($argument, '@gmail') === FALSE) { throw new Exception(tools::print_red("O {$argument} é um email válido mas NÃO é um email da gmail.")); } $config['smtp'][$field_name] = $argument; break; case 'notificar': $this->check_command_arguments(2); if (!filter_var($arguments[1], FILTER_VALIDATE_EMAIL)) { $email = $arguments[1]; throw new Exception(tools::print_red("{$email} é inválido.")); } $config['notificar'][$arguments[0]]['email'] = $arguments[1]; // Remove receiver. if (empty($arguments[1])) { unset($config['notificar'][$arguments[0]]); } break; case 'mostrar': $this->check_command_arguments(); $expected_parameters = $this->expected_parameters; $argument = $arguments[0]; if (!isset($config[$argument])) { throw new Exception(tools::print_yellow("'{$argument}' não foi reconhecido.")); } if (is_array($config[$argument])) { print_r($config[$argument]); } else { print $config[$argument] . PHP_EOL; } // Halt the execution. throw new Exception(); break; default: $throw_exception = TRUE; break; } if (!$throw_exception) { try { $writer = $this->container['ini_writer']; $writer->toFile($config_file, $config, $this->container['ini_writer_lock']); tools::print_green("O novo valor para %paramenters foi atualizado com sucesso.", array('%paramenters' => $paramenters)); } catch (Exception $e) { tools::print_red($e->getMessage); } } break; case 'sincronizar': $this->check_command_arguments(0); $nx = $this->container['nx']; $nx->scan_dados_folder(); break; case 'consultar': switch ($paramenters) { case 'produto product_id': case 'produto sku': case 'produto cod_produto_erp': $this->check_command_arguments(); $field_name = explode('produto ', $paramenters); $field_name = $field_name[1]; $method_name = "get_produto_by_{$field_name}"; $nx = $this->container['nx']; $nx->{$method_name}($arguments[0]); break; case 'pedido no': $this->check_command_arguments(); $nx = $this->container['nx']; $nx->get_pedido_by_number($arguments[0]); break; case 'cidades': $this->check_command_arguments(0); $nx = $this->container['nx']; $nx->get_cities(); break; default: $throw_exception = TRUE; break; } break; case 'resetar': switch ($paramenters) { case 'login': $this->check_command_arguments(0); $nx = $this->container['nx']; $nx->bootstrap_merchant_login(TRUE); break; default: $throw_exception = TRUE; break; } break; case 'testar': $this->check_command_arguments(0); $nx = $this->container['nx']; $nx->check(); break; } if ($throw_exception) { $msg = "O comando '{$command_type}' é válido mas o(s) parâmetro(s) é(são) inválido(s) ou incompleto(s)."; if (!empty($paramenters)) { $msg = "O comando '{$command_type}' é válido mas o(s) parâmetro(s) '{$paramenters}' está(ão) incompleto(s)."; } else { if (isset($arguments[0])) { $paramenter = $arguments[0]; $msg = "O comando '{$command_type}' é válido mas o parametro '{$paramenter}' não foi reconhecido."; } } throw new Exception(tools::print_yellow($msg)); } }