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