ldc-crm-client/src/CrmApiV4.php

145 lines
5.4 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 CrmApiV4
{
/**
* The Api object that will handle requests and responses
*
* @var \LDC\LDCCRM\Transport\ApiInterface
*/
private static $apiInterface;
/**
* CrmApiV4
*
* @param string|null $endpoint
* @param string|null $bearerToken
* @param string|null $bearerPwd
*/
public function __construct(
#[\SensitiveParameter] string $endpoint = null,
#[\SensitiveParameter] string $bearerToken = null,
#[\SensitiveParameter] 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('apiV4.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(#[\SensitiveParameter] 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(#[\SensitiveParameter] string $identification): ?Customer
{
return Customer::find($identification);
}
/**
* Set the single optin status for an email
*
* @param string $email
* @param boolean $opt_in
* @param array|null $answerId
* @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(#[\SensitiveParameter] string $email, bool $opt_in, ?array $answerId = null): bool
{
return SingleOptin::setStatus($email, $opt_in, $answerId);
}
/**
* 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(#[\SensitiveParameter] string $email, #[\SensitiveParameter] string $password): bool
{
return Customer::login($email, $password);
}
}