Scalr\Service\OpenStack\Services\AbstractService::getApiHandler PHP Method

getApiHandler() public method

Gets an API Handler for the service
public getApiHandler ( ) : object
return object Returns an API Handler for the service
    public function getApiHandler()
    {
        $class = get_class($this);
        if ($this->apiHandler === null) {
            //This method is declared in the ServiceInterface and must be defined in children classes.
            if (!$this->getOpenStack()->getConfig()->getAuthToken()) {
                $this->getOpenStack()->auth();
            }
            $ver = $this->getVersion();
            $config = $this->getOpenStack()->getConfig();
            $type = $this->getType();
            $region = $config->getRegion();
            try {
                $config->getAuthToken()->getEndpointUrl($type, $region, substr($ver, 1));
            } catch (OpenStackException $e) {
                $endpoints = $config->getAuthToken()->getRegionEndpoints();
                if (empty($endpoints[$type][$region])) {
                    throw $e;
                }
                $supported = array_map(function ($v) {
                    return substr($v, 1);
                }, $this->getSupportedVersions());
                $versions = array_intersect($supported, array_keys($endpoints[$type][$region]));
                if (empty($versions)) {
                    throw new NotSupportedException(sprintf("There is not version number which is supported for the %s service. Available: %s, Supported: %s", $type, join(", ", array_keys($endpoints[$type][$region])), join(", ", $supported)));
                }
                $ver = 'V' . array_shift($versions);
                $this->setVersion($ver);
            }
            $name = self::getOriginalServiceName($class);
            if ($name === null) {
                throw new ServiceException(sprintf('Invalid service interface class name "%s". It should end with "Service".', $class));
            }
            $apiClass = __NAMESPACE__ . '\\' . $name . '\\' . $ver . '\\' . $name . 'Api';
            $this->apiHandler = new $apiClass($this);
        }
        return $this->apiHandler;
    }