1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soheilrt\AdobeConnectClient; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Cache\Repository; |
6
|
|
|
use Illuminate\Contracts\Support\DeferrableProvider; |
7
|
|
|
use Illuminate\Support\Facades\App; |
8
|
|
|
use Illuminate\Support\Facades\Cache; |
9
|
|
|
use Illuminate\Support\ServiceProvider; |
10
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
11
|
|
|
use Soheilrt\AdobeConnectClient\Client\Client; |
12
|
|
|
use Soheilrt\AdobeConnectClient\Client\Connection\Curl\Connection; |
13
|
|
|
|
14
|
|
|
class AdobeConnectServiceProvider extends ServiceProvider implements DeferrableProvider |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Register services. |
18
|
|
|
* |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function register() |
22
|
|
|
{ |
23
|
|
|
$this->mergeConfigFrom(__DIR__ . "/config/adobeConnect.php", 'adobeConnect'); |
24
|
|
|
$this->bindFacades(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Bind Facades |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
private function bindFacades() |
33
|
|
|
{ |
34
|
|
|
$config = $this->getAdobeConfig(); |
35
|
|
|
$entities = $config["entities"]; |
36
|
|
|
|
37
|
|
|
$this->app->singleton(Client::class, function () { |
38
|
|
|
return $this->processClient(); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
$this->app->bind('sco', function () use ($entities) { |
42
|
|
|
return new $entities["sco"](); |
43
|
|
|
}); |
44
|
|
|
$this->app->bind('sco-record', function () use ($entities) { |
45
|
|
|
return new $entities["sco-record"](); |
46
|
|
|
}); |
47
|
|
|
$this->app->bind('principal', function () use ($entities) { |
48
|
|
|
return new $entities["principal"](); |
49
|
|
|
}); |
50
|
|
|
$this->app->bind('permission', function () use ($entities) { |
51
|
|
|
return new $entities["permission"](); |
52
|
|
|
}); |
53
|
|
|
$this->app->bind('common-info', function () use ($entities) { |
54
|
|
|
return new $entities["common-info"](); |
55
|
|
|
}); |
56
|
|
|
$this->app->bind('adobe-connect', function () { |
57
|
|
|
return App::make(Client::class); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* get Adobe Connect Client Config |
64
|
|
|
* |
65
|
|
|
* @return array|null |
66
|
|
|
*/ |
67
|
|
|
private function getAdobeConfig() |
68
|
|
|
{ |
69
|
|
|
return $this->app["config"]->get("adobeConnect"); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* login adobe client in case of there is no session configured |
74
|
|
|
* |
75
|
|
|
* @throws InvalidArgumentException |
76
|
|
|
* @return Client |
77
|
|
|
*/ |
78
|
|
|
private function processClient() |
79
|
|
|
{ |
80
|
|
|
$config = $this->getAdobeConfig(); |
81
|
|
|
|
82
|
|
|
$connection = new Connection($config["host"], $config["connection"]); |
83
|
|
|
$client = new Client($connection); |
84
|
|
|
|
85
|
|
|
if ($config["session-cache"]["enabled"]) { |
86
|
|
|
$this->loginClient($client); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $client; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Login Client Based information that introduced in environment/config file |
94
|
|
|
* |
95
|
|
|
* @param Client $client |
96
|
|
|
* |
97
|
|
|
* @throws InvalidArgumentException |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
private function loginClient(Client $client) |
101
|
|
|
{ |
102
|
|
|
$config = $this->getAdobeConfig(); |
103
|
|
|
$cacheRepository = Cache::store($this->getCacheDriver()); |
104
|
|
|
|
105
|
|
|
// this statement will check for session cache value and sees if there is |
106
|
|
|
// any valid value(not empty) session available inside cache or not. If there isn't any valid cache it |
107
|
|
|
// will send a login request and then put client session string inside cache on successful login. |
108
|
|
|
//on the other side if there is any valid session inside cache, it'll use it for creating client instance |
109
|
|
|
// instead of sending login request to adobe server |
110
|
|
|
if ($this->ValidateCache($cacheRepository, $config['session-cache']['key'])) { |
111
|
|
|
$client->setSession($cacheRepository->get($config["session-cache"]["key"])); |
|
|
|
|
112
|
|
|
} else { |
113
|
|
|
|
114
|
|
|
$client->login($config["user-name"], $config["password"]); |
115
|
|
|
//store client session string if login was successful |
116
|
|
|
if ($client->getSession()) { |
117
|
|
|
$cacheRepository->put( |
118
|
|
|
$config['session-cache']['key'], |
119
|
|
|
$client->getSession(), |
120
|
|
|
$config['session-cache']['timeout'] |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* get Preferred cache driver |
128
|
|
|
* |
129
|
|
|
* @return string|null |
130
|
|
|
*/ |
131
|
|
|
private function getCacheDriver() |
132
|
|
|
{ |
133
|
|
|
$config = $this->app["config"]->get("adobeConnect"); |
134
|
|
|
if ($driver = $config["session-cache"]["driver"]) { |
135
|
|
|
return $driver; |
136
|
|
|
} |
137
|
|
|
return $this->app["config"]->get("cache.default"); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* validate cached session based on it's value |
142
|
|
|
* tries to remove cache value in case of an empty value stored on cache. |
143
|
|
|
* |
144
|
|
|
* @param Repository $driver |
145
|
|
|
* @param string $key |
146
|
|
|
* |
147
|
|
|
* @throws InvalidArgumentException |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
private function ValidateCache(Repository $driver, string $key) |
151
|
|
|
{ |
152
|
|
|
$status = empty($driver->get($key)); |
153
|
|
|
if ($status) { |
154
|
|
|
$driver->forget($key); |
155
|
|
|
} |
156
|
|
|
return !$status; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Bootstrap services. |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function boot() |
165
|
|
|
{ |
166
|
|
|
$this->publishes([ |
167
|
|
|
__DIR__ . '/config/adobeConnect.php' => config_path('adobeConnect.php'), |
168
|
|
|
], 'adobe-connect'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get the services provided by the provider. |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
|
|
public function provides() |
177
|
|
|
{ |
178
|
|
|
return [ |
179
|
|
|
Client::class, |
180
|
|
|
'sco-record', |
181
|
|
|
'principal', |
182
|
|
|
'permission', |
183
|
|
|
'common-info', |
184
|
|
|
'sco', |
185
|
|
|
'adobe-connect' |
186
|
|
|
]; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|