ApiToken.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Models; |
| 4 | |
| 5 | use SureCart\Support\Encryption; |
| 6 | |
| 7 | /** |
| 8 | * The API token model. |
| 9 | */ |
| 10 | class ApiToken { |
| 11 | |
| 12 | /** |
| 13 | * The option key. |
| 14 | * |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $key = 'sc_api_token'; |
| 18 | |
| 19 | /** |
| 20 | * Prevent php warnings. |
| 21 | */ |
| 22 | final public function __construct() {} |
| 23 | |
| 24 | /** |
| 25 | * Clear the API token. |
| 26 | * |
| 27 | * @return bool True if the value was updated, false otherwise. |
| 28 | */ |
| 29 | protected function clear() { |
| 30 | return delete_option( $this->key ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Save and encrypt the API token. |
| 35 | * |
| 36 | * @param string $value The API token. |
| 37 | * @return bool True if the value was updated, false otherwise. |
| 38 | */ |
| 39 | protected function save( $value ) { |
| 40 | $saved = update_option( $this->key, Encryption::encrypt( $value ) ); |
| 41 | \SureCart::requests()->setToken( $value ); |
| 42 | return $saved; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get and decrypt the API token |
| 47 | * |
| 48 | * @return string The decoded API token. |
| 49 | */ |
| 50 | protected function get() { |
| 51 | if ( defined( 'SURECART_API_TOKEN' ) ) { |
| 52 | return SURECART_API_TOKEN; |
| 53 | } |
| 54 | return Encryption::decrypt( get_option( $this->key, '' ) ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Forward call to method |
| 59 | * |
| 60 | * @param string $method Method to call. |
| 61 | * @param mixed $params Method params. |
| 62 | */ |
| 63 | public function __call( $method, $params ) { |
| 64 | return call_user_func_array( [ $this, $method ], $params ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Static Facade Accessor |
| 69 | * |
| 70 | * @param string $method Method to call. |
| 71 | * @param mixed $params Method params. |
| 72 | * |
| 73 | * @return mixed |
| 74 | */ |
| 75 | public static function __callStatic( $method, $params ) { |
| 76 | return call_user_func_array( [ new static(), $method ], $params ); |
| 77 | } |
| 78 | } |
| 79 |