Skip to content

Instantly share code, notes, and snippets.

@vitorvargasdev
Created June 1, 2020 17:37
Show Gist options
  • Save vitorvargasdev/1101a89253d75c4447c156d8756db71c to your computer and use it in GitHub Desktop.
Save vitorvargasdev/1101a89253d75c4447c156d8756db71c to your computer and use it in GitHub Desktop.
<?php
namespace App\Domains\Asaas;
use \App\Domains\Asaas\Connection;
use \App\Domains\Asaas\Exceptions\ClienteException;
class Cliente
{
private $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
}
public function index()
{
return $this->connection->get('/customers');
}
public function filter($filter)
{
return $this->connection->get('/customers?'.$filter);
}
public function create($data, $json)
{
$cliente = $this->setCliente($data);
if ($json === true) {
return $this->connection->post('/customers', [$cliente], true);
} else {
return $this->connection->post('/customers', ['form_params' => $cliente], false);
}
}
public function update($id, $data, $json)
{
$cliente = $this->setCliente($data);
$cliente = $this->setCliente($data);
if ($json === true) {
return $this->connection->post('/customers', [$cliente], true);
} else {
return $this->connection->post('/customers/'.$id, ['form_params' => $cliente], false);
}
}
public function setCliente($cliente)
{
try {
if ( ! $this->cliente_valid($cliente) ) {
throw ClienteException::invalidClient();
}
$clienteArr = array(
'name' => '',
'cpfCnpj' => '',
'email' => '',
'phone' => '',
'mobilePhone' => '',
'address' => '',
'addressNumber' => '',
'complement' => '',
'province' => '',
'postalCode' => '',
'externalReference' => '',
'notificationDisabled' => '',
'additionalEmails' => '',
);
$clienteArr = array_merge($clienteArr, $cliente);
return $clienteArr;
} catch (\Exception $e) {
return 'Erro ao definir o cliente. - ' . $e->getMessage();
}
}
public function cliente_valid($cliente)
{
return ! ( empty($cliente['name']) OR empty($cliente['cpfCnpj']) OR empty($cliente['email']) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment