/** * @param \PhpSigep\Model\AbstractModel|\PhpSigep\Model\SolicitaEtiquetas $params * * @throws \PhpSigep\Services\Exception * @throws InvalidArgument * @return BuscaClienteResult */ public function execute(AbstractModel $params) { if (!$params instanceof \PhpSigep\Model\AccessData) { throw new InvalidArgument(); } $soapArgs = array('idContrato' => $params->getNumeroContrato(), 'idCartaoPostagem' => $params->getCartaoPostagem(), 'usuario' => $params->getUsuario(), 'senha' => $params->getSenha()); $result = new Result(); try { if (!$params->getUsuario() || !$params->getSenha() || !$params->getNumeroContrato() || !$params->getCartaoPostagem()) { throw new Exception('Para usar este serviço você precisa setar o nome de usuário, a senha, o numero ' . 'do contrato e o número do cartão de postagem.'); } $r = SoapClientFactory::getSoapClient()->buscaCliente($soapArgs); if (!$r || !is_object($r) || !isset($r->return) || $r instanceof \SoapFault) { if ($r instanceof \SoapFault) { throw $r; } throw new \Exception('Erro ao consultar os dados do cliente. Retorno: "' . $r . '"'); } $result->setResult(new BuscaClienteResult((array) $r->return)); } catch (\Exception $e) { if ($e instanceof \SoapFault) { $result->setIsSoapFault(true); $result->setErrorCode($e->getCode()); $result->setErrorMsg(SoapClientFactory::convertEncoding($e->getMessage())); } else { $result->setErrorCode($e->getCode()); $result->setErrorMsg($e->getMessage()); } } return $result; }
/** * @param \PhpSigep\Model\AbstractModel|\PhpSigep\Model\SolicitaEtiquetas $params * * @throws \PhpSigep\Services\Exception * @throws InvalidArgument * @return Result<Etiqueta[]> */ public function execute(AbstractModel $params) { if (!$params instanceof \PhpSigep\Model\SolicitaEtiquetas) { throw new InvalidArgument(); } $soapArgs = array('tipoDestinatario' => 'C', 'identificador' => $params->getAccessData()->getCnpjEmpresa(), 'idServico' => $params->getServicoDePostagem()->getIdServico(), 'qtdEtiquetas' => 1, 'usuario' => $params->getAccessData()->getUsuario(), 'senha' => $params->getAccessData()->getSenha()); $result = new Result(); try { if (!$params->getAccessData() || !$params->getAccessData()->getUsuario() || !$params->getAccessData()->getSenha()) { throw new Exception('Para usar este serviço você precisa setar o nome de usuário e senha.'); } $etiquetasReservadas = array(); for ($i = 0; $i < $params->getQtdEtiquetas(); $i++) { $r = SoapClientFactory::getSoapClient()->solicitaEtiquetas($soapArgs); if (class_exists('\\StaLib_Logger')) { \StaLib_Logger::log('Retorno SIGEP solicitar etiquetas: ' . print_r($r, true)); } if ($r && is_object($r) && isset($r->return) && !$r instanceof \SoapFault) { $r = explode(',', $r->return); // $etiquetasReservadas[] = str_replace(' ', '', $r[0]); $etiqueta = new Etiqueta(); $etiqueta->setEtiquetaSemDv($r[0]); $etiquetasReservadas[] = $etiqueta; } else { if ($r instanceof \SoapFault) { throw $r; } throw new \Exception('Não foi possível obter as etiquetas solicitadas. Retorno: "' . $r . '"'); } } $result->setResult($etiquetasReservadas); } catch (\Exception $e) { if ($e instanceof \SoapFault) { $result->setIsSoapFault(true); $result->setErrorCode($e->getCode()); $result->setErrorMsg("Resposta do Correios: " . SoapClientFactory::convertEncoding($e->getMessage())); } else { $result->setErrorCode($e->getCode()); $result->setErrorMsg($e->getMessage()); } } return $result; }
/** * @param AbstractModel|AbstractModel[]|\SoapFault $result * @throws InvalidArgument * @return $this; */ public function setResult($result) { if ($result instanceof \SoapFault) { $this->setIsSoapFault(true); $this->setErrorCode($result->getCode()); $this->setErrorMsg(SoapClientFactory::convertEncoding($result->getMessage())); $this->result = null; $this->setSoapFault($result); } else { $piece = $result; if (is_array($result)) { if (count($result)) { $piece = reset($result); } else { $piece = null; } } if ($piece !== null && !$piece instanceof AbstractModel && !$piece instanceof \SoapFault) { throw new InvalidArgument('O resultado deve ser uma instância de PhpSigep\\Model\\AbstractModel ou um ' . 'array de PhpSigep\\Model\\AbstractModel ou uma instância de \\SoapFault.'); } $this->result = $result; } return $this; }