Skip to content
99 changes: 56 additions & 43 deletions src/Pronamic/Twinfield/Customer/CustomerFactory.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<?php
namespace Pronamic\Twinfield\Customer;


use \Pronamic\Twinfield\Factory\ParentFactory;
use \Pronamic\Twinfield\Customer\Mapper\CustomerMapper;
use \Pronamic\Twinfield\Request as Request;

/**
* CustomerFactory
*
*
* A facade factory to make interaction with the the twinfield service easier
* when trying to retrieve or send information about Customers.
*
*
* Each function has detailed explanation over what is required, and what
* happens.
*
*
* If you require more complex interactions or a heavier amount of control
* over the requests to/from then look inside the methods or see
* the advanced guide detailing the required usages.
*
*
* @package Pronamic\Twinfield
* @subpackage Customer
* @author Leon Rowland <[email protected]>
Expand All @@ -29,29 +30,29 @@ class CustomerFactory extends ParentFactory
/**
* Requests a specific customer based off the passed in code
* and office.
*
*
* Office is an optional parameter.
*
*
* First it attempts to login with the passed configuration into
* this instances constructor. If successful it will get the Service
* class to handle further interactions.
*
*
* If no office has been passed it will instead take the default office
* from the passed in config class.
*
*
* It makes a new instance of the Request\Read\Customer() and sets the
* office and code parameters.
*
*
* Using the Service class it will attempt to send the DOM document from
* Read\Customer()
*
*
* It sets the response to this instances method setResponse() (which you
* can access with getResponse())
*
* If the response was successful it will return a
*
* If the response was successful it will return a
* \Pronamic\Twinfield\Customer\Customer instance, made by the
* \Pronamic\Twinfield\Customer\Mapper\CustomerMapper class.
*
*
* @access public
* @param int $code
* @param int $office
Expand All @@ -61,7 +62,7 @@ public function get($code, $office = null)
{
// Attempts to process the login
if ($this->getLogin()->process()) {

// Get the secure service class
$service = $this->getService();

Expand All @@ -75,7 +76,7 @@ public function get($code, $office = null)
$request_customer
->setOffice($office)
->setCode($code);

// Send the Request document and set the response to this instance.
$response = $service->send($request_customer);
$this->setResponse($response);
Expand All @@ -88,29 +89,29 @@ public function get($code, $office = null)
}
}
}

/**
* Requests all customers from the List Dimension Type.
*
*
* First attempts to login with the passed configuration into this
* instances constructor. If successful will get the Service class
* to handle further interactions.
*
*
* Makes a new instance of Catalog\Dimension and sets the office and
* dimtype values.
*
*
* Using the service class it will attempt to send the DOM document
* from Catalog\Dimension.
*
*
* It sets the response to this instances method setResponse() (which you
* can access with getResponse())
*
*
* If the response was successful it will loop through all the results
* in the response and make an array of the customer ID as the array key
* and the value being an array of 'name' and 'shortname'
*
*
* If the response wasn't succesful it will return false.
*
*
* @access public
* @return array | false
*/
Expand Down Expand Up @@ -169,23 +170,23 @@ public function listAll($office = null, $dimType = 'DEB')
/**
* Sends a \Pronamic\Twinfield\Customer\Customer instance to Twinfield
* to update or add.
*
*
* First attempts to login with the passed configuration in the constructor.
* If successful will get the secure Service class.
*
* It will then make an instance of
*
* It will then make an instance of
* \Pronamic\Twinfield\Customer\DOM\CustomersDocument where it will
* pass in the Customer class in this methods parameter.
*
*
* It will then attempt to send the DOM document from CustomersDocument
* and set the response to this instances method setResponse() (which you
* can get with getResponse())
*
*
* If successful will return true, else will return false.
*
*
* If you want to map the response back into a customer use getResponse()->
* getResponseDocument()->asXML() into the CustomerMapper::map method.
*
*
* @access public
* @param \Pronamic\Twinfield\Customer\Customer $customer
* @return boolean
Expand All @@ -201,47 +202,59 @@ public function send(Customer $customer)
$customersDocument = new DOM\CustomersDocument();
$customersDocument->addCustomer($customer);


// Send the DOM document request and set the response
$response = $service->send($customersDocument);
$this->setResponse($response);

// Return a bool on status of response.
if ($response->isSuccessful()) {
return true;
} else {
return false;
}

} else {

$customersDocument = new DOM\CustomersDocument();
$customersDocument->addCustomer($customer);

$service = $this->getService();
$response = $service->sendWithLobbi($customersDocument);
$this->setResponse($response);

}

// Return a bool on status of response.
if ($response->isSuccessful()) {
return true;
} else {
return false;
}
}

/**
* Returns the very next free code available for the Customer Code. As adding a new customer still requires
* a code. This method first retrieves all customers, sorts by key, gets the last element, and increments the code
* by one.
*
*
* @access public
* @return int
*/
public function getFirstFreeCode()
{
// Get all customers
$customers = $this->listAll();

// Check some customers exist
if (empty($customers)) {
return 0;
}

// Get the keys
$customersKeys = array_keys($customers);

// Sort the keys and reverse to get the last first
asort($customersKeys);
$customersKeys = array_reverse($customersKeys);

// Get the first of the reversed keys
$latestCustomerCode = $customersKeys[0];

// Increment by one and return.
return (int) ++$latestCustomerCode;
}
Expand Down
21 changes: 21 additions & 0 deletions src/Pronamic/Twinfield/Secure/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class Config
*/
private $oauth = null;

private $token = array(
'token' => '',
'url' => '',
'office' => '',
'organisation' => ''
);


/**
* Holds the optional soap client options
Expand Down Expand Up @@ -109,6 +116,8 @@ public function getOAuthParameters()
$this->oauth = new OAuth($this->oauthCredentials);
return $this->oauth->getParameters();
}


/**
* Sets the details for this config
* object.
Expand All @@ -129,6 +138,18 @@ public function setCredentials($username, $password, $organisation, $office)
$this->setOrganisationAndOffice($organisation, $office);
}

public function setToken($token,$org, $office)
{
$this->token['token'] = $token['access_token'];
$this->token['url'] = $token['cluster_url'];
$this->token['organisation'] = $org;
$this->token['office'] = $office;
}

public function getToken(){
return $this->token;
}

/**
* Sets the organisation en office details for this config
* object.
Expand Down
41 changes: 37 additions & 4 deletions src/Pronamic/Twinfield/Secure/Login.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Pronamic\Twinfield\Secure;

use phpDocumentor\Reflection\Types\Object_;
use Pronamic\Twinfield\SoapClient;

/**
Expand Down Expand Up @@ -30,10 +31,10 @@ class Login
protected $loginWSDL = 'https://login.twinfield.com/webservices/session.asmx?wsdl';
protected $clusterWSDL = '%s/webservices/processxml.asmx?wsdl';
protected $xmlNamespace = 'http://schemas.xmlsoap.org/soap/envelope/';

/**
* Holds the passed in Config instance
*
*
* @access private
* @var Pronamic\Twinfield\Secure\Config
*/
Expand Down Expand Up @@ -158,14 +159,29 @@ public function getHeader()
);
}

public function getOauth2Header($token){

$ns = 'http://www.twinfield.com/';

$headerbody = array(
'AccessToken' => $token['token'],
'CompanyCode' => $token['office']

) ;
$header = new \SoapHeader($ns,'Header',$headerbody);


return $header;
}

/**
* Gets the soap client with the headers attached
*
* Will automatically login if haven't already on this instance
*
* @since 0.0.1
*
* @param string|null $wsdl the wsdl to use. If null, the clusterWSDL is used.
* @param string|null $wsdl the wsdl to use. If null, the clusterWSDL is used.
* @access public
* @return \SoapClient
*/
Expand All @@ -174,12 +190,29 @@ public function getClient($wsdl = null)
if (! $this->processed) {
$this->process();
}
$wsdl = is_null($wsdl) ? $this->clusterWSDL : $wsdl;
$wsdl = is_null($wsdl) ? $this->clusterWSDL : $wsdl;
$header = $this->getHeader();
// Makes a new client, and assigns the header to it
$client = new SoapClient(sprintf($wsdl, $this->cluster), $this->config->getSoapClientOptions());
$client->__setSoapHeaders($header);

return $client;
}

public function getClientWithToken( $wsdl = null) {


$token = $this->config->getToken();

$wsdl = is_null($wsdl) ? $this->clusterWSDL : $wsdl;
$header = $this->getOauth2Header($token);


$client = new SoapClient(sprintf($wsdl, $token['url']), array('trace' => 1, 'exceptions' => 0 ));
$client->__setSoapHeaders($header);

return $client;

}

}
Loading