| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\StorageDrivers\S3; |
| 4 |
|
| 5 |
use FluentCart\Framework\Support\Arr; |
| 6 |
|
| 7 |
class S3Settings |
| 8 |
{ |
| 9 |
|
| 10 |
protected $settings; |
| 11 |
|
| 12 |
protected $driverHandler = 'fluent_cart_storage_settings_s3'; |
| 13 |
|
| 14 |
static ?array $cachedSettings = null; |
| 15 |
|
| 16 |
public bool $isUsingDefineMode = false; |
| 17 |
|
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
if (self::$cachedSettings) { |
| 21 |
$this->settings = self::$cachedSettings; |
| 22 |
return; |
| 23 |
} |
| 24 |
$settings = fluent_cart_get_option($this->driverHandler, []); |
| 25 |
|
| 26 |
$this->isUsingDefineMode = defined('FCT_S3_ACCESS_KEY') && defined('FCT_S3_SECRET_KEY'); |
| 27 |
|
| 28 |
$this->settings = wp_parse_args($settings, static::getDefaults()); |
| 29 |
|
| 30 |
if ($this->isUsingDefineMode) { |
| 31 |
$this->settings['is_using_define_mode'] = true; |
| 32 |
$this->settings['access_key'] = FCT_S3_ACCESS_KEY; |
| 33 |
$this->settings['secret_key'] = FCT_S3_SECRET_KEY; |
| 34 |
} |
| 35 |
|
| 36 |
$settings['region'] = 'us-east-1'; |
| 37 |
self::$cachedSettings = $this->settings; |
| 38 |
} |
| 39 |
|
| 40 |
public static function getDefaults() |
| 41 |
{ |
| 42 |
return [ |
| 43 |
'is_active' => 'no', |
| 44 |
'secret_key' => '', |
| 45 |
'access_key' => '', |
| 46 |
'bucket' => '', |
| 47 |
'region' => 'us-east-1', |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
public function isActive() |
| 52 |
{ |
| 53 |
$settings = $this->get(); |
| 54 |
|
| 55 |
$isActive = Arr::get($settings, 'is_active') === 'yes'; |
| 56 |
|
| 57 |
if ($isActive) { |
| 58 |
$requiredKeys = ['secret_key', 'access_key', 'region']; |
| 59 |
|
| 60 |
return $this->hasRequiredKeys($settings, $requiredKeys); |
| 61 |
} |
| 62 |
|
| 63 |
return false; |
| 64 |
} |
| 65 |
|
| 66 |
private function hasRequiredKeys(array $settings, array $requiredKeys): bool |
| 67 |
{ |
| 68 |
foreach ($requiredKeys as $key) { |
| 69 |
if (empty(Arr::get($settings, $key))) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return true; |
| 75 |
} |
| 76 |
|
| 77 |
public function get($key = '') |
| 78 |
{ |
| 79 |
return $this->settings; |
| 80 |
} |
| 81 |
} |