diff --git a/.vscode/settings.json b/.vscode/settings.json index 87684c12..8f66a910 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,6 @@ { - "files.encoding": "iso88591" + "files.encoding": "iso88591", + "editor.tabSize": 4, + "editor.insertSpaces": true, + "editor.detectIndentation": false } \ No newline at end of file diff --git a/crm/Utils.php b/crm/Utils.php new file mode 100644 index 00000000..51a2fd83 --- /dev/null +++ b/crm/Utils.php @@ -0,0 +1,13 @@ +generateToken()) Utils::printToLog("Token do Salesforce gerado com sucesso.", __LINE__); + else Utils::printToLog("ERRO! Falha ao gerar Token do Salesforce.", __LINE__); + } + + /* + * Gera o token de autenticacao que ira ser colocado no cabecalho das requisicoes, e armazena na variavel $this->token + */ + private function generateToken() + { + $this->url = CONF_ACCESS_TOKEN_URL; + $this->setMethod('services/oauth2/token'); + $this->setRequest('POST'); + $this->contentType = 'application/x-www-form-urlencoded'; + $this->params = array( + 'client_id' => $this->clientId, + 'client_secret' => $this->clientSecret, + 'redirect_uri' => CONF_ENDPOINT, + 'password' => $this->password, + 'username' => $this->username, + 'grant_type' => $this->grantType + ); + + Utils::printToLog('Corpo da requisicao de gerar token: ' . print_r($this->params, true), __LINE__); + + $data = $this->setParams(); + + if (substr($this->responseCode, 0, 1) == '2') { + $this->setToken($data['access_token']); + return true; + } + + return false; + } + + /* + Recupera do Salesforce o objeto do contrato cujo id foi passado no argumento do metodo + */ + public function getContract(string $id) + { + } + + /** + * Parametriza o method utilizado para a consulta. + * + * @param type $method + */ + private function setMethod($method) + { + $this->method = $method; + } + + /** + * Escreve a query para ser passada ao curl + * + * @param string $query + */ + private function setQuery($query) + { + return $this->query .= $query; + } + + /** + * retorna a string pronta da query do curl e limpa a variavel. + * + * @return string $query + */ + private function getQuery() + { + $query = $this->query; + unset($this->query); + return $query; + } + + /** + * Constroi de forma dinamica a string para realizar a execucao do Curl + * + * @void + */ + private function curl() + { + $this->curl = curl_init(); + + curl_setopt_array($this->curl, array( + CURLOPT_URL => $this->url . $this->method, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 60, + CURLOPT_CUSTOMREQUEST => $this->request, + CURLOPT_HTTPHEADER => array( + 'Authorization:' . $this->token, + 'Content-Type:' . $this->contentType ? $this->contentType : 'application/json' + ) + )); + + + if ($this->setRequest() == "POST") { + + Utils::printToLog('body: ' . $this->query, __LINE__); + + curl_setopt_array( + $this->curl, + array( + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $this->getQuery() + ) + ); + } + unset($this->contentType); + $this->url = CONF_ENDPOINT; + } + + /** + * Informa o tipo de requisicao que sera feita pela cURL + * @param bool $request + */ + private function setRequest($request = null) + { + if (!$this->request || $request) { + $this->request = (strtoupper($request) == 'POST' ? strtoupper($request) : "GET"); + } + + return $this->request; + } + + + /** + * Retorna o token a ser utilizado + * @param string $this->token + */ + /* private function getToken() + { + return $this->token; + } + */ + + /** + * Define o token a ser utilizado + * @param string $this->token + */ + private function setToken($token) + { + $this->token = 'Bearer ' . $token; + } + + /** + * Recebe array em forma de indice e valor + * + * @example array("qtype" => 'test_api', "query" => '123', "oper" => '=') + * + * @param type $token + * @debug_track function debug_backtrace() + */ + private function setParams() + { + //decides if body will have url encoded content or json + if (!$this->contentType) { + $this->query = $this->params; + } else { + $this->query = http_build_query($this->params); + } + + unset($this->params); + $data = $this->exec(); + return $this->response($data); + } + + /** + * Recebe as informacoes e realiza a execucao da API + * + * @void + */ + private function exec() + { + $this->curl(); + $response = curl_exec($this->curl); + Utils::printToLog("resposta da requisicao: " . print_r(curl_getinfo($this->curl), true), __LINE__); + $this->responseCode = curl_getinfo($this->curl)['http_code']; + if (curl_errno($this->curl)) { + curl_close($this->curl); + return false; + } + + curl_close($this->curl); + return $response; + } + + /** + * Prepara dos dados para serem transmitidos para o method a serem retornados a + * integracao. + * + * @return array + */ + private function response($data) + { + Utils::printToLog("Response code: " . $this->responseCode . "\nReponse API: " . print_r(json_decode($data, true), true), __LINE__); + + if ($data) { + return json_decode($data, true); + } else { + return false; + } + } +} \ No newline at end of file diff --git a/crm/controller/ContractController.php b/crm/controller/ContractController.php new file mode 100644 index 00000000..57254bc9 --- /dev/null +++ b/crm/controller/ContractController.php @@ -0,0 +1,59 @@ +setNome($d['nome']); + $usuario->setSobrenome($d['sobrenome']); + $usuario->setIdade($d['idade']); + $usuario->setSexo($d['sexo']); + + $usuariodao->create($usuario); + + header("Location: ../../"); +} +// se a requisição for editar +else if (isset($_POST['editar'])) { + + $usuario->setNome($d['nome']); + $usuario->setSobrenome($d['sobrenome']); + $usuario->setIdade($d['idade']); + $usuario->setSexo($d['sexo']); + $usuario->setId($d['id']); + + $usuariodao->update($usuario); + + header("Location: ../../"); +} +// se a requisição for deletar +else if (isset($_GET['del'])) { + + $usuario->setId($_GET['del']); + + $usuariodao->delete($usuario); + + header("Location: ../../"); +} else { + header("Location: ../../"); +} */ \ No newline at end of file diff --git a/crm/service/ContractService.php b/crm/service/ContractService.php new file mode 100644 index 00000000..f4128d47 --- /dev/null +++ b/crm/service/ContractService.php @@ -0,0 +1,11 @@ +getContract($id); + } +} \ No newline at end of file diff --git a/crm/tests/testSalesforceConsumer.php b/crm/tests/testSalesforceConsumer.php new file mode 100644 index 00000000..591fbef4 --- /dev/null +++ b/crm/tests/testSalesforceConsumer.php @@ -0,0 +1,11 @@ +