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