Skip to content

Commit

Permalink
Improve code style; require php ^7.4 || ^8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JorisDebonnet committed Nov 23, 2021
1 parent 7f1f985 commit 5e3990a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 77 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
}
],
"require": {
"php": "^5.4 || ^7.0",
"php": "^7.4 || ^8.0",
"ext-soap": "*",
"ext-simplexml": "*"
},
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
"dev-master": "0.2.x-dev"
}
},
"autoload": {
Expand Down
10 changes: 5 additions & 5 deletions src/ResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

class ResponseException extends \Exception
{
private $repsonse = '';
private string $response = '';

public function __construct($message, $repsonse)
public function __construct($message, $response)
{
$this->repsonse = $repsonse;
$this->response = $response;
parent::__construct($message);
}

public function getResponse()
public function getResponse(): string
{
return $this->repsonse;
return $this->response;
}
}
137 changes: 67 additions & 70 deletions src/Yuki.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,34 @@
*
* Magic methods passed to SOAP call:
* General:
* @method array Authenticate(array $params)
* @method array Administrations(array $params)
* @method object Authenticate(array $params)
* @method object Administrations(array $params)
*
* Sales:
* @method array ProcessSalesInvoices(array $params)
* @method object ProcessSalesInvoices(array $params)
*
* Accounting:
* @method array CheckOutstandingItem(array $params)
* @method array NetRevenue(array $params)
* @method array GLAccountBalance(array $params)
* @method array GLAccountTransactions(array $params)
* @method object CheckOutstandingItem(array $params)
* @method object NetRevenue(array $params)
* @method object GLAccountBalance(array $params)
* @method object GLAccountTransactions(array $params)
*
* AccountingInfo:
* @method array GetGLAccountScheme(array $params)
* @method object GetGLAccountScheme(array $params)
*/
class Yuki
{
const SALES_WSDL = 'https://api.yukiworks.nl/ws/Sales.asmx?WSDL';
const ACCOUNTING_WSDL = 'https://api.yukiworks.nl/ws/Accounting.asmx?WSDL';
const ACCOUNTINGINFO_WSDL = 'https://api.yukiworks.nl/ws/AccountingInfo.asmx?WSDL';
private const SALES_WSDL = 'https://api.yukiworks.nl/ws/Sales.asmx?WSDL';
private const ACCOUNTING_WSDL = 'https://api.yukiworks.nl/ws/Accounting.asmx?WSDL';
private const ACCOUNTINGINFO_WSDL = 'https://api.yukiworks.nl/ws/AccountingInfo.asmx?WSDL';

private $soap, // the SOAP client
// the currently active identifiers for the logged in Yuki user:
$sid, // SessionID
$aid = null; // AdministrationID
private SoapClient $soap; // the SOAP client

// The currently active identifiers for the logged in Yuki user:
// SessionID
private string $sid;
// AdministrationID
private ?string $aid = null;


/**
Expand Down Expand Up @@ -65,19 +68,19 @@ class Yuki
* ];
* See http://www.yukiworks.nl/schemas/SalesInvoices.xsd, but note that only a subset is supported here (see code)
*
* @param array $invoice Invoice data in associative array, with matching Yuki keys
* @param bool $escaped Todo: Whether the data is already trimmed and escaped for inclusion in XML tags
* @return mixed
* @param array $invoice Invoice data in associative array, with matching Yuki keys.
* @param bool $escaped Todo: Whether the data is already trimmed and escaped for inclusion in XML tags.
* @return bool
* @throws ResponseException
*/
public function ProcessInvoice($invoice, $escaped = false) {
public function ProcessInvoice(array $invoice, bool $escaped = false): bool {
if (!$escaped) {
array_walk_recursive($invoice, function(&$_v) {
$_v = htmlspecialchars(trim($_v), ENT_XML1);
});
}

// General fields
// General fields.
$SalesInvoice = '';
foreach (['Reference', 'Subject', 'PaymentMethod', 'Date', 'DueDate', 'Currency', 'ProjectCode', 'Remarks'] as $k) {
if (!empty($invoice[$k])) {
Expand All @@ -89,7 +92,7 @@ public function ProcessInvoice($invoice, $escaped = false) {
}
}

// Client (company)
// Client (company).
$Contact = '';
foreach (['ContactCode', 'FullName', 'CountryCode', 'City', 'Zipcode', 'AddressLine_1', 'AddressLine_2', 'EmailAddress', 'CoCNumber', 'VATNumber', 'ContactType'] as $k) {
if (!empty($invoice['Contact'][$k])) {
Expand All @@ -102,7 +105,7 @@ public function ProcessInvoice($invoice, $escaped = false) {
$SalesInvoice .= "<ContactPerson><FullName>{$invoice['ContactPerson']['FullName']}</FullName></ContactPerson>";
}

// Invoice lines
// Invoice lines.
if (!empty($invoice['InvoiceLines'])) {
$InvoiceLines = [];
foreach ($invoice['InvoiceLines'] as $InvoiceLine) {
Expand All @@ -126,27 +129,27 @@ public function ProcessInvoice($invoice, $escaped = false) {
$SalesInvoice .= '<InvoiceLines>'. implode($InvoiceLines) .'</InvoiceLines>';
}

// Generate XML doc
$xmlvar = new SoapVar(
// Generate XML doc.
$xmlDoc = new SoapVar(
'<ns1:xmlDoc><SalesInvoices xmlns="urn:xmlns:http://www.theyukicompany.com:salesinvoices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SalesInvoice>'
. $SalesInvoice
. '</SalesInvoice></SalesInvoices></ns1:xmlDoc>',
XSD_ANYXML
);

$return = $this->ProcessSalesInvoices(['sessionId' => $this->sid, 'administrationId' => $this->aid, 'xmlDoc' => $xmlvar]);
$return = $this->ProcessSalesInvoices(['sessionId' => $this->sid, 'administrationId' => $this->aid, 'xmlDoc' => $xmlDoc]);

// Check the result to see whether it was succesful
// Check the result to see whether it was successful.
$result_xml = simplexml_load_string(current($return->ProcessSalesInvoicesResult));
if (!$result_xml->TotalSucceeded->__toString()) {
// None succeeded, so throw the error message
// None succeeded, so throw the error message.
throw new ResponseException($result_xml->Invoice->Message, $result_xml->asXML());
}
return true; // success
}


public function GetInvoiceBalance($invoiceReference){
public function GetInvoiceBalance($invoiceReference): array {
$yuki_invoice = $this->CheckOutstandingItem(['sessionID' => $this->sid, 'Reference' => $invoiceReference]);
$xml = simplexml_load_string($yuki_invoice->CheckOutstandingItemResult->any);

Expand All @@ -157,11 +160,11 @@ public function GetInvoiceBalance($invoiceReference){
}

/**
* Also called from constructor, if constructed with api key
* @param string $apiKey Yuki API key
* @throws Exception If api key is not set
* Also called from constructor, if constructed with api key.
* @param string $apiKey Yuki API key.
* @throws Exception If api key is not set.
*/
public function login($apiKey) {
public function login(string $apiKey): void {
if (!$apiKey) {
throw new Exception('Yuki API key not set. Please check your company\'s settings. You can find or create a Yuki API key (of type Administration) under Settings > Webservices in Yuki.');
}
Expand All @@ -171,11 +174,11 @@ public function login($apiKey) {
}

/**
* Get the AdministrationID
* Get the AdministrationID.
* @return string AdministrationID
* @throws Exception
*/
private function aid() {
private function aid(): string {
// could maybe be saved to DB the first time, but an API key might later get attached to a different Administration...
$result = $this->Administrations(['sessionID' => $this->sid]);
// Save and return the result
Expand All @@ -188,12 +191,12 @@ private function aid() {
}

/**
* Get a sessionID
* Get a sessionID.
* @param string $api_key Yuki accessKey
* @return string sessionID
* @throws Exception
*/
private function sid($api_key) {
private function sid(string $api_key): string {
$result = $this->Authenticate(['accessKey' => $api_key]);

// Save and return the result
Expand All @@ -205,74 +208,68 @@ private function sid($api_key) {
}

/**
* Get the currently loaded AdministrationID
* Get the currently loaded AdministrationID.
* (small g because it's a class function rather than a web call)
* @return null|string
* @return ?string
*/
public function getAdministrationID() {
public function getAdministrationID(): ?string {
return $this->aid;
}


/**
* Generic call method (it just passes it on to the SoapClient)
* Generic call method (it just passes it on to the SoapClient).
* @param string $method
* @param array $params
* @return object Response
* @return mixed Response
* @throws Exception
*/
public function __call($method, $params) {
public function __call(string $method, array $params) {
try {
$result = $this->soap->__soapCall($method, $params);
return $result;
return $this->soap->__soapCall($method, $params);
} catch (Exception $e) {
// rethrow with a little more information in Message
// Rethrow with a little more information in Message.
throw new Exception("$method failed: " . @$e->faultcode . ' ' . $e->getMessage() . '.');
}
}

/**
* Yuki constructor (creates the SOAP client).
*
* @param null|string $apikey If provided, will immediately connect
* @param null|string $wsdl 'sales' or 'accounting' (if null then 'sales')
* @throws Exception If the SOAP client could not be instantiated, or the login failed
* @param ?string $apikey If provided, will immediately connect.
* @param string $wsdl 'sales' or 'accounting'.
* @throws Exception If the SOAP client could not be instantiated, or the login failed.
*/
public function __construct($apikey = null, $wsdl = null) {
public function __construct(string $apikey = null, string $wsdl = 'sales') {
$this->soap = new SoapClient($this->getWSDL($wsdl), ['trace' => true]);
if ($apikey) {
$this->login($apikey);
}
}

/***
* @param string $wsdl name of the corresponding WSDL
* @return string URL of the corresponding WSDL
* @param string $wsdl name of the corresponding WSDL.
* @return string URL of the corresponding WSDL.
*/
private function getWSDL($wsdl){
switch ($wsdl){
case 'sales':
return self::SALES_WSDL;
break;
private function getWSDL(string $wsdl): string {
switch ($wsdl) {
case 'accounting':
return self::ACCOUNTING_WSDL;
break;
case 'accountinginfo':
return self::ACCOUNTINGINFO_WSDL;
break;
case 'sales':
default:
return self::SALES_WSDL;
break;
}
}

/**
* @param $start
* @param $end
* @return array
* @param string $start Start date.
* @param string $end End date.
* @return object
* @throws Exception
*/
public function GetAdministrationNetRevenue($start, $end){
public function GetAdministrationNetRevenue(string $start, string $end): object {
try {
return $this->NetRevenue(['sessionID' => $this->sid, 'administrationID' => $this->aid, 'StartDate' => $start, 'EndDate' => $end]);
} catch (Exception $e) {
Expand All @@ -281,11 +278,11 @@ public function GetAdministrationNetRevenue($start, $end){
}

/**
* @param $date
* @return array
* @param string $date
* @return object
* @throws Exception
*/
public function GetAccountBalance($date){
public function GetAccountBalance(string $date): object {
try {
return $this->GLAccountBalance(['sessionID' => $this->sid, 'administrationID' => $this->aid, 'transactionDate' => $date]);
} catch (Exception $e) {
Expand All @@ -294,10 +291,10 @@ public function GetAccountBalance($date){
}

/**
* @return array
* @return object
* @throws Exception
*/
public function GetAccountCodes(){
public function GetAccountCodes(): object {
try {
return $this->GetGLAccountScheme(['sessionID' => $this->sid, 'administrationID' => $this->aid]);
} catch (Exception $e) {
Expand All @@ -309,10 +306,10 @@ public function GetAccountCodes(){
* @param $accountCode
* @param $start
* @param $end
* @return array
* @return object
* @throws Exception
*/
public function GetTransactions($accountCode, $start, $end){
public function GetTransactions($accountCode, $start, $end): object {
try {
return $this->GLAccountTransactions(['sessionID' => $this->sid, 'administrationID' => $this->aid, 'GLAccountCode' => $accountCode, 'StartDate' => $start, 'EndDate' => $end]);
} catch (Exception $e) {
Expand Down

0 comments on commit 5e3990a

Please sign in to comment.