Answer the question
In order to leave comments, you need to log in
What is the best way to build a class for processing data from an external API?
Greetings. I am developing a statistics output for each employee from Bitrix CRM. It is required to take the number of calls, its productivity, the amount of transactions, etc.
The question is how to make more convenient methods for data processing. I have a function that requests data about calls. Moreover, the request is built by specifying filters for the Bitrix API, for example, CALL_DURATION> 20. And in one case, I need to get all calls from all employees, and in the other, calls only from certain users (by their ID). It turns out I need to specify several different filter parameters for one function. The problem is that you have to add a bunch of arguments to this function: call duration, sampling date, user IDs, sorting type, pagination
getCallsHistory($callDuration=0, $dateStart=0, $dateEnd=0, array $usersID=[], $sort=null, &$result = array(), $nextPagination=0);
<?php
class bitrix24 extends BaseObject {
//Как бы общая функция для выдачи звонков по разным фильтрам
private static function getCallsHistorySource($filterData, &$result = array(), $nextPagination=0)
{
//Тут попытка создать дефолтные значения для фильтров
$defaultFilterData = [
'callDuration' => 0,
'dateStart' => 0,
'dateEnd' => 0,
'usersID' => '',
'sort' => 'CALL_START_DATE'
];
$filterData = array_merge($defaultFilterData, $filterData);
//Тут я уже формирую фильтр для запроса в битрикс
$filter = array(
">=CALL_DURATION" => $filterData['callDuration'],
"CALL_TYPE" => 1,
">CALL_START_DATE" => $filterData['dateStart'],
"<CALL_START_DATE" => $filterData['dateEnd'],
'PORTAL_USER_ID' => $filterData['usersID']
);
// Получаем данные
$callsHistory = Yii::$app->bitrix24->Send("voximplant.statistic.get", 'POST', array(
"FILTER" => $filter,
"SORT" => $filterData['sort'],
"ORDER" => "DESC",
"start" => $nextPagination,
));
// Рекурсией обрабатываем оставшившиеся данные
if(isset($callsHistory['nextPagination']) && $callsHistory['nextPagination'] > 0) {
self::getCallsHistorySource($filterData,$result, $callsHistory['nextPagination']);
}
return $callsHistory['result'];
}
//Это как бы обертка-функция. Хочется, чтобы было видно какие нужно указать аргументы и какие-то сделать обязательными
//Фунукция для получения звонков по айди сотрудников
public static function getCallsHistoryByUsersID(array $usersID, $callDuration=0, $dateStart=0, $dateEnd=0, $sort='')
{
//Вот тут я формирую массив для фильтрации запроса
$filterData = [
'usersID' => $usersID,
'callDuration' => $callDuration,
'dateStart' => $dateStart,
'dateEnd' => $dateEnd,
'sort' => $sort
];
//Вызываю общую функцию
return self::getCallsHistorySource($filterData);
}
//Тут тоже обертка-функция, но уже получаю все звонки по всем пользователям и с другой сортировкой
public static function getCallsHistory($callDuration=0, $dateStart=0, $dateEnd=0, $sort='')
{
//Опять формирую массив фильтров
$filterData = [
'callDuration' => $callDuration,
'dateStart' => $dateStart,
'dateEnd' => $dateEnd,
'sort' => $sort
];
//Вызываю общую функцию
return self::getCallsHistorySource($filterData);
}
}
Answer the question
In order to leave comments, you need to log in
I would do it through the Params function and in which I would return an array of parameters
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question