Files
CloudObjects-PHP-SDK/CloudObjects/SDK/Helpers/SDKLoader.php

119 lines
4.9 KiB
PHP

<?php
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
namespace CloudObjects\SDK\Helpers;
use Exception;
use CloudObjects\SDK\Exceptions\InvalidSDKConfigurationException;
use CloudObjects\SDK\NodeReader, CloudObjects\SDK\ObjectRetriever;
/**
* The SDKLoader helper allows developers to quickly load common PHP SDKs
* from API providers and apply configuration stored in CloudObjects.
*/
class SDKLoader {
private $objectRetriever;
private $reader;
private $classes = [];
/**
* @param ObjectRetriever $objectRetriever An initialized and authenticated object retriever.
*/
public function __construct(ObjectRetriever $objectRetriever) {
$this->objectRetriever = $objectRetriever;
$this->reader = new NodeReader;
}
/**
* Initialize the SDK with the given name and options; used for
* SDKs that are initialized with a function and not a class name.
*
* @param string $sdkName The name of the SDK to initialize.
* @param array $options Additional options for the SDK (if necessary).
* @return mixed The return value of the SDK initialization, which may vary depending on the SDK.
* @throws Exception If the SDK is not supported or cannot be initialized.
*/
public function init(string $sdkName, array $options = []) : mixed {
$namespace = $this->objectRetriever->getAuthenticatingNamespaceCloudObject();
if (!$namespace)
throw new InvalidSDKConfigurationException("The authenticating namespace object could not be retrieved.");
switch (strtolower($sdkName)) {
case "sentry":
// --- Sentry (https://sentry.io/) ---
$initFunction = '\Sentry\init';
return $initFunction(array_merge([
'dsn' => $namespace->getString('coid://sentry.io.3rd-party.co/DSN')
], $options));
default:
throw new Exception("No rules defined to initialize SDK with name <".$sdkName.">.");
}
}
/**
* Initialize and return the SDK with the given classname.
* Throws Exception if the SDK is not supported.
*
* @param string $classname Classname for the SDK's main class.
* @param array $options Additional options for the SDK (if necessary).
* @return mixed The initialized SDK instance.
* @throws Exception If the SDK is not supported or cannot be initialized.
*/
public function get(string $classname, array $options) : mixed {
if (!class_exists($classname))
throw new Exception("<".$classname."> is not a valid classname.");
$hashkey = md5($classname.serialize($options));
if (!isset($this->classes[$hashkey])) {
$nsNode = $this->objectRetriever->getAuthenticatingNamespaceObjectNode();
if (!$nsNode)
throw new InvalidSDKConfigurationException("The authenticating namespace object could not be retrieved.");
// --- Amazon Web Services (https://aws.amazon.com/) ---
// has multiple classnames, so check for common superclass
if (is_a($classname, 'Aws\AwsClient', true)) {
$class = new $classname(array_merge($options, [
'credentials' => [
'key' => $this->reader->getFirstValueString($nsNode, 'coid://aws.3rd-party.co/accessKeyId'),
'secret' => $this->reader->getFirstValueString($nsNode, 'coid://aws.3rd-party.co/secretAccessKey')
]
]));
} else {
switch ($classname) {
// --- stream (https://getstream.io/) ---
case "GetStream\Stream\Client":
$class = new $classname(
$this->reader->getFirstValueString($nsNode, 'coid://getstreamio.3rd-party.co/key'),
$this->reader->getFirstValueString($nsNode, 'coid://getstreamio.3rd-party.co/secret')
);
break;
// --- Pusher (https://pusher.com/) ---
case "Pusher":
$class = new $classname(
$this->reader->getFirstValueString($nsNode, 'coid://pusher.3rd-party.co/key'),
$this->reader->getFirstValueString($nsNode, 'coid://pusher.3rd-party.co/secret'),
$this->reader->getFirstValueString($nsNode, 'coid://pusher.3rd-party.co/appId'),
$options
);
break;
}
}
}
if (!isset($class))
throw new Exception("No rules defined to initialize <".$classname.">.");
$this->classes[$hashkey] = $class;
return $this->classes[$hashkey];
}
}