Skip to content

Commit fded1f9

Browse files
committed
INT-12240 local_aws_sdk: add config helper
1 parent 892b604 commit fded1f9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

classes/aws_sdk.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,47 @@
3434
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3535
*/
3636
class aws_sdk {
37+
/**
38+
* Required AWS config names.
39+
*/
40+
const REQUIRED_CONFIGS = ['key', 'secret', 'region'];
41+
3742
/**
3843
* Autoload the AWS SDK classes.
3944
*/
4045
public static function autoload() {
4146
require_once(__DIR__.'/../vendor/autoload.php');
4247
}
48+
49+
/**
50+
* Extract region, key and secret from CFG global.
51+
*
52+
* @param string $name The CFG config name
53+
* @return array
54+
*/
55+
public static function config_from_cfg($name) {
56+
global $CFG;
57+
58+
if (empty($CFG->$name)) {
59+
throw new \coding_exception('The $CFG->'.$name.' must be set');
60+
}
61+
if (!is_array($CFG->$name)) {
62+
throw new \coding_exception('The $CFG->'.$name.' must be set to an array');
63+
}
64+
$cfg = $CFG->$name;
65+
foreach (self::REQUIRED_CONFIGS as $key) {
66+
if (!array_key_exists($key, $cfg)) {
67+
throw new \coding_exception('The $CFG->'.$name.' is missing the \''.$key.'\' option. '.
68+
'Required configs: '.implode(', ', self::REQUIRED_CONFIGS));
69+
}
70+
}
71+
72+
return [
73+
'region' => $cfg['region'],
74+
'credentials' => [
75+
'key' => $cfg['key'],
76+
'secret' => $cfg['secret']
77+
],
78+
];
79+
}
4380
}

0 commit comments

Comments
 (0)