1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Api; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Encryption\Encrypter; |
6
|
|
|
|
7
|
|
|
class Client |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The Encrypter instance. |
11
|
|
|
* |
12
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter |
13
|
|
|
*/ |
14
|
|
|
protected $encrypter; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The api clients. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $clients = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The default app key. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $defaultAppKey; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param array $clients |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Encrypter $encrypter, array $clients = []) |
36
|
|
|
{ |
37
|
|
|
$this->encrypter = $encrypter; |
38
|
|
|
$this->clients = $clients; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the clients. |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function getClients() |
47
|
|
|
{ |
48
|
|
|
return $this->clients; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Set the client. |
53
|
|
|
* |
54
|
|
|
* @param array $clients |
55
|
|
|
*/ |
56
|
|
|
public function setClients(array $clients) |
57
|
|
|
{ |
58
|
|
|
$this->clients = $clients; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the Encrypter instance. |
63
|
|
|
* |
64
|
|
|
* @return \Illuminate\Contracts\Encryption\Encrypter |
65
|
|
|
*/ |
66
|
|
|
public function getEncrypter() |
67
|
|
|
{ |
68
|
|
|
return $this->encrypter; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set the Encrypter instance. |
73
|
|
|
* |
74
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter |
75
|
|
|
*/ |
76
|
|
|
public function setEncrypter(Encrypter $encrypter) |
77
|
|
|
{ |
78
|
|
|
$this->encrypter = $encrypter; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the default app key. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function defaultAppKey() |
87
|
|
|
{ |
88
|
|
|
return $this->defaultAppKey ?: (string) array_first(array_keys($this->clients)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the default app key. |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function setDefaultAppKey($key) |
98
|
|
|
{ |
99
|
|
|
$this->defaultAppKey = $key; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the app key for the given app name. |
106
|
|
|
* |
107
|
|
|
* @param string $appName |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function appKeyForName($appName) |
111
|
|
|
{ |
112
|
|
|
return substr(sha1((string) $appName.$this->encrypter->getKey()), 0, 16); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Generate an app secret for the given app key. |
117
|
|
|
* |
118
|
|
|
* @param string $appKey |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function generateAppSecretForKey($appKey) |
122
|
|
|
{ |
123
|
|
|
return md5($this->encrypter->encrypt($appKey)); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Generate an app secret for the given app name. |
128
|
|
|
* |
129
|
|
|
* @param string $appName |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function generateAppSecretForName($appName) |
133
|
|
|
{ |
134
|
|
|
return $this->generateAppSecretForKey($this->appKeyForName($appName)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the app name for the given app key. |
139
|
|
|
* |
140
|
|
|
* @param string $key |
141
|
|
|
* @return string|null |
142
|
|
|
*/ |
143
|
|
|
public function getAppNameForKey($key) |
144
|
|
|
{ |
145
|
|
|
return array_get($this->clients, $key.'.name'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the app secret for the given app key. |
150
|
|
|
* |
151
|
|
|
* @param string $key |
152
|
|
|
* @return string|null |
153
|
|
|
*/ |
154
|
|
|
public function getAppSecretForKey($key) |
155
|
|
|
{ |
156
|
|
|
return array_get($this->clients, $key.'.secret'); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.