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