Skip to content

Commit

Permalink
Merge pull request #2 from ditointernet/NASA-48
Browse files Browse the repository at this point in the history
[NASA 48] Criar método unlink
  • Loading branch information
pdropaiva authored Feb 2, 2018
2 parents 5fdcd63 + 3ae65a2 commit 16d3d20
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
9 changes: 9 additions & 0 deletions examples/identify.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@
// 'id' => 10202840988483394,
// )
// )
// ));

// $dito->unlink(array(
// 'id' => 3123123321,
// 'accounts' => array(
// 'portal' => array(
// 'id' => 10202840988483394,
// )
// )
// ));
49 changes: 43 additions & 6 deletions lib/Dito.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,32 +151,65 @@ public function track($options = array()){
return $this->request('post', 'events', '/users/'. $id, $params);
}

public function link($options = array()) {
private function buildParamsLinkUnlink($options = array()) {
$credentials = $this->generateIDAndIDType($options);
$id = $credentials['id'];
$idType = $credentials['idType'];
$networkName = $credentials['networkName'];

$params = array('accounts' => json_encode($options['accounts']));
if(isset($idType)) $params['id_type'] = $idType;
if(isset($networkName)) $params['network_name'] = $networkName;

return [$id, $params];
}

private function validationParamsLinkUnlink($options = array()) {
$credentials = $this->generateIDAndIDType($options);
$id = $credentials['id'];

if(!isset($id)){
return array(
'error' => array('message' => 'Missing the user id param. See the available options here: http://developers.dito.com.br/docs/php-sdk')
);
}

$params = array('accounts' => json_encode($options['accounts']));
if(isset($idType)) $params['id_type'] = $idType;
if(isset($networkName)) $params['network_name'] = $networkName;
if(!isset($options['accounts']) || !is_array($options['accounts']) || count($options['accounts']) == 0) {
return array(
'error' => array('message' => 'Missing the user accounts params. See the available options here: http://developers.dito.com.br/docs/php-sdk')
);
}

return false;
}

public function link($options = array()) {
if($error = $this->validationParamsLinkUnlink($options)) {
return $error;
}

list($id, $params) = $this->buildParamsLinkUnlink($options);

return $this->request('post', 'login', "/users/{$id}/link", $params);
}

public function unlink($options = array()) {
if($error = $this->validationParamsLinkUnlink($options)) {
return $error;
}

list($id, $params) = $this->buildParamsLinkUnlink($options);

return $this->request('post', 'login', "/users/{$id}/unlink", $params);
}

private

function generateIDAndIDType($options = array()){
if(isset($options['reference'])){
$id = $options['reference'];
$idType = nil;
$networkName = nil;
$idType = null;
$networkName = null;
}
else if(isset($options['facebook_id'])){
$id = $options['facebook_id'];
Expand All @@ -197,6 +230,10 @@ function generateIDAndIDType($options = array()){
$id = $options['id'];
$idType = 'id';
$networkName = 'pt';
} else {
$id = null;
$idType = null;
$networkName = null;
}

return array('id' => $id, 'idType' => $idType, 'networkName' => $networkName);
Expand Down

0 comments on commit 16d3d20

Please sign in to comment.