Source of file Client.php
Size: 11,484 Bytes - Last Modified: 2021-12-23T10:21:36+00:00
/var/www/docs.ssmods.com/process/src/src/API/Client/Client.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 | <?php namespace SilverCart\API\Client; use ReflectionClass; use SilverCart\API\Response\Response; use SilverCart\Dev\Tools; use SilverCart\Model\ShopEmail; /** * Main handler for CURL client calls. * * @package SilverCart * @subpackage API\Client * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 21.10.2019 * @license see license file in modules root directory * @copyright 2019 pixeltricks GmbH */ class Client { use \SilverStripe\Core\Config\Configurable; use \SilverStripe\Core\Injector\Injectable; /** * API target URL. * * @var string */ private static $api_url = ''; /** * API proxy URL (optional). * * @var string */ private static $api_proxy_url = ''; /** * API user name. * * @var string */ private static $api_username = ''; /** * API password. * * @var string */ private static $api_password = ''; /** * API error recipient email address. * * @var string */ private static $api_error_recipient = ''; /** * API timeout in seconds. * Caution: may not work for every kind of API. * * @var int */ private static $api_timeout = 30; /** * Set to true to disable the server SSL certificate verification. * Caution: Avoid to go for this property in production environments. * * @var bool */ private static $disable_ssl_verification = false; /** * List of occurred errors responded by the API * * @var array */ protected $errorList = []; /** * File name to use for logging. * * @var string */ protected $logFileName = null; /** * Constructor. * Calls the parent constructor and sets the log file name. * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 11.02.2019 */ public function __construct() { $reflection = new ReflectionClass($this); $shortName = $reflection->getShortName(); $this->setLogFileName("APIClient.{$shortName}"); } /** * Returns whether the API is enabled. * Alias for self::isEnabled(). * * @return bool */ public function isActive() : bool { return self::isEnabled(); } /** * Returns whether the API is enabled. * * @return bool */ public static function isEnabled() : bool { $client = static::create(); $url = $client->getAPIURL(); $user = $client->getAPIUsername(); $pass = $client->getAPIPassword(); return !(empty($url) || empty($user) || empty($pass)); } /**************************************************************************/ /**************************************************************************/ /** **/ /** API Helper Section **/ /** **/ /**************************************************************************/ /**************************************************************************/ /** * Returns whether to disable the SSL verification. * Alias for @see $this->getDisableSSLVerification(). * * @return bool */ public function disableSSLVerification() : bool { return $this->getDisableSSLVerification(); } /** * Returns whether to use a proxy. * * @return bool */ public function getAPIProxyURL() : string { return $this->config()->api_proxy_url; } /** * Returns whether to disable the SSL verification. * * @return bool */ public function getDisableSSLVerification() : bool { return $this->config()->disable_ssl_verification; } /** * Returns whether to use a proxy. * * @return bool */ public function useAPIProxy() : bool { return !empty($this->config()->api_proxy_url); } /** * Returns whether one or more errors occurred. * * @return bool * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 27.09.2018 */ public function errorOccurred() : bool { return count($this->errorList) > 0; } /** * Sends an error notification email. * * @param string $subject Subject * @param string $content Content * * @return Client * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 27.03.2019 */ public function sendErrorNotificationEmail(string $subject = 'An error occurred while trying to deal with the API', string $content = '') : Client { $recipient = $this->getAPIErrorRecipient(); if (!empty($recipient) && $this->errorOccurred() ) { if (!empty($content)) { $content .= PHP_EOL; $content .= PHP_EOL; $content .= PHP_EOL; } foreach ($this->getErrorList() as $error) { $content .= $error . PHP_EOL; } ShopEmail::send_email($recipient, $subject, $content); } return $this; } /** * Adds a single error message to the list of occurred errors responded by * the API. * * @param string $error Error message * * @return $this * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 27.09.2018 */ public function addError(string $error) : Client { $this->errorList[] = $error; return $this; } /** * Adds an error list to the list of occurred errors responded by the API. * * @param array $errorList List of errors * * @return $this * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 27.09.2018 */ public function addErrorList(array $errorList) : array { $this->errorList = array_merge( $this->errorList, $errorList ); return $this; } /** * Returns the list of occurred errors responded by the API. * * @return array */ public function getErrorList() : array { return $this->errorList; } /** * Sets the list of occurred errors responded by the API. * * @param array $errorList List of errors * * @return $this */ public function setErrorList($errorList) : Client { $this->errorList = $errorList; return $this; } /**************************************************************************/ /**************************************************************************/ /** **/ /** API Properties Section **/ /** **/ /**************************************************************************/ /**************************************************************************/ /** * Returns the API URL. * * @return string */ public function getAPIURL() : string { return $this->config()->api_url; } /** * Returns the API user name. * * @return string */ public function getAPIUsername() : string { return $this->config()->api_username; } /** * Returns the API password. * * @return string */ public function getAPIPassword() : string { return $this->config()->api_password; } /** * Returns the API password. * * @return string */ public function getAPIErrorRecipient() : string { $recipient = $this->config()->api_error_recipient; if (empty($recipient)) { $recipient = ''; } return $recipient; } /** * Returns the API timeout. * * @return int */ public function getAPITimeout() : int { return $this->config()->api_timeout; } /**************************************************************************/ /**************************************************************************/ /** **/ /** Response Section **/ /** **/ /**************************************************************************/ /**************************************************************************/ /** * Returns the default error response object. * * @param string $message Error message * @param string $code Error code * * @return Response */ protected function getErrorResponse(string $message, string $code) : Response { return Response::create($this, '', null, true, $message, $code); } /** * Returns the default success response object. * * @param string $body Response body string * @param object $data Response data object * * @return Response */ protected function getSuccessResponse(string $body = '', object $data = null) : Response { return Response::create($this, $body, $data); } /** * Returns the default unknown error response object. * * @return Response */ protected function getUnknownErrorResponse() : Response { return $this->getErrorResponse('UnknownError', '-1'); } /**************************************************************************/ /**************************************************************************/ /** **/ /** Logging Section **/ /** **/ /**************************************************************************/ /**************************************************************************/ /** * Returns the log file name. * * @return string */ public function getLogFileName() : string { if (is_null($this->logFileName)) { $this->logFileName = self::class; } return $this->logFileName; } /** * Sets the log file name. * * @param string $logFileName Log file name * * @return Client */ public function setLogFileName(string $logFileName) : Client { $this->logFileName = $logFileName; return $this; } /** * Logs the given message. * * @param string $message Message to log * @param string $type Type of message * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 11.02.2019 */ protected function log(string $message, string $type = 'notice') : void { Tools::Log(strtoupper($type), $message, $this->getLogFileName()); } } |