|
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
|
|
|
class LeadService implements ServiceInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var RequestHandler |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $request; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $fields = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $response; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array Lead |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $lead = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* LeadService constructor. |
|
33
|
|
|
* @param RequestHandler $requestHandler |
|
34
|
|
|
*/ |
|
35
|
3 |
|
public function __construct(RequestHandler $requestHandler) |
|
36
|
|
|
{ |
|
37
|
3 |
|
$this->request = $requestHandler; |
|
38
|
3 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Lead $lead |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function add(EntityInterface $lead) |
|
44
|
|
|
{ |
|
45
|
3 |
|
if ($lead instanceof Lead) { |
|
46
|
3 |
|
$this->lead[] = $lead; |
|
47
|
|
|
} |
|
48
|
3 |
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
public function createLead() |
|
51
|
|
|
{ |
|
52
|
3 |
|
$this->composeAddFields(); |
|
53
|
3 |
|
$this->request->performRequest($this->getLink(), $this->fields); |
|
54
|
3 |
|
$this->response = $this->request->getResponse(); |
|
55
|
|
|
|
|
56
|
3 |
|
if ($this->checkResponse()) { |
|
57
|
2 |
|
return $this->getResponse(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function getResponse() |
|
64
|
|
|
{ |
|
65
|
2 |
|
return $this->response; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
3 |
|
protected function checkResponse() |
|
72
|
|
|
{ |
|
73
|
3 |
|
if (isset($this->response['_embedded']['items']) && count($this->response['_embedded']['items'])) { |
|
74
|
2 |
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
3 |
|
protected function getLink() |
|
84
|
|
|
{ |
|
85
|
3 |
|
return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/leads'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Fill fields for response |
|
90
|
|
|
*/ |
|
91
|
3 |
|
protected function composeAddFields() |
|
92
|
|
|
{ |
|
93
|
3 |
|
$fields = []; |
|
94
|
|
|
|
|
95
|
3 |
|
foreach ($this->lead as $lead) { |
|
96
|
3 |
|
$fields[] = $lead->get(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
$this->fields['add'] = $fields; |
|
100
|
|
|
} |
|
101
|
|
|
} |