| 1 |
<?php |
| 2 |
/*! |
| 3 |
* Hybridauth |
| 4 |
* https://hybridauth.github.io | https://github.com/hybridauth/hybridauth |
| 5 |
* (c) 2017 Hybridauth authors | https://hybridauth.github.io/license.html |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Hybridauth\Adapter; |
| 9 |
|
| 10 |
/** |
| 11 |
* Trait DataStoreTrait |
| 12 |
*/ |
| 13 |
trait DataStoreTrait |
| 14 |
{ |
| 15 |
/** |
| 16 |
* Returns storage instance |
| 17 |
* |
| 18 |
* @return \Hybridauth\Storage\StorageInterface |
| 19 |
*/ |
| 20 |
abstract public function getStorage(); |
| 21 |
|
| 22 |
/** |
| 23 |
* Store a piece of data in storage. |
| 24 |
* |
| 25 |
* This method is mainly used for OAuth tokens (access, secret, refresh, and whatnot), but it |
| 26 |
* can be also used by providers to store any other useful data (i.g., user_id, auth_nonce, etc.) |
| 27 |
* |
| 28 |
* @param string $name |
| 29 |
* @param mixed $value |
| 30 |
*/ |
| 31 |
protected function storeData($name, $value = null) |
| 32 |
{ |
| 33 |
// if empty, we simply delete the thing as we'd want to only store necessary data |
| 34 |
if (empty($value)) { |
| 35 |
$this->deleteStoredData($name); |
| 36 |
} |
| 37 |
|
| 38 |
$this->getStorage()->set($this->providerId . '.' . $name, $value); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Retrieve a piece of data from storage. |
| 43 |
* |
| 44 |
* This method is mainly used for OAuth tokens (access, secret, refresh, and whatnot), but it |
| 45 |
* can be also used by providers to retrieve from store any other useful data (i.g., user_id, |
| 46 |
* auth_nonce, etc.) |
| 47 |
* |
| 48 |
* @param string $name |
| 49 |
* |
| 50 |
* @return mixed |
| 51 |
*/ |
| 52 |
protected function getStoredData($name) |
| 53 |
{ |
| 54 |
return $this->getStorage()->get($this->providerId . '.' . $name); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Delete a stored piece of data. |
| 59 |
* |
| 60 |
* @param string $name |
| 61 |
*/ |
| 62 |
protected function deleteStoredData($name) |
| 63 |
{ |
| 64 |
$this->getStorage()->delete($this->providerId . '.' . $name); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Delete all stored data of the instantiated adapter |
| 69 |
*/ |
| 70 |
protected function clearStoredData() |
| 71 |
{ |
| 72 |
$this->getStorage()->deleteMatch($this->providerId . '.'); |
| 73 |
} |
| 74 |
} |
| 75 |
|