Completed
Push — master ( dc5034...dd736e )
by dotzero
02:29
created

Account::getShorted()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 36
ccs 23
cts 23
cp 1
rs 8.8571
cc 2
eloc 27
nc 2
nop 1
crap 2
1
<?php
2
3
namespace AmoCRM\Models;
4
5
/**
6
 * Class Account
7
 *
8
 * Класс модель для работы с Аккаунтом
9
 *
10
 * @package AmoCRM\Models
11
 * @version 0.1.0
12
 * @author dotzero <[email protected]>
13
 * @link http://www.dotzero.ru/
14
 * @link https://github.com/dotzero/amocrm-php
15
 *
16
 * For the full copyright and license information, please view the LICENSE
17
 * file that was distributed with this source code.
18
 */
19
class Account extends Base
20
{
21
    /**
22
     * Данные по аккаунту
23
     *
24
     * Получение информации по аккаунту в котором произведена авторизация:
25
     * название, оплаченный период, пользователи аккаунта и их права,
26
     * справочники дополнительных полей контактов и сделок, справочник статусов сделок,
27
     * справочник типов событий, справочник типов задач и другие параметры аккаунта.
28
     *
29
     * @link https://developers.amocrm.ru/rest_api/accounts_current.php
30
     * @param bool $short Краткий формат, только основные поля
31
     * @return array Ответ amoCRM API
32
     */
33 1
    public function apiCurrent($short = false)
34
    {
35 1
        $result = $this->getRequest('/private/api/v2/json/accounts/current');
36
37 1
        return $short ? $this->getShorted($result['account']) : $result['account'];
38
    }
39
40
    /**
41
     * Урезание значения возвращаемоного методом apiCurrent,
42
     * оставляет только основные поля такие как 'id', 'name', 'type_id', 'enums'
43
     *
44
     * @param array $account Ответ amoCRM API
45
     * @return mixed Краткий ответ amoCRM API
46
     */
47 1
    private function getShorted($account)
48
    {
49 1
        $keys = array_fill_keys(['id', 'name', 'login'], 1);
50
        $account['users'] = array_map(function ($val) use ($keys) {
51 1
            return array_intersect_key($val, $keys);
52 1
        }, $account['users']);
53
54 1
        $keys = array_fill_keys(['id', 'name'], 1);
55
        $account['leads_statuses'] = array_map(function ($val) use ($keys) {
56 1
            return array_intersect_key($val, $keys);
57 1
        }, $account['leads_statuses']);
58
59 1
        $keys = array_fill_keys(['id', 'name'], 1);
60
        $account['note_types'] = array_map(function ($val) use ($keys) {
61 1
            return array_intersect_key($val, $keys);
62 1
        }, $account['note_types']);
63
64 1
        $keys = array_fill_keys(['id', 'name'], 1);
65
        $account['task_types'] = array_map(function ($val) use ($keys) {
66 1
            return array_intersect_key($val, $keys);
67 1
        }, $account['task_types']);
68
69 1
        $keys = array_fill_keys(['id', 'name', 'type_id', 'enums'], 1);
70 1
        foreach ($account['custom_fields'] AS $type => $fields) {
71
            $account['custom_fields'][$type] = array_map(function ($val) use ($keys) {
72 1
                return array_intersect_key($val, $keys);
73 1
            }, $fields);
74 1
        }
75
76 1
        $keys = array_fill_keys(['id', 'label', 'name'], 1);
77 1
        $account['pipelines'] = array_map(function ($val) use ($keys) {
78 1
            return array_intersect_key($val, $keys);
79 1
        }, $account['pipelines']);
80
81 1
        return $account;
82
    }
83
}
84