Completed
Push — master ( 4d38ee...409412 )
by dotzero
02:01
created

CustomersPeriods::apiList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
namespace AmoCRM\Models;
4
5
/**
6
 * Class CustomersPeriods
7
 *
8
 * Класс модель для работы со Списком периодов
9
 *
10
 * @package AmoCRM\Models
11
 * @author dotzero <[email protected]>
12
 * @link http://www.dotzero.ru/
13
 * @link https://github.com/dotzero/amocrm-php
14
 *
15
 * For the full copyright and license information, please view the LICENSE
16
 * file that was distributed with this source code.
17
 */
18
class CustomersPeriods extends Base
19
{
20
    /**
21
     * @var array Список доступный полей для модели (исключая кастомные поля)
22
     */
23
    protected $fields = [
24
        'id',
25
        'period',
26
        'sort',
27
        'color',
28
    ];
29
30
    /**
31
     * Список периодов
32
     *
33
     * Метод для получения списка периодов
34
     *
35
     * @link https://developers.amocrm.ru/rest_api/customers_periods/list.php
36
     * @return array Ответ amoCRM API
37
     */
38
    public function apiList()
39
    {
40
        $response = $this->getRequest('/private/api/v2/json/customers_periods/list');
41
42
        return isset($response['customers_periods']['list']) ? $response['customers_periods']['list'] : [];
43
    }
44
45
    /**
46
     * Добавление, удаление и обновление периодов покупателей
47
     *
48
     * Метод позволяет изменять данные по периодам.
49
     * При изменении необходимо передать полный список периодов, включая уже существующие.
50
     * При удалении периода нужно исключить его из запроса.
51
     *
52
     * @link https://developers.amocrm.ru/rest_api/customers_periods/set.php
53
     * @param array periods Массив периодов
54
     * @return int|array Уникальный идентификатор периода или массив при пакетном добавлении
55
     */
56
    public function apiSet($periods = [])
57
    {
58
        $parameters = [
59
            'customers_periods' => [
60
                'update' => [],
61
            ],
62
        ];
63
64
        foreach ($periods AS $period) {
65
            $parameters['customers_periods']['update'][] = $period->getValues();
66
        }
67
68
        $response = $this->postRequest('/private/api/v2/json/customers_periods/set', $parameters);
69
70
        if (isset($response['customers_periods']['set'])) {
71
            $result = array_map(function($item) {
72
                return $item['id'];
73
            }, $response['customers_periods']['set']);
74
        } else {
75
            return [];
76
        }
77
78
        return count($periods) == 1 ? array_shift($result) : $result;
79
    }
80
}
81