ldc-crm-client/src/CrmApiV2.php
2023-06-28 15:13:35 +02:00

141 lines
5.1 KiB
PHP

<?php
namespace LDC\LDCCRM;
use LDC\LDCCRM\Enums\ApiStatus;
use LDC\LDCCRM\Models\Customer;
use LDC\LDCCRM\Models\SingleOptin;
use LDC\LDCCRM\Transport\ApiInterface;
/**
* This is the main class to interact with the CRM
*
* @package LDCCrmClient
*/
class CrmApiV2
{
/**
* The Api object that will handle requests and responses
*
* @var \LDC\LDCCRM\Transport\ApiInterface
*/
private static $apiInterface;
/**
* CrmApiV2
*
* @param string|null $endpoint
* @param string|null $bearerToken
* @param string|null $bearerPwd
*/
public function __construct(string $endpoint = null, string $bearerToken = null, string $bearerPwd = null)
{
self::$apiInterface = self::$apiInterface ?: new ApiInterface(
$endpoint,
$bearerToken,
$bearerPwd
);
return $this;
}
/**
* Use this the ping the crm and know its current status
*
* @return boolean
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs
* on server side or on getting response.
*/
public function ping(): bool
{
$request = Utils::getResquest('apiV2.ping');
$response = self::$apiInterface->call($request);
return $response->status->equals(ApiStatus::OK());
}
/**
*
* Get a customer from its hashcode or email
* If it does not exists a new instance
* will be returned. You would have
* to call its save method later on.
*
* @param string $identification The customer hashcode or email.
* @return \LDC\LDCCRM\Models\Customer A client Customer model.
*
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
* @phpcs:ignore Squiz.Commenting.FunctionComment.EmptyThrows
*/
public function getCustomerOrNew(string $identification): Customer
{
return $this->getCustomer($identification) ?? new Customer();
}
/**
* Get a customer from its hashcode or email
* If it does not exists a new instance
* will be returned. You would have
* to call its save method later on.
*
* @param string $identification The customer hashcode or email.
* @return \LDC\LDCCRM\Models\Customer|null Return null if the customer was not found
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
* Possibles are : - modelNotFound, If this customer hashcode could not be found.
* @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
*/
public function getCustomer(string $identification): ?Customer
{
return Customer::find($identification);
}
/**
* Set the single optin status for an email
*
* @param string $email
* @param boolean $opt_in
* @return boolean true if succeeded
* @throws \LDC\LDCCRM\Exceptions\ValidationException If a field email could not be validated.
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
* Possibles are : - singleOptinCustomerExists, If a customer exists with this email.
* @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
*/
public static function setStatus(string $email, bool $opt_in): bool
{
return SingleOptin::setStatus($email, $opt_in);
}
/**
* Set the single optin status for an email
*
* @param string $hashcode
* @return boolean true if succeeded
* @throws \LDC\LDCCRM\Exceptions\ValidationException If a field hashcode could not be validated.
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
* Possibles are : - modelNotFound, If a single optin does not exists.
* @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
*/
public static function unsetStatusWithHashcode(string $hashcode): bool
{
return SingleOptin::unsetStatusWithHashcode($hashcode);
}
/**
* Test a customer credentials to see if it can login
*
* @param string $email
* @param string $password
* @return boolean
* @throws \LDC\LDCCRM\Exceptions\CrmException If an error occurs on server side or on getting response.
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any error or \LDC\LDCCRM\Enums\ApiError occurs.
* Possibles are : - authFailed, If credentials are not valid.
* - customerNotActivated, If the account is not activated.
* @phpcs:ignore Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
*/
public static function login(string $email, string $password): bool
{
return Customer::login($email, $password);
}
}