1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace linkprofit\AmoCRM\services; |
4
|
|
|
|
5
|
|
|
use linkprofit\AmoCRM\entities\Authorization; |
6
|
|
|
use linkprofit\AmoCRM\entities\EntityInterface; |
7
|
|
|
use linkprofit\AmoCRM\RequestHandler; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AuthorizationService |
11
|
|
|
* @package linkprofit\AmoCRM\services |
12
|
|
|
*/ |
13
|
|
|
class AuthorizationService implements ServiceInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var RequestHandler |
17
|
|
|
*/ |
18
|
|
|
protected $request; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $fields; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var mixed |
27
|
|
|
*/ |
28
|
|
|
protected $response; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Authorization |
32
|
|
|
*/ |
33
|
|
|
protected $authorization; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* AuthorizationService constructor. |
37
|
|
|
* @param RequestHandler $request |
38
|
|
|
*/ |
39
|
2 |
|
public function __construct(RequestHandler $request) |
40
|
|
|
{ |
41
|
2 |
|
$this->request = $request; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Authorization $authorization |
46
|
|
|
*/ |
47
|
2 |
|
public function add(EntityInterface $authorization) |
48
|
|
|
{ |
49
|
2 |
|
if ($authorization instanceof Authorization) { |
50
|
2 |
|
$this->authorization = $authorization; |
51
|
2 |
|
} |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
2 |
|
public function authorize() |
58
|
|
|
{ |
59
|
2 |
|
$this->composeFields(); |
60
|
2 |
|
$this->request->performRequest($this->getAuthLink(), $this->fields); |
61
|
2 |
|
$this->response = $this->request->getResponse(); |
62
|
2 |
|
$this->response = $this->response['response']; |
63
|
|
|
|
64
|
2 |
|
return $this->checkResponse(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
2 |
|
protected function checkResponse() |
71
|
|
|
{ |
72
|
2 |
|
if (isset($this->response['auth']) && $this->response['auth'] !== false) { |
73
|
1 |
|
return true; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
2 |
|
protected function getAuthLink() |
83
|
|
|
{ |
84
|
2 |
|
return 'https://' . $this->request->getSubdomain() . '.amocrm.ru/private/api/auth.php?type=json'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Fill fields for request |
89
|
|
|
*/ |
90
|
2 |
|
protected function composeFields() |
91
|
|
|
{ |
92
|
2 |
|
$this->fields = $this->authorization->get(); |
93
|
|
|
} |
94
|
|
|
} |