1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace linkprofit\AmoCRM\services; |
4
|
|
|
|
5
|
|
|
use linkprofit\AmoCRM\entities\EntityInterface; |
6
|
|
|
use linkprofit\AmoCRM\RequestHandler; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class BaseService |
10
|
|
|
* @package linkprofit\AmoCRM\services |
11
|
|
|
*/ |
12
|
|
|
abstract class BaseService implements ServiceInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var RequestHandler |
16
|
|
|
*/ |
17
|
|
|
protected $request; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $fields = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var mixed |
26
|
|
|
*/ |
27
|
|
|
protected $response; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array EntityInterface |
31
|
|
|
*/ |
32
|
|
|
protected $entities = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* LeadService constructor. |
36
|
|
|
* @param RequestHandler $requestHandler |
37
|
|
|
*/ |
38
|
37 |
|
public function __construct(RequestHandler $requestHandler) |
39
|
|
|
{ |
40
|
37 |
|
$this->request = $requestHandler; |
41
|
37 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return bool|mixed |
45
|
|
|
*/ |
46
|
29 |
|
public function save() |
47
|
|
|
{ |
48
|
29 |
|
$this->composeFields(); |
49
|
29 |
|
$this->request->performRequest($this->getLink(), $this->fields); |
50
|
29 |
|
$this->response = $this->request->getResponse(); |
51
|
|
|
|
52
|
29 |
|
if ($this->checkResponse()) { |
53
|
21 |
|
return $this->getResponse(); |
54
|
|
|
} |
55
|
|
|
|
56
|
8 |
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
21 |
|
public function getResponse() |
63
|
|
|
{ |
64
|
21 |
|
return $this->response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
16 |
|
public function getEntities() |
71
|
|
|
{ |
72
|
16 |
|
return $this->entities; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array|bool |
77
|
|
|
*/ |
78
|
29 |
|
public function parseResponseToEntities() |
79
|
|
|
{ |
80
|
29 |
|
if (!$this->checkResponse()) { |
81
|
8 |
|
return false; |
82
|
|
|
} |
83
|
21 |
|
$this->entities = []; |
84
|
|
|
|
85
|
21 |
|
foreach ($this->response['_embedded']['items'] as $item) { |
86
|
21 |
|
$this->entities[] = $this->parseArrayToEntity($item); |
87
|
21 |
|
} |
88
|
|
|
|
89
|
21 |
|
return $this->entities; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $array |
94
|
|
|
* @return EntityInterface |
95
|
|
|
*/ |
96
|
|
|
abstract public function parseArrayToEntity($array); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
29 |
|
protected function checkResponse() |
102
|
|
|
{ |
103
|
29 |
|
if (isset($this->response['_embedded']['items']) && count($this->response['_embedded']['items'])) { |
104
|
21 |
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
8 |
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Fill fields for save request |
112
|
|
|
*/ |
113
|
29 |
|
protected function composeFields() |
114
|
|
|
{ |
115
|
29 |
|
$addFields = []; |
116
|
29 |
|
$updateFields = []; |
117
|
|
|
|
118
|
29 |
|
foreach ($this->entities as $entity) { |
119
|
29 |
|
if ($entity->id) { |
120
|
8 |
|
$updateFields[] = $entity->get(); |
121
|
8 |
|
} else { |
122
|
21 |
|
$addFields[] = $entity->get(); |
123
|
|
|
} |
124
|
29 |
|
} |
125
|
|
|
|
126
|
29 |
|
if (count($addFields)) { |
127
|
21 |
|
$this->fields['add'] = $addFields; |
128
|
21 |
|
} |
129
|
|
|
|
130
|
29 |
|
if (count($updateFields)) { |
131
|
8 |
|
$this->fields['update'] = $updateFields; |
132
|
8 |
|
} |
133
|
29 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
abstract protected function getLink(); |
139
|
|
|
} |