1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace linkprofit\AmoCRM\services; |
4
|
|
|
|
5
|
|
|
use linkprofit\AmoCRM\entities\EntityInterface; |
6
|
|
|
use linkprofit\AmoCRM\entities\TaskType; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class TaskTypeService |
10
|
|
|
* |
11
|
|
|
* @package linkprofit\AmoCRM\services |
12
|
|
|
*/ |
13
|
|
|
class TaskTypeService extends BaseService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var TaskType[] |
17
|
|
|
*/ |
18
|
|
|
protected $entities = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param $array |
22
|
|
|
* |
23
|
|
|
* @return EntityInterface |
24
|
|
|
*/ |
25
|
3 |
|
public function parseArrayToEntity($array) |
26
|
|
|
{ |
27
|
3 |
|
$taskType = new TaskType(); |
28
|
3 |
|
$taskType->set($array); |
29
|
|
|
|
30
|
3 |
|
return $taskType; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
3 |
|
protected function getLink() |
37
|
|
|
{ |
38
|
3 |
|
return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/private/tasks/ajax_task_status_edit.php'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param \linkprofit\AmoCRM\entities\EntityInterface $taskType |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
3 |
|
public function add(EntityInterface $taskType) |
47
|
|
|
{ |
48
|
3 |
|
if ($taskType instanceof TaskType) { |
49
|
3 |
|
$this->entities[] = $taskType; |
50
|
3 |
|
} |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return bool|mixed |
55
|
|
|
*/ |
56
|
3 |
View Code Duplication |
public function save() |
|
|
|
|
57
|
|
|
{ |
58
|
3 |
|
$this->composeFields(); |
59
|
3 |
|
$this->request->performRequest($this->getLink(), $this->fields, 'application/x-www-form-urlencoded'); |
60
|
3 |
|
$this->response = $this->request->getResponse(); |
61
|
|
|
|
62
|
3 |
|
if ($this->checkResponse()) { |
63
|
2 |
|
return $this->getResponse(); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Fill fields for save request |
71
|
|
|
*/ |
72
|
3 |
View Code Duplication |
protected function composeFields() |
|
|
|
|
73
|
|
|
{ |
74
|
3 |
|
$addFields = []; |
75
|
3 |
|
$updateFields = []; |
76
|
|
|
|
77
|
3 |
|
foreach ($this->entities as $entity) { |
78
|
3 |
|
if ($entity->id) { |
79
|
1 |
|
$updateFields[] = $entity->get(); |
80
|
1 |
|
} else { |
81
|
2 |
|
$addFields[] = $entity->get(); |
82
|
|
|
} |
83
|
3 |
|
} |
84
|
|
|
|
85
|
3 |
|
$this->fields['ACTION'] = 'ALL_EDIT'; |
86
|
|
|
|
87
|
3 |
|
$fields = array_merge($addFields, $updateFields); |
88
|
|
|
|
89
|
3 |
|
if (count($fields)) { |
90
|
3 |
|
$this->fields['task_types'] = $fields; |
91
|
3 |
|
} |
92
|
3 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array|bool |
96
|
|
|
*/ |
97
|
3 |
View Code Duplication |
public function parseResponseToEntities() |
|
|
|
|
98
|
|
|
{ |
99
|
3 |
|
if (!$this->checkResponse()) { |
100
|
1 |
|
return false; |
101
|
|
|
} |
102
|
2 |
|
$this->entities = []; |
103
|
|
|
|
104
|
2 |
|
foreach ($this->response['data'] as $item) { |
105
|
2 |
|
$this->entities[] = $this->parseArrayToEntity($item); |
106
|
2 |
|
} |
107
|
|
|
|
108
|
2 |
|
return $this->entities; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
3 |
|
protected function checkResponse() |
115
|
|
|
{ |
116
|
3 |
|
if (isset($this->response['status']) && $this->response['status'] === 'OK' && count($this->response['data'])) { |
117
|
2 |
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
return false; |
121
|
|
|
} |
122
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.