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
|
2 |
|
public function __construct(RequestHandler $requestHandler) |
36
|
|
|
{ |
37
|
2 |
|
$this->request = $requestHandler; |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Lead $lead |
42
|
|
|
*/ |
43
|
2 |
|
public function add(EntityInterface $lead) |
44
|
|
|
{ |
45
|
2 |
|
if ($lead instanceof Lead) { |
46
|
2 |
|
$this->lead[] = $lead; |
47
|
|
|
} |
48
|
2 |
|
} |
49
|
|
|
|
50
|
2 |
|
public function createLead() |
51
|
|
|
{ |
52
|
2 |
|
$this->composeAddFields(); |
53
|
2 |
|
$this->request->performRequest($this->getLink(), $this->fields); |
54
|
2 |
|
$this->response = $this->request->getResponse(); |
55
|
|
|
|
56
|
2 |
|
if ($this->checkResponse()) { |
57
|
2 |
|
return $this->getResponse(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
public function getResponse() |
64
|
|
|
{ |
65
|
2 |
|
return $this->response; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
2 |
|
protected function checkResponse() |
72
|
|
|
{ |
73
|
2 |
|
if (isset($this->response['_embedded']['items'])) { |
74
|
2 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
2 |
|
protected function getLink() |
84
|
|
|
{ |
85
|
2 |
|
return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/api/v2/leads'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Fill fields for response |
90
|
|
|
*/ |
91
|
2 |
|
protected function composeAddFields() |
92
|
|
|
{ |
93
|
2 |
|
$fields = []; |
94
|
|
|
|
95
|
2 |
|
foreach ($this->lead as $lead) { |
96
|
2 |
|
$fields[] = $lead->get(); |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
$this->fields['add'] = $fields; |
100
|
|
|
} |
101
|
|
|
} |