JRA, PHP cAPI interface
  • Namespace
  • Class
  • Tree

Namespaces

  • JRA
    • Abstracts
    • Commands
      • Invokers
      • Methods
        • Content
        • Token
        • User
      • Routes
    • ComposerHandler
    • Config
      • Helper
    • Exceptions
    • Facades
    • Factories
    • Interfaces
    • Objects
      • Response
        • Data
    • Responsibilities
      • Handlers
      • Responsible
        • Login
        • Logout

Classes

  • JRA\Abstracts\AbstractCommand
  • JRA\Abstracts\AbstractMethod
  • JRA\Commands\ContentCommand
  • JRA\Commands\Invokers\CommandInvoker
  • JRA\Commands\Methods\Content\DeleteContentId
  • JRA\Commands\Methods\Content\GetCategoryFields
  • JRA\Commands\Methods\Content\GetCategoryListAll
  • JRA\Commands\Methods\Content\GetCategoryListIdCategories
  • JRA\Commands\Methods\Content\GetCategoryListIdContent
  • JRA\Commands\Methods\Content\GetCategorySingleId
  • JRA\Commands\Methods\Content\GetContentFields
  • JRA\Commands\Methods\Content\GetContentListAll
  • JRA\Commands\Methods\Content\GetContentSingleId
  • JRA\Commands\Methods\Content\PostContentCreate
  • JRA\Commands\Methods\Content\PostContentUpdateId
  • JRA\Commands\Methods\Token\GetTokenToken
  • JRA\Commands\Methods\User\GetUser
  • JRA\Commands\Methods\User\GetUserLoginUsernamePassword
  • JRA\Commands\Methods\User\GetUserLogoutUserSession
  • JRA\Commands\Methods\User\GetUserSessions
  • JRA\Commands\Methods\User\PutUserProfile
  • JRA\Commands\Methods\User\PutUserProfileId
  • JRA\Commands\Routes\ContentRoutes
  • JRA\Commands\Routes\TokenRoutes
  • JRA\Commands\Routes\UserRoutes
  • JRA\Commands\TokenCommand
  • JRA\Commands\UserCommand
  • JRA\ComposerHandler\GuzzleHandler
  • JRA\Config\ARSAnnatech
  • JRA\Config\Authenticate
  • JRA\Config\Helper\APIRoutes
  • JRA\Config\Helper\Versions
  • JRA\Config\Server
  • JRA\Config\Version
  • JRA\Facades\APIFacade
  • JRA\Facades\ConfigFacade
  • JRA\Factories\ChainFactory
  • JRA\Factories\CommandFactory
  • JRA\Factories\ConfigFactory
  • JRA\Factories\GuzzleFactory
  • JRA\Factories\InternalFactory
  • JRA\FrontFactory
  • JRA\Objects\CreateContentObject
  • JRA\Objects\Response\CategoryFieldsResponseObject
  • JRA\Objects\Response\CategoryListResponseObject
  • JRA\Objects\Response\CategoryResponseObject
  • JRA\Objects\Response\ContentDeleteResponseObject
  • JRA\Objects\Response\ContentFieldsResponseObject
  • JRA\Objects\Response\ContentListResponseObject
  • JRA\Objects\Response\ContentResponseObject
  • JRA\Objects\Response\Data\CategoryObject
  • JRA\Objects\Response\Data\ContentObject
  • JRA\Objects\Response\Data\DatabaseFieldObject
  • JRA\Objects\Response\Data\SessionObject
  • JRA\Objects\Response\JResponseObject
  • JRA\Objects\Response\MsgResponseObject
  • JRA\Objects\Response\SessionResponseObject
  • JRA\Objects\Response\UserLoginResponseObject
  • JRA\Objects\Response\UserLogoutResponseObject
  • JRA\Objects\StateObject
  • JRA\Objects\UpdateContentObject
  • JRA\Objects\UpdateUserProfileObject
  • JRA\Responsibilities\Handlers\AbstractLoginHandler
  • JRA\Responsibilities\Handlers\AbstractLogoutHandler
  • JRA\Responsibilities\Responsible\Login\HeaderTokenLogin
  • JRA\Responsibilities\Responsible\Login\TokenLogin
  • JRA\Responsibilities\Responsible\Login\UserCredentialsLogin
  • JRA\Responsibilities\Responsible\Logout\TokenLogout
  • JRA\Responsibilities\Responsible\Logout\UserCredentialsLogout

Interfaces

  • JRA\Interfaces\ConfigInterface

Exceptions

  • JRA\Exceptions\APIException
  • JRA\Exceptions\AuthenticationConfigException
  • JRA\Exceptions\CommandException
  • JRA\Exceptions\JRAException
  • JRA\Exceptions\ServerConfigException
  • JRA\Exceptions\TokenConfigException
  • JRA\Exceptions\UserAPIException
  • JRA\Exceptions\UserCredentialsConfigException
  • JRA\Exceptions\VersionException
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 
<?php

/**
 * Created by PhpStorm.
 * Copyright: Bertschi AG, 2015
 * User: jbaumann
 * File: Server.php
 * Date: 23.12.2015
 * Time: 08:39
 */

namespace JRA\Config;


use JRA\Exceptions\ServerConfigException;
use ReflectionClass;

class Server
{

    /**
     * The generated url to the Joomla instance with the full qualified path name to the current used cAPI version.
     *
     * @var string
     */
    private $_restAPIUrl;

    /**
     * The version of the used cAPI version.
     *
     * @var int
     */
    private $_version;

    /**
     * The protocol of the url.
     *
     * @var string
     */
    private $_scheme;

    /**
     * The hostname of the url.
     *
     * @var string
     */
    private $_host;

    /**
     * The port of the url.
     *
     * @var int
     */
    private $_port;

    /**
     * The username of the url.
     *
     * @var string
     */
    private $_user;

    /**
     * The password of the url.
     *
     * @var string
     */
    private $_pass;

    /**
     * The path of the url.
     *
     * @var string
     */
    private $_path;

    /**
     * The query of the url.
     *
     * @var string
     */
    private $_query;

    /**
     * The fragment of the url.
     *
     * @var string
     */
    private $_fragment;

    /**
     * Server constructor.
     *
     * @param int $pVersion The version of the used cAPI.
     */
    public function __construct($pVersion)
    {
        $this->_version = $pVersion;
    }

    /**
     * Sets the url.
     * Starts the parsing of the url.
     * Starts the validation of the url.
     * Starts the generation of the url.
     *
     * @param string $pUrl The url which should be set.
     * @throws ServerConfigException Throws an exception if the url is already defined.
     */
    public function setUrl($pUrl)
    {
        if ($this->_restAPIUrl === null) {
            $this->_defineUrlComponents($pUrl);
            $this->_validateUrlComponents();
            $this->_generateRestAPIUrl();
        } else {
            throw new ServerConfigException('Url is already defined.', ServerConfigException::SERVER_CONFIG_EXCEPTION_ALREADY_DEFINED);
        }
    }

    /**
     * Distributes the url in its components.
     *
     * @param string $pUrl The url which should be parsed.
     * @throws ServerConfigException Throws an exception if the url is not valid.
     */
    private function _defineUrlComponents($pUrl)
    {
        $parsedUrl = parse_url($pUrl);
        if ($parsedUrl) {
            $this->_scheme = (isset($parsedUrl['scheme'])) ? $parsedUrl['scheme'] : null;
            $this->_host = (isset($parsedUrl['host'])) ? $parsedUrl['host'] : null;
            $this->_port = (isset($parsedUrl['port'])) ? $parsedUrl['port'] : null;
            $this->_user = (isset($parsedUrl['user'])) ? $parsedUrl['user'] : null;
            $this->_pass = (isset($parsedUrl['pass'])) ? $parsedUrl['pass'] : null;
            $this->_path = (isset($parsedUrl['path'])) ? $parsedUrl['path'] : null;
            $this->_query = (isset($parsedUrl['query'])) ? $parsedUrl['query'] : null;
            $this->_fragment = (isset($parsedUrl['fragment'])) ? $parsedUrl['fragment'] : null;
        } else {
            throw new ServerConfigException('Url is not valid.', ServerConfigException::SERVER_CONFIG_EXCEPTION_URL_NOT_VALID);
        }
    }

    /**
     * Validates the url and sets possibly and standard value if no one is specified.
     *
     * @throws ServerConfigException Throws an exception if a part of the url could not be generated and there is no way to specify a valid alternate string.
     */
    private function _validateUrlComponents()
    {
        if (empty($this->_scheme)) {
            $this->_scheme = 'http';
        }
        if (empty($this->_host)) {
            throw new ServerConfigException('Host is not valid.', ServerConfigException::SERVER_CONFIG_EXCEPTION_HOST_NOT_VALID);
        }
        if (empty($this->_port) || $this->_port === 80) {
            $this->_port = null;
        }
        if (empty($this->_user) || empty($this->_pass)) {
            $this->_user = null;
            $this->_pass = null;
        }
        if (array_search($this->_path . '/', (new ReflectionClass('JRA\Config\Helper\APIRoutes'))->getConstants())) {
            $this->_path = null;
        }
    }

    /**
     * Generates the url which is used for all request from the JRA.
     */
    private function _generateRestAPIUrl()
    {
        $url = '';
        $url .= $this->_scheme . '://';
        if ($this->_user !== null && $this->_pass !== null) {
            $url .= $this->_user . ':' . $this->_pass . '@';
        }
        $url .= $this->_host;
        if ($this->_port !== null) {
            $url .= ':' . $this->_port;
        }
        if ($this->_path !== null) {
            $url .= $this->_path;
            if (substr($this->_path, -1) !== '/') {
                $url .= '/';
            }
        } else {
            $url .= '/';
        }
        if ($versionKey = array_search($this->_version, (new ReflectionClass('JRA\Config\Helper\Versions'))->getConstants())) {
            $url .= (new ReflectionClass('JRA\Config\Helper\APIRoutes'))->getConstants()[$versionKey];
        }
        $this->_restAPIUrl = $url;
    }

    /**
     * Returns the base url which is used for all further requests.
     *
     * @return string The base url for cAPI calls.
     * @throws ServerConfigException Throws an exception if the url is undefined.
     */
    public function getRestAPIUrl()
    {
        if ($this->_restAPIUrl === null) {
            throw new ServerConfigException('Url is undefined.', ServerConfigException::SERVER_CONFIG_EXCEPTION_URL_UNDEFINED);
        } else {
            return $this->_restAPIUrl;
        }
    }
}
JRA, PHP cAPI interface API documentation generated by ApiGen