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