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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 
<?php
/**
 * Created by PhpStorm.
 * Copyright: Bertschi AG, 2016
 * User: jbaumann
 * File: AbstractCommand.php
 * Date: 18.01.2016
 * Time: 08:59
 */

namespace JRA\Abstracts;


use JRA\Factories\InternalFactory;
use JRA\Interfaces\ConfigInterface;

abstract class AbstractCommand
{
    /**
     * True if an auto login should be performed, false otherwise
     *
     * @var boolean
     */
    private $_autoLogin;

    /**
     * These arguments contains the values for replacing the keys in the route.
     * It also contains addition arguments, which are over given in the request body.
     *
     * @var array
     */
    private $_additionalArguments;

    /**
     * The response of the web request (only the html)
     *
     * @var string
     */
    private $_response;

    /**
     * The words which should be replaced in the url
     *
     * @var string[]
     */
    protected $_keyWords;

    /**
     * The http method (options, get, head, post, put, delete)
     *
     * @var string
     */
    private $_httpMethod;

    /**
     * The configuration for the current JRA execution
     *
     * @var ConfigInterface
     */
    protected $_config;

    /**
     * The factory for internal use
     *
     * @var InternalFactory
     */
    protected $_factory;

    /**
     * The method which should be executed
     *
     * @var AbstractMethod
     */
    private $_method;

    /**
     * The route or URL for the current method
     *
     * @var string
     */
    private $_methodRoute;

    /**
     * The params which will be attached to the url
     *
     * @var array
     */
    private $_urlParams;

    /**
     * AbstractCommand constructor.
     *
     * @param ConfigInterface $pConfig
     * @param InternalFactory $pInternalFactory
     */
    public function __construct(ConfigInterface $pConfig, InternalFactory $pInternalFactory)
    {
        $this->_config = $pConfig;
        $this->_factory = $pInternalFactory;
    }

    /**
     * Returns true if auto login is on, false otherwise.
     *
     * @return boolean
     */
    public function isAutoLogin()
    {
        return $this->_autoLogin;
    }

    /**
     * Sets the auto login to the specified value.
     *
     * @param boolean $pValue
     */
    protected function setAutoLogin($pValue)
    {
        $this->_autoLogin = $pValue;
    }

    /**
     * Get the additional arguments which are for replacing the keys in the routes.
     * It also contains additional arguments which will be sent with the request body.
     *
     * @return array
     */
    public function getAdditionalArguments()
    {
        return $this->_additionalArguments;
    }

    /**
     * Set the additional arguments which are for replacing the keys in the routes.
     * It also contains additional arguments which will be sent with the request body.
     *
     * @param array $pAdditionalArguments
     */
    public function setAdditionalArguments($pAdditionalArguments)
    {
        $this->_additionalArguments = $pAdditionalArguments;
    }

    /**
     * Get the response of the web request (only the html)
     *
     * @return string
     */
    public function getResponse()
    {
        return $this->_response;
    }

    /**
     * Set the response of the web request (only the html)
     *
     * @param string $pResponse
     */
    protected function setResponse($pResponse)
    {
        $this->_response = $pResponse;
    }

    /**
     * Processes the method and calls Guzzle for requesting the URL.
     */
    public function process()
    {
        $httpMethod = $this->getHttpMethod();
        $this->_response = $this->_factory
            ->getGuzzleFactory()
            ->getGuzzleHandler()
            ->$httpMethod(
                $this->getParsedUrl(),
                (($httpMethod === 'POST' || $httpMethod === 'PUT') ? $this->getAdditionalArguments() : null)
            )
            ->getBody(true);

    }

    /**
     * Checks the arguments, so that all necessary arguments are give.
     *
     * @return boolean Returns true if correct arguments are specified, false otherwise.
     */
    public function checkArguments()
    {
        if (empty($this->getMethodRoute())) {
            return false;
        }
        preg_match_all('/[:](\w+)/', $this->getMethodRoute(), $matches);
        $this->_keyWords = $matches[1];
        foreach ($this->_keyWords as $pValue) {
            if (empty($this->_additionalArguments[$pValue])) {
                return false;
            }
        }
        if ($this->_checkArguments()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * The methods need to check addition arguments by itself.
     *
     * @return boolean
     */
    abstract protected function _checkArguments();

    /**
     * Returns the parsed url (with keywords replaced and url params attached).
     *
     * @return string
     */
    public function getParsedUrl()
    {
        $route = $this->getMethodRoute();
        $additionalArguments = $this->getAdditionalArguments();

        foreach ($this->_keyWords as $pValue) {
            $route = str_replace(':' . $pValue, urlencode($additionalArguments[$pValue]), $route);
            unset($additionalArguments[$pValue]);
        }

        if (!empty($additionalArguments) && $this->getHttpMethod() !== 'POST' && $this->getHttpMethod() !== 'PUT') {
            $route .= '?';
            foreach ($additionalArguments as $pKey => $pValue) {
                $route .= urlencode($pKey) . '=' . urlencode($pValue) . '&';
            }
            $route = substr($route, 0, -1);
        }
        return $route;
    }

    /**
     * Returns the http method for the current method.
     *
     * @return string
     */
    public function getHttpMethod()
    {
        if (empty($this->_httpMethod)) {
            $className = get_class($this);
            preg_match('/\\\\([A-Z][a-z]*)\w*$/', $className, $match);
            $match = array_flip($match);
            $match = array_change_key_case($match, CASE_UPPER);
            $match = array_flip($match);
            $match = $match[1];
            $this->_httpMethod = $match;
        }
        return $this->_httpMethod;
    }

    /**
     * Returns the current specified method.
     *
     * @return AbstractMethod
     */
    protected function _getMethod()
    {
        return $this->_method;
    }

    /**
     * Sets the current specified method.
     *
     * @param AbstractMethod $pMethod
     */
    protected function _setMethod($pMethod)
    {
        $this->_method = $pMethod;
    }

    /**
     * Returns the none parsed route.
     *
     * @return string
     */
    public function getMethodRoute()
    {
        return $this->_methodRoute;
    }

    /**
     * Sets the none parsed route.
     *
     * @param string $pMethodRoute
     */
    public function setMethodRoute($pMethodRoute)
    {
        $this->_methodRoute = $pMethodRoute;
    }

    /**
     * Get all url params.
     *
     * @return array
     */
    public function getUrlParams()
    {
        return $this->_urlParams;
    }

    /**
     * Set all url params.
     *
     * @param array $urlParams
     */
    public function setUrlParams($urlParams)
    {
        foreach ($urlParams as $pKey => $pValue) {
            $this->_urlParams[] = $pKey . '=' . urlencode($pValue);
        }
    }
}
JRA, PHP cAPI interface API documentation generated by ApiGen