Example #1
0
 /**
  * Adds a syncronization attempt tag into the item data array.
  *
  * @param Array $item_data
  *   See scan_dados_item_data().
  *
  * @param Bool $action
  *   Which action has been performed. CREATE/UPDATE or FAIL.
  *
  * @param String $service
  *   The service name.
  */
 private function set_sync_attempt_tag(&$item_data, $action, $service = 'produto')
 {
     $prime_id_field = $this->config['servicos'][$service]['prime_id_field'];
     switch ($action) {
         case nx::SYNC_TAG_ACTION_CREATE:
             $prime_id_field_value = $item_data[$prime_id_field];
             $msg = "Item {$prime_id_field}={$prime_id_field_value} foi CRIADO com sucesso no servico {$service}.";
             tools::print_green($msg);
             break;
         case nx::SYNC_TAG_ACTION_UPDATE:
             $prime_id_field_value = $item_data[$prime_id_field];
             $msg = "Item {$prime_id_field}={$prime_id_field_value} foi ATUALIZADO com sucesso no servico {$service}.";
             tools::print_green($msg);
             break;
         case nx::SYNC_TAG_ACTION_ITEM_DATA_EMPTY:
             $msg = "O arquivo dados estava vazio.";
             tools::print_yellow($msg);
             break;
         case nx::SYNC_TAG_ACTION_FAIL:
             $msg = "A sincronizacao falhou.";
             tools::print_yellow($msg);
             break;
         default:
             $msg = "O valor do parametro \$action eh invalido. Entre em contado com a NortaoX.";
             $this->log($msg);
             throw new \InvalidArgumentException(tools::print_red($msg));
     }
     $attempts = 1;
     if (isset($item_data['-sincronizacao-']['tentativas'])) {
         $attempts += $item_data['-sincronizacao-']['tentativas'];
     }
     $date_time = $this->container['date_time'];
     $date_time = $date_time->format("Y-m-d H:i:s");
     $item_data['-sincronizacao-'] = array('tentativas' => $attempts, 'hora_ultima_tentativa' => $date_time, 'ultima_msg' => $msg);
 }
Example #2
0
 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));
     }
 }