1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace linkprofit\AmoCRM\services; |
4
|
|
|
|
5
|
|
|
use linkprofit\AmoCRM\entities\Account; |
6
|
|
|
use linkprofit\AmoCRM\entities\Field; |
7
|
|
|
use linkprofit\AmoCRM\entities\TaskType; |
8
|
|
|
use linkprofit\AmoCRM\RequestHandler; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class AccountService |
12
|
|
|
* @package linkprofit\AmoCRM\services |
13
|
|
|
*/ |
14
|
|
|
class AccountService implements AccountServiceInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \linkprofit\AmoCRM\RequestHandler |
18
|
|
|
*/ |
19
|
|
|
protected $request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $fieldAssociation = [ |
25
|
|
|
'contacts' => Field::CONTACT_ELEMENT_TYPE, |
26
|
|
|
'leads' => Field::LEAD_ELEMENT_TYPE, |
27
|
|
|
'companies' => Field::COMPANY_ELEMENT_TYPE, |
28
|
|
|
'customers' => Field::CUSTOMER_ELEMENT_TYPE, |
29
|
|
|
'catalogs' => true |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ServiceInterface constructor. |
34
|
|
|
* |
35
|
|
|
* @param RequestHandler $requestHandler |
36
|
|
|
*/ |
37
|
4 |
|
public function __construct(RequestHandler $requestHandler) |
38
|
|
|
{ |
39
|
4 |
|
$this->request = $requestHandler; |
40
|
4 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return bool|array |
44
|
|
|
*/ |
45
|
1 |
|
public function getAllArray() |
46
|
|
|
{ |
47
|
1 |
|
$this->send(); |
48
|
|
|
|
49
|
1 |
|
return $this->request->getResponse(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return array|bool|\linkprofit\AmoCRM\entities\Account |
54
|
|
|
*/ |
55
|
1 |
|
public function getAccount() |
56
|
|
|
{ |
57
|
1 |
|
$this->send([]); |
58
|
|
|
|
59
|
1 |
|
return $this->parseArrayToAccountEntity($this->request->getResponse()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* TODO |
64
|
|
|
* |
65
|
|
|
* @return \linkprofit\AmoCRM\entities\Field[] |
66
|
|
|
*/ |
67
|
1 |
|
public function getCustomFields() |
68
|
|
|
{ |
69
|
1 |
|
$this->send(['with' => 'custom_fields']); |
70
|
|
|
|
71
|
1 |
|
return $this->parseCustomFieldsArrayToFieldEntities($this->request->getResponse()['_embedded']['custom_fields']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* TODO |
76
|
|
|
* |
77
|
|
|
* @return \linkprofit\AmoCRM\entities\User[] |
78
|
|
|
*/ |
79
|
|
|
public function getUsers() {} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* TODO |
83
|
|
|
* |
84
|
|
|
* @return \linkprofit\AmoCRM\entities\Pipeline[] |
85
|
|
|
*/ |
86
|
|
|
public function getPipelines() {} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* TODO |
90
|
|
|
* |
91
|
|
|
* @return \linkprofit\AmoCRM\entities\Group[] |
92
|
|
|
*/ |
93
|
|
|
public function getGroups() {} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* TODO |
97
|
|
|
* |
98
|
|
|
* @return \linkprofit\AmoCRM\entities\NoteTypes[] |
99
|
|
|
*/ |
100
|
|
|
public function getNoteTypes() {} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return \linkprofit\AmoCRM\entities\TaskType[] |
104
|
|
|
*/ |
105
|
1 |
|
public function getTaskTypes() |
106
|
|
|
{ |
107
|
1 |
|
$this->send(['with' => 'task_types']); |
108
|
|
|
|
109
|
1 |
|
return $this->parseArrayToTaskTypeEntities($this->request->getResponse()['_embedded']['task_types']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
4 |
|
protected function getLink() |
116
|
|
|
{ |
117
|
4 |
|
return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/account'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param array $with |
122
|
|
|
*/ |
123
|
4 |
|
private function send($with = ['with' => 'custom_fields,users,pipelines,groups,note_types,task_types']) |
124
|
|
|
{ |
125
|
4 |
|
$link = $this->getLink() . '?' . http_build_query($with); |
126
|
4 |
|
$this->request->performRequest($link, [], 'application/json', 'GET'); |
127
|
4 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $array |
131
|
|
|
* |
132
|
|
|
* @return Account |
133
|
|
|
*/ |
134
|
1 |
|
private function parseArrayToAccountEntity($array) |
135
|
|
|
{ |
136
|
1 |
|
$account = new Account(); |
137
|
1 |
|
$account->set($array); |
138
|
|
|
|
139
|
1 |
|
return $account; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param array $array |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
1 |
|
public function parseArrayToTaskTypeEntities(array $array) |
148
|
|
|
{ |
149
|
1 |
|
$entities = []; |
150
|
|
|
|
151
|
1 |
|
foreach ($array as $item) { |
152
|
1 |
|
$entity = new TaskType(); |
153
|
1 |
|
$entity->set($item); |
154
|
1 |
|
$entities[] = $entity; |
155
|
1 |
|
} |
156
|
|
|
|
157
|
1 |
|
return $entities; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $array |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
1 |
|
private function parseCustomFieldsArrayToFieldEntities(array $array) |
166
|
|
|
{ |
167
|
1 |
|
$entities = []; |
168
|
|
|
|
169
|
1 |
|
foreach ($array as $elementTypeKey => $items) { |
170
|
1 |
|
if (!isset($this->fieldAssociation[$elementTypeKey])) { |
171
|
|
|
continue; |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
$elementType = $this->fieldAssociation[$elementTypeKey]; |
175
|
|
|
|
176
|
1 |
|
if ($elementType === true) { |
177
|
1 |
|
$entities = array_merge($entities, $this->parseArrayToCatalogFieldEntities($items)); |
178
|
1 |
|
} else { |
179
|
1 |
|
$entities = array_merge($entities, $this->parseArrayToFieldEntities($items, $elementType)); |
180
|
|
|
} |
181
|
1 |
|
} |
182
|
|
|
|
183
|
1 |
|
return $entities; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $items |
188
|
|
|
* @param int $elementType |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
1 |
|
private function parseArrayToFieldEntities(array $items, $elementType) |
193
|
|
|
{ |
194
|
1 |
|
$entities = []; |
195
|
1 |
|
foreach ($items as $item) { |
196
|
1 |
|
$entities[] = $this->parseArrayToFieldEntity($item, $elementType); |
197
|
1 |
|
} |
198
|
|
|
|
199
|
1 |
|
return $entities; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param array $item |
204
|
|
|
* @param $elementType |
205
|
|
|
* |
206
|
|
|
* @return \linkprofit\AmoCRM\entities\Field |
207
|
|
|
*/ |
208
|
1 |
|
private function parseArrayToFieldEntity(array $item, $elementType) |
209
|
|
|
{ |
210
|
1 |
|
$entity = new Field(); |
211
|
1 |
|
$entity->set($item); |
212
|
1 |
|
$entity->element_type = $elementType; |
213
|
|
|
|
214
|
1 |
|
return $entity; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param array $items |
219
|
|
|
* |
220
|
|
|
* @return array |
221
|
|
|
*/ |
222
|
1 |
|
private function parseArrayToCatalogFieldEntities(array $items) |
223
|
|
|
{ |
224
|
1 |
|
$entities = []; |
225
|
1 |
|
foreach ($items as $elementType => $catalogItems) { |
226
|
1 |
|
$entities = array_merge($entities, $this->parseArrayToFieldEntities($catalogItems, $elementType)); |
227
|
1 |
|
} |
228
|
1 |
|
return $entities; |
229
|
|
|
} |
230
|
|
|
} |