1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: dp |
5
|
|
|
* Date: 16.08.17 |
6
|
|
|
* Time: 13:09 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Lan\Ebs\Sdk; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
|
13
|
|
|
final class Security implements Common |
14
|
|
|
{ |
15
|
|
|
const TEST_TOKEN = '7c0c2193d27108a509abd8ea84a8750c82b3a520'; |
16
|
|
|
|
17
|
|
|
const PROD_API_HOST = 'https://openapi.e.lanbook.com'; |
18
|
|
|
const TEST_API_HOST = 'http://openapi.landev.ru'; |
19
|
|
|
const DEV_API_HOST = 'http://eop.local'; |
20
|
|
|
|
21
|
|
|
const PROD_EBS_HOST = 'https://e.lanbook.com'; |
22
|
|
|
const TEST_EBS_HOST = 'http://ebs.landev.ru'; |
23
|
|
|
const DEV_EBS_HOST = 'http://ebs.local'; |
24
|
|
|
|
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Security constructor. |
29
|
|
|
* |
30
|
|
|
* @param Client $client |
31
|
|
|
* @throws Exception |
32
|
|
|
*/ |
33
|
2 |
|
public function __construct(Client $client) |
34
|
|
|
{ |
35
|
2 |
|
if (!$client) { |
36
|
|
|
throw new Exception('Client not defined'); |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
$this->client = $client; |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $method |
44
|
|
|
* @param array $params |
45
|
|
|
* @return array |
46
|
|
|
* @throws Exception |
47
|
|
|
*/ |
48
|
2 |
|
public function getUrl($method, array $params = []) |
49
|
|
|
{ |
50
|
|
|
switch ($method) { |
51
|
2 |
|
case 'getDemoUrl': |
52
|
|
|
return [ |
53
|
1 |
|
'url' => '/1.0/security/demoUrl', |
54
|
1 |
|
'method' => 'GET', |
55
|
|
|
'code' => 200 |
56
|
1 |
|
]; |
57
|
1 |
|
case 'getAutologinUrl': |
58
|
|
|
return [ |
59
|
1 |
|
'url' => '/1.0/security/autologinUrl', |
60
|
1 |
|
'method' => 'GET', |
61
|
|
|
'code' => 200 |
62
|
1 |
|
]; |
63
|
|
|
default: |
64
|
|
|
throw new Exception('Route for ' . $method . ' not found'); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $type |
70
|
|
|
* @param $id |
71
|
|
|
* @return mixed |
72
|
|
|
* @throws Exception |
73
|
|
|
*/ |
74
|
1 |
|
public function getDemoUrl($type, $id) |
75
|
|
|
{ |
76
|
1 |
|
return $this->client->getResponse($this->getUrl(__FUNCTION__), ['type' => $type, 'id' => $id])['data']; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $uid |
81
|
|
|
* @param null $fio |
82
|
|
|
* @param null $email |
83
|
|
|
* @param null $redirect |
84
|
|
|
* @return mixed |
85
|
|
|
* @throws Exception |
86
|
|
|
*/ |
87
|
1 |
|
public function getAutologinUrl($uid, $fio = null, $email = null, $redirect = null) { |
88
|
1 |
|
return $this->client->getResponse( |
89
|
1 |
|
$this->getUrl(__FUNCTION__), |
90
|
|
|
[ |
91
|
1 |
|
'uid' => $uid, |
92
|
1 |
|
'time' => date('YmdHi'), |
93
|
1 |
|
'fio' => $fio, |
94
|
1 |
|
'email' => $email, |
95
|
|
|
'redirect' => $redirect |
96
|
1 |
|
] |
97
|
1 |
|
)['data']; |
98
|
|
|
} |
99
|
|
|
|
100
|
16 |
|
public static function getApiHost() |
|
|
|
|
101
|
|
|
{ |
102
|
16 |
|
return isset($_SERVER['USER']) && $_SERVER['USER'] == 'dp' ? Security::DEV_API_HOST : Security::TEST_API_HOST; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function getEbsHost() |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
return isset($_SERVER['USER']) && $_SERVER['USER'] == 'dp' ? Security::DEV_EBS_HOST : Security::TEST_EBS_HOST; |
108
|
|
|
} |
109
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: