mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 10:11:49 +01:00
composer.json - move tests-related classes to "autoload-dev" section (used for development purposes only and avoid polluting the autoloader in production)
This commit is contained in:
107
src/Manager/JsonRpcClientManager.php
Normal file
107
src/Manager/JsonRpcClientManager.php
Normal file
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Manager;
|
||||
|
||||
use JsonRPC\Client as RpcClient;
|
||||
use JsonRPC\Exception\InvalidJsonFormatException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Configuration\ConnectionConfiguration;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\InvalidResultOfMethodRunException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\UnknownMethodException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
|
||||
/**
|
||||
* Manager of the JsonRPC client used while connecting to LimeSurvey's API
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class JsonRpcClientManager
|
||||
{
|
||||
/**
|
||||
* Configuration used while connecting to LimeSurvey's API
|
||||
*
|
||||
* @var ConnectionConfiguration
|
||||
*/
|
||||
private $connectionConfiguration;
|
||||
|
||||
/**
|
||||
* The JsonRPC client used while connecting to LimeSurvey's API
|
||||
*
|
||||
* @var RpcClient
|
||||
*/
|
||||
private $rpcClient;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param ConnectionConfiguration $configuration Configuration used while connecting to LimeSurvey's API
|
||||
*/
|
||||
public function __construct(ConnectionConfiguration $configuration)
|
||||
{
|
||||
$this->connectionConfiguration = $configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs given method with given arguments and returns raw data
|
||||
*
|
||||
* @param string $method Name of method to call. One of the MethodType class constants.
|
||||
* @param array $arguments (optional) Arguments of the method to call
|
||||
* @return mixed
|
||||
*
|
||||
* @throws UnknownMethodException
|
||||
* @throws InvalidResultOfMethodRunException
|
||||
*/
|
||||
public function runMethod($method, $arguments = [])
|
||||
{
|
||||
$result = null;
|
||||
$method = MethodType::getValidatedMethod($method);
|
||||
|
||||
try {
|
||||
$result = $this
|
||||
->getRpcClient()
|
||||
->execute($method, $arguments);
|
||||
} catch (InvalidJsonFormatException $exception) {
|
||||
throw new InvalidResultOfMethodRunException($exception, $method);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the JsonRPC client used while connecting to LimeSurvey's API
|
||||
*
|
||||
* @return RpcClient
|
||||
*/
|
||||
protected function getRpcClient()
|
||||
{
|
||||
if (null === $this->rpcClient) {
|
||||
/*
|
||||
* Let's prepare the JsonRPC Client
|
||||
*/
|
||||
$url = $this->connectionConfiguration->getFullUrl();
|
||||
$this->rpcClient = new RpcClient($url);
|
||||
|
||||
/*
|
||||
* The "debug" mode is turned on?
|
||||
*/
|
||||
if ($this->connectionConfiguration->isDebugModeOn()) {
|
||||
$this
|
||||
->rpcClient
|
||||
->getHttpClient()
|
||||
->withDebug();
|
||||
}
|
||||
|
||||
/*
|
||||
* The SSL certificate verification is turned off?
|
||||
*/
|
||||
if (!$this->connectionConfiguration->isVerifySslCertificateOn()) {
|
||||
$this
|
||||
->rpcClient
|
||||
->getHttpClient()
|
||||
->withoutSslVerification();
|
||||
}
|
||||
}
|
||||
|
||||
return $this->rpcClient;
|
||||
}
|
||||
}
|
||||
115
src/Manager/SessionManager.php
Normal file
115
src/Manager/SessionManager.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Manager;
|
||||
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\CreateSessionKeyFailedException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\SystemMethodType;
|
||||
|
||||
/**
|
||||
* Manager of session started while connecting to LimeSurvey's API
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class SessionManager
|
||||
{
|
||||
/**
|
||||
* The session key.
|
||||
* Used to authenticate user while connecting to LimeSurvey's API.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $sessionKey;
|
||||
|
||||
/**
|
||||
* Manager of the JsonRPC client used while connecting to LimeSurvey's API
|
||||
*
|
||||
* @var JsonRpcClientManager
|
||||
*/
|
||||
private $rpcClientManager;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param JsonRpcClientManager $rpcClientManager Manager of the JsonRPC client used while connecting to
|
||||
* LimeSurvey's API
|
||||
*/
|
||||
public function __construct(JsonRpcClientManager $rpcClientManager)
|
||||
{
|
||||
$this->rpcClientManager = $rpcClientManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class destructor
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->releaseSessionKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns key/id of session used while connecting to LimeSurvey's API
|
||||
*
|
||||
* @param string $username Name of user used to authenticate to LimeSurvey
|
||||
* @param string $password Password used to authenticate to LimeSurvey
|
||||
* @return string
|
||||
*
|
||||
* @throws CreateSessionKeyFailedException
|
||||
*/
|
||||
public function getSessionKey($username, $password)
|
||||
{
|
||||
if (null === $this->sessionKey) {
|
||||
$arguments = [
|
||||
$username,
|
||||
$password,
|
||||
];
|
||||
|
||||
/*
|
||||
* Let's fetch the key/id of session
|
||||
*/
|
||||
$this->sessionKey = $this
|
||||
->rpcClientManager
|
||||
->runMethod(SystemMethodType::GET_SESSION_KEY, $arguments);
|
||||
|
||||
/*
|
||||
* Oops, something is broken
|
||||
*/
|
||||
if (is_array($this->sessionKey)) {
|
||||
$reason = '';
|
||||
|
||||
/*
|
||||
* The "status" is provided?
|
||||
* It's a reason of failure
|
||||
*/
|
||||
if (isset($this->sessionKey['status'])) {
|
||||
$reason = $this->sessionKey['status'];
|
||||
}
|
||||
|
||||
throw new CreateSessionKeyFailedException($reason);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->sessionKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases key/id of session and closes the RPC session
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function releaseSessionKey()
|
||||
{
|
||||
$arguments = [
|
||||
$this->sessionKey,
|
||||
];
|
||||
|
||||
/*
|
||||
* Let's release the key/id of session
|
||||
*/
|
||||
$this
|
||||
->rpcClientManager
|
||||
->runMethod(SystemMethodType::RELEASE_SESSION_KEY, $arguments);
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user