Skip to content

Commit

Permalink
Merge pull request #6 from plcf5/main
Browse files Browse the repository at this point in the history
upgrade constructor
  • Loading branch information
li-hoy authored Apr 4, 2022
2 parents b15c0c1 + e2b23c6 commit bf26f3a
Showing 1 changed file with 46 additions and 40 deletions.
86 changes: 46 additions & 40 deletions Assistent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

namespace Lihoy\Amo;

use \Ufee\Amo\Oauthapi as AmoClient;
use \Ufee\Amo\Services\Account as AmoAccount;
use Exception;
use Ufee\Amo\Oauthapi as AmoClient;
use Ufee\Amo\Services\Account as AmoAccount;

class Assistent
{
Expand All @@ -21,7 +22,7 @@ public function __construct(
string $storagePath
) {
if (empty($storagePath)) {
throw new \Exception("Storage path is empty.");
throw new Exception("Storage path is empty.");
}
$this->storage_path = $storagePath;
$this->setStorage($this->storage_path);
Expand Down Expand Up @@ -60,7 +61,7 @@ public function getFromStorage(
$fileContent = \json_decode($fileContent, $associative);
return $fileContent;
}
throw new \Exception("Storage '$key' doesn't exist.");
throw new Exception("Storage '$key' doesn't exist.");
}

/**
Expand All @@ -80,7 +81,7 @@ public function saveToStorage(
}
$json = json_encode($data);
if ($checkJson && is_null(json_decode($data))) {
throw new \Exception('Wrong json.');
throw new Exception('Wrong json.');
}
$fm = $this->stoarage;
$fileName = $key . ".json";
Expand All @@ -97,13 +98,13 @@ protected function setStorage(string $path)
$subfolderLine = "";

if (empty($path)) {
throw new \Exception("Path is empty.");
throw new Exception("Path is empty.");
}
$pattern = '/^\/(([a-z\d\/]+)\/)?([a-z\d\._-]*?)$/i';
$matches = [];
$path_validated = preg_match($pattern, $path, $matches);
if ($path_validated !== 1) {
throw new \Exception(
throw new Exception(
"Wrong path (validation pattern: " . addslashes($pattern) . ")."
);
}
Expand Down Expand Up @@ -131,7 +132,9 @@ public function getCusomFieldValues($cf)
* @param customFieldName - string
* @return mixed - custom Field Value or false if it does not exist
*/
public function getCustomField($entity = null, $customFieldName = '')
public function getCustomField(
\Ufee\Amo\Base\Models\ModelWithCF $entity = null,
string $customFieldName = '')
{
if (
!empty($entity)
Expand All @@ -141,10 +144,10 @@ public function getCustomField($entity = null, $customFieldName = '')
$customFieldValue = $entity->cf($customFieldName)->getValue();
return empty($customFieldValue) ? "" : $customFieldValue;
} else {
throw new \Exception("Custom field \"$customFieldName\" doesт`t exist.");
throw new Exception("Custom field \"$customFieldName\" doesт`t exist.");
}
}
throw new \Exception("Wrong params");
throw new Exception("Wrong params");
}

public function getEntityCategoryByType($entityType)
Expand Down Expand Up @@ -172,19 +175,19 @@ public function getEntityCategoryByType($entityType)
}

/**
* @param confirg
* @param array confirg
* @return \Ufee\Amo\Oauthapi
*/
public function setAmoClient(
object $config
array $config
) {
$client = AmoClient::setInstance($config->oauth);
$client->queries->logs($config->log_queries ?? false);
$client->queries->setDelay($config->query_delay ?? 0.15);
$client = AmoClient::setInstance($config);
$client->queries->logs($config['log_queries'] ?? false);
$client->queries->setDelay($config['query_delay'] ?? 0.5);
$client->setOauthPath(
$config->oauth_storage ?? (self::$storage_path . '/Oauth')
$config['oauth_storage'] ?? (self::$storage_path . '/Oauth')
);
AmoAccount::setCacheTime($config->account_cache_time ?? 1800);
AmoAccount::setCacheTime($config['account_cache_time'] ?? 1800);
$this->amo_client = $client;
return $this->amo_client;
}
Expand All @@ -194,31 +197,34 @@ public function setAmoClient(
* @return boolean
*/
public function setCustomField(
$entity = null,
$name = "",
$value = "",
$type = 'text'
\Ufee\Amo\Base\Models\ModelWithCF $entity,
string $name,
string $value,
string $type = 'text'
) {
if (
(!empty($entity) && !empty($name) && !empty($value))
&& $entity->cf($name)
) {
switch ($type) {
case 'date':
$entity->cf($name)->setDate($value);
break;
case 'switch':
$value
? $entity->cf($name)->enable()
: $entity->cf($name)->disable();
break;
default:
$entity->cf($name)->reset();
$entity->cf($name)->setValue($value);
}
return true;
if (empty($name)) {
throw new Exception("Argument 'name' can`t be empty.");
}
if (empty($value)) {
throw new Exception("Argument 'value' can`t be empty.");
}
if (!$entity->cf($name)) {
throw new Exception("Wrong name of field.");
}
switch ($type) {
case 'date':
$entity->cf($name)->setDate($value);
break;
case 'switch':
$value
? $entity->cf($name)->enable()
: $entity->cf($name)->disable();
break;
default:
$entity->cf($name)->reset();
$entity->cf($name)->setValue($value);
}
throw new \Exception("Wrong params");
return true;
}

}

0 comments on commit bf26f3a

Please sign in to comment.