CustomersPeriods   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 70
ccs 23
cts 24
cp 0.9583
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apiList() 0 6 2
B apiSet() 0 31 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 AbstractModel
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 1
    public function apiList()
39
    {
40 1
        $response = $this->getRequest('/private/api/v2/json/customers_periods/list');
41
42 1
        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 1
    public function apiSet($periods = [])
57
    {
58 1
        if (empty($periods)) {
59 1
            $periods = [$this];
60 1
        }
61
62
        $parameters = [
63
            'customers_periods' => [
64 1
                'update' => [],
65 1
            ],
66 1
        ];
67
68 1
        foreach ($periods AS $period) {
69 1
            if ($period instanceof self) {
70 1
                $period = $period->getValues();
71 1
            }
72 1
            $parameters['customers_periods']['update'][] = $period;
73 1
        }
74
75 1
        $response = $this->postRequest('/private/api/v2/json/customers_periods/set', $parameters);
76
77 1
        if (isset($response['customers_periods']['set'])) {
78 1
            $result = array_map(function ($item) {
79 1
                return $item['id'];
80 1
            }, $response['customers_periods']['set']);
81 1
        } else {
82
            return [];
83
        }
84
85 1
        return count($periods) == 1 ? array_shift($result) : $result;
86
    }
87
}
88