mirror of
https://github.com/wiosna-dev/limesurvey-api-client.git
synced 2026-03-12 10:11:49 +01:00
ParticipantService - getSurveyParticipants() method - catch and serve an exception while fetching participants of given survey
This commit is contained in:
@@ -16,6 +16,13 @@ namespace Meritoo\LimeSurvey\ApiClient\Exception;
|
||||
*/
|
||||
class CannotProcessDataException extends \Exception
|
||||
{
|
||||
/**
|
||||
* Reason why data cannot be processed, e.g. "Invalid user name or password"
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $reason;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
@@ -23,9 +30,21 @@ class CannotProcessDataException extends \Exception
|
||||
*/
|
||||
public function __construct($reason)
|
||||
{
|
||||
$this->reason = $reason;
|
||||
|
||||
$template = 'Raw data returned by the LimeSurvey\'s API cannot be processed. Reason: \'%s\'.';
|
||||
$message = sprintf($template, $reason);
|
||||
$message = sprintf($template, $this->reason);
|
||||
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns reason why data cannot be processed, e.g. "Invalid user name or password"
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getReason()
|
||||
{
|
||||
return $this->reason;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,12 @@ namespace Meritoo\LimeSurvey\ApiClient\Service;
|
||||
|
||||
use Meritoo\Common\Collection\Collection;
|
||||
use Meritoo\LimeSurvey\ApiClient\Client\Client;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\CannotProcessDataException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Exception\MissingParticipantOfSurveyException;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Collection\Participants;
|
||||
use Meritoo\LimeSurvey\ApiClient\Result\Item\Participant;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\MethodType;
|
||||
use Meritoo\LimeSurvey\ApiClient\Type\ReasonType;
|
||||
|
||||
/**
|
||||
* Service that serves participants
|
||||
@@ -69,6 +71,8 @@ class ParticipantService
|
||||
*
|
||||
* @param int $surveyId ID of survey
|
||||
* @return Collection
|
||||
*
|
||||
* @throws CannotProcessDataException
|
||||
*/
|
||||
public function getSurveyParticipants($surveyId)
|
||||
{
|
||||
@@ -81,10 +85,24 @@ class ParticipantService
|
||||
$surveyId,
|
||||
];
|
||||
|
||||
$participants = $this
|
||||
->client
|
||||
->run(MethodType::LIST_PARTICIPANTS, $arguments)
|
||||
->getData();
|
||||
try {
|
||||
$participants = $this
|
||||
->client
|
||||
->run(MethodType::LIST_PARTICIPANTS, $arguments)
|
||||
->getData();
|
||||
} catch (CannotProcessDataException $exception) {
|
||||
$reason = $exception->getReason();
|
||||
|
||||
/*
|
||||
* Reason of the exception is different than "Oops, there is no participants. Everything else is fine."?
|
||||
* Let's throw the exception
|
||||
*/
|
||||
if (ReasonType::NO_PARTICIPANTS_FOUND !== $reason) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
$participants = new Collection();
|
||||
}
|
||||
|
||||
$this
|
||||
->allParticipants
|
||||
|
||||
28
src/Type/ReasonType.php
Normal file
28
src/Type/ReasonType.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Meritoo\LimeSurvey\ApiClient\Type;
|
||||
|
||||
use Meritoo\Common\Type\Base\BaseType;
|
||||
|
||||
/**
|
||||
* Type of reason used by LimeSurvey's exception
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class ReasonType extends BaseType
|
||||
{
|
||||
/**
|
||||
* Reason of exception when there is no participants of survey
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const NO_PARTICIPANTS_FOUND = 'No survey participants found.';
|
||||
|
||||
/**
|
||||
* Reason of exception when there is no table with tokens/participants of survey
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const NO_TOKEN_TABLE = 'Error: No token table';
|
||||
}
|
||||
Reference in New Issue
Block a user