O
O
Oleg2019-10-31 22:10:48
PHP
Oleg, 2019-10-31 22:10:48

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);

For greater convenience, I thought to use one array where all parameters for filtering will be passed. Thus, the function becomes much cleaner in the arguments, but it seems to me that I'm not very comfortable building this array of filters. I have no control over the filling of values ​​and names of keys. Is there any better way to create this array? Or maybe the whole idea with an array is bad? I give below the whole code
<?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);
    }

}

And an additional question. Is it okay to use wrapper functions like this to form a different transparent set of arguments? And this, in fact, you can simply pass an array with different filters into one function.
Thank you in advance for any advice and criticism

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexandrto, 2019-10-31
@alexandrto

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 question

Ask a Question

731 491 924 answers to any question