Skip to content

Commit

Permalink
Add tacking to improve the library use
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanalemunioz committed Nov 29, 2022
1 parent 1d99115 commit 72f5f70
Show file tree
Hide file tree
Showing 18 changed files with 1,787 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/.DS_Store
40 changes: 40 additions & 0 deletions src/Afip.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @version 0.6
**/

include_once __DIR__.'/libs/mixpanel/Mixpanel.php';

class Afip {
/**
* File name for the WSDL corresponding to WSAA
Expand Down Expand Up @@ -88,16 +90,26 @@ function __construct($options)
{
ini_set("soap.wsdl_cache_enabled", "0");

// Create the Mixpanel class instance
$this->mixpanel = Mixpanel::getInstance("e87ee11c8cc288e5c5dc213c4d957c7e");

$this->mixpanel->register('afip_sdk_library', 'php');

if (isset($_SERVER['REMOTE_ADDR'])) {
$this->mixpanel->register('ip', $_SERVER['REMOTE_ADDR']);
}

if (!isset($options['CUIT'])) {
throw new Exception("CUIT field is required in options array");
} else {
$this->CUIT = $options['CUIT'];
$this->mixpanel->register('distinct_id', $options['CUIT']);
}

if (!isset($options['production'])) {
$options['production'] = FALSE;
}
$this->mixpanel->register('production', $options['production']);

if (!isset($options['passphrase'])) {
$options['passphrase'] = 'xxxxx';
Expand Down Expand Up @@ -147,6 +159,10 @@ function __construct($options)
throw new Exception("Failed to open ".$this->PRIVATEKEY."\n", 2);
if (!file_exists($this->WSAA_WSDL))
throw new Exception("Failed to open ".$this->WSAA_WSDL."\n", 3);

try {
$this->mixpanel->track("initialized", $options);
} catch (Exception $e) {}
}

/**
Expand Down Expand Up @@ -264,6 +280,28 @@ public function WebService($service, $options)
return new AfipWebService($this, $options);
}

/**
* Track SDK usage
*
* @param string $web_service ID of the web service used
* @param string $operation SOAP operation called
* @param array $params Parameters for the ws
**/
public function TrackUsage($web_service, $operation, $params = array())
{
$options = array();

if ($web_service === 'wsfe' && $operation === 'FECAESolicitar') {
if (isset($params['FeCAEReq']) && isset($params['FeCAEReq']['FeCabReq']) && isset($params['FeCAEReq']['FeCabReq']['CbteTipo'])) {
$options['CbteTipo'] = $params['FeCAEReq']['FeCabReq']['CbteTipo'];
}
}

try {
$this->mixpanel->track($web_service.'.'.$operation, $options);
} catch (Exception $e) {}
}

public function __get($property)
{
if (in_array($property, $this->implemented_ws)) {
Expand Down Expand Up @@ -466,6 +504,8 @@ public function ExecuteRequest($operation, $params = array())
$results = $this->soap_client->{$operation}($params);

$this->_CheckErrors($operation, $results);

$this->afip->TrackUsage($this->options['service'], $operation, $params);

return $results;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Class/ElectronicBilling.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ public function FormatDate($date)
**/
public function ExecuteRequest($operation, $params = array())
{
$this->options = array('service' => 'wsfe');

$params = array_replace($this->GetWSInitialRequest($operation), $params);

$results = parent::ExecuteRequest($operation, $params);
Expand Down
2 changes: 2 additions & 0 deletions src/Class/RegisterInscriptionProof.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public function GetTaxpayersDetails($identifiers)
**/
public function ExecuteRequest($operation, $params = array())
{
$this->options = array('service' => 'ws_sr_padron_a5');

$results = parent::ExecuteRequest($operation, $params);

return $results->{
Expand Down
2 changes: 2 additions & 0 deletions src/Class/RegisterScopeFive.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public function GetTaxpayersDetails($identifiers)
**/
public function ExecuteRequest($operation, $params = array())
{
$this->options = array('service' => 'ws_sr_padron_a5');

$results = parent::ExecuteRequest($operation, $params);

return $results->{
Expand Down
2 changes: 2 additions & 0 deletions src/Class/RegisterScopeFour.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function GetTaxpayerDetails($identifier)
**/
public function ExecuteRequest($operation, $params = array())
{
$this->options = array('service' => 'ws_sr_padron_a4');

$results = parent::ExecuteRequest($operation, $params);

return $results->{$operation == 'getPersona' ? 'personaReturn' : 'return'};
Expand Down
2 changes: 2 additions & 0 deletions src/Class/RegisterScopeTen.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public function GetTaxpayerDetails($identifier)
**/
public function ExecuteRequest($operation, $params = array())
{
$this->options = array('service' => 'ws_sr_padron_a10');

$results = parent::ExecuteRequest($operation, $params);

return $results->{$operation == 'getPersona' ? 'personaReturn' : 'return'};
Expand Down
2 changes: 2 additions & 0 deletions src/Class/RegisterScopeThirteen.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public function GetTaxIDByDocument($documentNumber)
**/
public function ExecuteRequest($operation, $params = array())
{
$this->options = array('service' => 'ws_sr_padron_a13');

$results = parent::ExecuteRequest($operation, $params);

return $results->{$operation == 'getPersona' ? 'personaReturn' :
Expand Down
66 changes: 66 additions & 0 deletions src/libs/mixpanel/Base/MixpanelBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* This a Base class which all Mixpanel classes extend from to provide some very basic
* debugging and logging functionality. It also serves to persist $_options across the library.
*
*/
class Base_MixpanelBase {


/**
* Default options that can be overridden via the $options constructor arg
* @var array
*/
private $_defaults = array(
"max_batch_size" => 50, // the max batch size Mixpanel will accept is 50,
"max_queue_size" => 1000, // the max num of items to hold in memory before flushing
"debug" => false, // enable/disable debug mode
"consumer" => "curl", // which consumer to use
"host" => "api.mixpanel.com", // the host name for api calls
"events_endpoint" => "/track", // host relative endpoint for events
"people_endpoint" => "/engage", // host relative endpoint for people updates
"groups_endpoint" => "/groups", // host relative endpoint for groups updates
"use_ssl" => true, // use ssl when available
"error_callback" => null // callback to use on consumption failures
);


/**
* An array of options to be used by the Mixpanel library.
* @var array
*/
protected $_options = array();


/**
* Construct a new MixpanelBase object and merge custom options with defaults
* @param array $options
*/
public function __construct($options = array()) {
$options = array_merge($this->_defaults, $options);
$this->_options = $options;
}


/**
* Log a message to PHP's error log
* @param $msg
*/
protected function _log($msg) {
$arr = debug_backtrace();
$class = $arr[0]['class'];
$line = $arr[0]['line'];
error_log ( "[ $class - line $line ] : " . $msg );
}


/**
* Returns true if in debug mode, false if in production mode
* @return bool
*/
protected function _debug() {
return isset($this->_options["debug"]) && $this->_options["debug"] == true;
}

}
65 changes: 65 additions & 0 deletions src/libs/mixpanel/ConsumerStrategies/AbstractConsumer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
require_once(dirname(__FILE__) . "/../Base/MixpanelBase.php");

/**
* Provides some base methods for use by a Consumer implementation
*/
abstract class ConsumerStrategies_AbstractConsumer extends Base_MixpanelBase {

/**
* Creates a new AbstractConsumer
* @param array $options
*/
function __construct($options = array()) {

parent::__construct($options);

if ($this->_debug()) {
$this->_log("Instantiated new Consumer");
}

}

/**
* Encode an array to be persisted
* @param array $params
* @return string
*/
protected function _encode($params) {
return base64_encode(json_encode($params));
}

/**
* Handles errors that occur in a consumer
* @param $code
* @param $msg
*/
protected function _handleError($code, $msg) {
if (isset($this->_options['error_callback'])) {
$handler = $this->_options['error_callback'];
call_user_func($handler, $code, $msg);
}

if ($this->_debug()) {
$arr = debug_backtrace();
$class = get_class($arr[0]['object']);
$line = $arr[0]['line'];
error_log ( "[ $class - line $line ] : " . print_r($msg, true) );
}
}

/**
* Number of requests/batches that will be processed in parallel.
* @return int
*/
public function getNumThreads() {
return 1;
}

/**
* Persist a batch of messages in whatever way the implementer sees fit
* @param array $batch an array of messages to consume
* @return boolean success or fail
*/
abstract function persist($batch);
}
Loading

0 comments on commit 72f5f70

Please sign in to comment.