Account::getShorted()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 23
cts 23
cp 1
rs 9.344
c 0
b 0
f 0
cc 2
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
 * @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 Account extends AbstractModel
19
{
20
    /**
21
     * Данные по аккаунту
22
     *
23
     * Получение информации по аккаунту в котором произведена авторизация:
24
     * название, оплаченный период, пользователи аккаунта и их права,
25
     * справочники дополнительных полей контактов и сделок, справочник статусов сделок,
26
     * справочник типов событий, справочник типов задач и другие параметры аккаунта.
27
     *
28
     * @link https://developers.amocrm.ru/rest_api/accounts_current.php
29
     * @param bool $short Краткий формат, только основные поля
30
     * @param array $parameters Массив параметров к amoCRM API
31
     * @return array Ответ amoCRM API
32
     */
33 2
    public function apiCurrent($short = false, $parameters = [])
34
    {
35 2
        $result = $this->getRequest('/private/api/v2/json/accounts/current', $parameters);
36
37 2
        return $short ? $this->getShorted($result['account']) : $result['account'];
38
    }
39
40
    /**
41
     * Возвращает сведения о пользователе по его логину.
42
     * Если не указывать логин, вернутся сведения о владельце API ключа.
43
     *
44
     * @param null|string $login Логин пользователя
45
     * @return mixed Данные о пользователе
46
     */
47 1
    public function getUserByLogin($login = null)
48
    {
49 1
        if ($login === null) {
50 1
            $login = $this->getParameters()->getAuth('login');
51 1
        }
52
53 1
        $login = strtolower($login);
54 1
        $result = $this->apiCurrent();
55
56 1
        foreach ($result['users'] as $user) {
57 1
            if (strtolower($user['login']) == $login) {
58 1
                return $user;
59
            }
60 1
        }
61
62
        return false;
63
    }
64
65
    /**
66
     * Урезание значения возвращаемого методом apiCurrent,
67
     * оставляет только основные поля такие как 'id', 'name', 'type_id', 'enums'
68
     *
69
     * @param array $account Ответ amoCRM API
70
     * @return mixed Краткий ответ amoCRM API
71
     */
72 1
    private function getShorted($account)
73
    {
74 1
        $keys = array_fill_keys(['id', 'name', 'login'], 1);
75
        $account['users'] = array_map(function($val) use ($keys) {
76 1
            return array_intersect_key($val, $keys);
77 1
        }, $account['users']);
78
79 1
        $keys = array_fill_keys(['id', 'name'], 1);
80
        $account['leads_statuses'] = array_map(function($val) use ($keys) {
81 1
            return array_intersect_key($val, $keys);
82 1
        }, $account['leads_statuses']);
83
84 1
        $keys = array_fill_keys(['id', 'name'], 1);
85
        $account['note_types'] = array_map(function($val) use ($keys) {
86 1
            return array_intersect_key($val, $keys);
87 1
        }, $account['note_types']);
88
89 1
        $keys = array_fill_keys(['id', 'name'], 1);
90
        $account['task_types'] = array_map(function($val) use ($keys) {
91 1
            return array_intersect_key($val, $keys);
92 1
        }, $account['task_types']);
93
94 1
        $keys = array_fill_keys(['id', 'name', 'type_id', 'enums'], 1);
95 1
        foreach ($account['custom_fields'] AS $type => $fields) {
96
            $account['custom_fields'][$type] = array_map(function($val) use ($keys) {
97 1
                return array_intersect_key($val, $keys);
98 1
            }, $fields);
99 1
        }
100
101 1
        $keys = array_fill_keys(['id', 'label', 'name'], 1);
102 1
        $account['pipelines'] = array_map(function($val) use ($keys) {
103 1
            return array_intersect_key($val, $keys);
104 1
        }, $account['pipelines']);
105
106 1
        return $account;
107
    }
108
}
109