Added a helper class `economic_customer` to standardize customer data handling within the Economic API. Introduced a new route, `/modules/economic/customer`, for retrieving customers using their customer number. Updated `economic` class to integrate the helper and support streamlined customer operations.
129 lines
3.7 KiB
PHP
129 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace helpers;
|
|
|
|
use classes\economic;
|
|
use Exception;
|
|
|
|
class economic_customer
|
|
{
|
|
/**
|
|
* The Economic customerNumber, which is the unique identifier for a customer
|
|
* @var int $customer_number
|
|
*/
|
|
protected int $customer_number;
|
|
/**
|
|
* The raw customer data from the Economic API
|
|
* @var object $customer_data
|
|
*/
|
|
protected object $customer_data_object;
|
|
|
|
|
|
/**
|
|
* Construct a new Economic customer object
|
|
* @throws Exception if the customer does not exist
|
|
*/
|
|
public function __construct(int $customer_number)
|
|
{
|
|
self::setCustomerNumber($customer_number);
|
|
self::requireExists();
|
|
self::fetchCustomerData();
|
|
self::requireSelected();
|
|
}
|
|
|
|
/**
|
|
* Check if a customer exists
|
|
* @param int $customer_number The Economic customer number to check for
|
|
* @throws Exception if the customer does not exist
|
|
*/
|
|
protected function requireExists(): void
|
|
{
|
|
if (!self::exists()) {
|
|
throw new Exception('The customer does not exist in the Economic system');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a customer exists in the Economic system
|
|
* @return bool True if the customer exists, false if not
|
|
* @throws Exception
|
|
*/
|
|
public function exists(): bool
|
|
{
|
|
// Make sure the customer number is set
|
|
self::requireCustomerNumber();
|
|
// Check if the customer exists
|
|
$economic = new economic();
|
|
return $economic->customers->customers->exists(self::getCustomerNumber());
|
|
}
|
|
|
|
/**
|
|
* Require that the customer number is set
|
|
* @throws Exception if the customer number is not set
|
|
*/
|
|
protected function requireCustomerNumber(): void
|
|
{
|
|
if (!isset($this->customer_number)) {
|
|
throw new Exception('The customer number is not set');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the customer number
|
|
* @return int The Economic customer number
|
|
*/
|
|
public function getCustomerNumber(): int
|
|
{
|
|
return $this->customer_number;
|
|
}
|
|
|
|
/**
|
|
* Set the customer number
|
|
* @param int $customer_number The Economic customer number
|
|
*/
|
|
protected function setCustomerNumber(int $customer_number): void
|
|
{
|
|
|
|
$this->customer_number = $customer_number;
|
|
}
|
|
|
|
/**
|
|
* Fetch the customer data from the Economic API and store it in the object property $customer_data_object
|
|
* @throws Exception if the customer data could not be fetched
|
|
* @throws Exception if the customer data is invalid or empty
|
|
*/
|
|
protected function fetchCustomerData(): void
|
|
{
|
|
// Get the customer data from the Economic API
|
|
$economic = new economic();
|
|
$tmp = $economic->customers->customers->get($this->customer_number);
|
|
// Check if the customer data is invalid or empty
|
|
if (!isset($tmp->customerNumber)) {
|
|
throw new Exception('The customer data is invalid or empty');
|
|
}
|
|
// Store the customer data in the object property
|
|
$this->customer_data_object = $tmp;
|
|
}
|
|
|
|
/**
|
|
* Require that a customer is selected, i.e. that the customer data is valid and not empty
|
|
* @throws Exception if the customer data is invalid or empty
|
|
*/
|
|
protected function requireSelected(): void
|
|
{
|
|
if (!isset($this->customer_data_object->customerNumber)) {
|
|
throw new Exception('The customer data is invalid or empty');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the customer data as an object
|
|
* @return object The customer data as an object
|
|
* @throws Exception if the customer data is invalid or empty
|
|
*/
|
|
public function asObject(): object
|
|
{
|
|
self::requireSelected();
|
|
return $this->customer_data_object;
|
|
}
|
|
} |