<commit_before><?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\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 and return the SDK with the given classname.
     * Throws Exception if the SDK is not supported.
     * 
     * @param $classname Classname for the SDK's main class
     * @param array $options Additional options for the SDK (if necessary)
     */
    public function get($classname, array $options) {
        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->getAuthenticatingNamespaceObject();

            // --- 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://amazonws.cloudobjects.io/accessKeyId'),
                        'secret' => $this->reader->getFirstValueString($nsNode, 'coid://amazonws.cloudobjects.io/secretAccessKey')
                    ]
                ]));
            } else {
                switch ($classname) {

                    // --- stream (https://getstream.io/) ---
                    case "GetStream\Stream\Client":
                        $class = new $classname(
                            $this->reader->getFirstValueString($nsNode, 'coid://getstreamio.cloudobjects.io/key'),
                            $this->reader->getFirstValueString($nsNode, 'coid://getstreamio.cloudobjects.io/secret')
                        );
                        break;

                    // --- Pusher (https://pusher.com/) ---
                    case "Pusher":
                        $class = new $classname(
                            $this->reader->getFirstValueString($nsNode, 'coid://pusher.cloudobjects.io/key'),
                            $this->reader->getFirstValueString($nsNode, 'coid://pusher.cloudobjects.io/secret'),
                            $this->reader->getFirstValueString($nsNode, 'coid://pusher.cloudobjects.io/appId'),
                            $options
                    );
                    break;
                }
            }
        }

        if (!isset($class))<commit_msg>Namespace refactorings for 3rd party SDKs<commit_after><?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\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 and return the SDK with the given classname.
     * Throws Exception if the SDK is not supported.
     * 
     * @param $classname Classname for the SDK's main class
     * @param array $options Additional options for the SDK (if necessary)
     */
    public function get($classname, array $options) {
        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->getAuthenticatingNamespaceObject();

            // --- 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))
{'fraction_non_alphanumeric': 0.12383570010688655, 'fraction_numerical': 0.002748511223087494, 'mean_word_length': 1.8220594571305473, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 49, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2217743', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1697, 'n_words': 408}