Skip to content

Commit

Permalink
Qe 585 refactor prepare functions to transformer classes (#3675)
Browse files Browse the repository at this point in the history
* refactor prepareAdvancedSettings() into TransformerInputQuestionAttribute

* refactor getTransformedLanguageProps() into TransformerInputLanguageTrait

* Added missing import of OpHandlerExceptionTrait

* Added one more missing import of OpHandlerExceptionTrait

* dev: refactor op handler transform and validation (work in progress)

* dev: refactor op handler transform and validation (work in progress)

* dev: refactor op handler transform and validation (work in progress)

* dev: refactor op handler transform and validation (work in progress)

* dev: refactor op handler transform and validation (work in progress)

* dev: refactor op handler transform and validation (work in progress)

* dev: refactor op handler transform and validation (work in progress)

* dev: FormatterYnToBool should implement FormatterInterface

* dev: refactor transformer

* dev: refactor transformer

* dev: refactor op handler transform and validation (work in progress)

* dev: api transformer required option can be operation specific

* dev: add transformer tests

* dev: add transformer tests

* dev: transformer 'required' option can be operation specific

* dev: transformer 'required' option can be operation specific

* dev: fix subquestion transformation

* dev: fix OpHandlerQuestionUpdate

* dev: refactor OpHandlerLanguageSettingsUpdate

* dev: refactor api transformers

* dev: refactor op handler tests

* dev: refactor op handler tests

* dev: refactor OpHandlerQuestionL10nUpdate

* dev: fix psalm errors

* dev: fix php mess detector issues

* Fix for getLanguageSettingsData() variant 2 not recognized

* Fix for getLanguageSettingsData() variant 2 not recognized (other approach)

* Fix for getLanguageSettingsData() variant 2 not recognized (mixed approach)

* Adjusted comment

* dev: fix subquestion ophandler l10ns

* dev: mess detector fix

* dev: transfomer input question qid is not required

* Fix for L10n related OpHandlers

* dev: revertclear

* dev: fix QuestionAggregateService/SubQuestionsService

* dev: revert SubQuestionsService

* Fix TransformerInputQuestionGroupAggregate.php and passing of options into nested valdiations and transforms

* Fix OpHandlerQuestionGroup.php create

* Fixed issue: mandatory "S" was not handled anymore in OpHandlerQuestionUpdate

* Fixed issue: mandatory "S" was not handled anymore in OpHandlerQuestionUpdate (better approach)

* Fixed issue: TransformerInputSubQuestionL10ns.php was not returning the expected structure

* Fixed issue: TransformerInputQuestion was expecting sid to be required on create

* Fixed issue: TransformerInputSubQuestionL10ns.php was not returning the expected structure (psalm fix)

* Fixed issue: tempId mapping for subquestions wasn't working anymore after internal change from "title" to "code"

* new TransformerInputAnswerTest.php

* new TransformerInputAnswerTest.php (added tempId)

* new TransformerInputAnswerTest.php (added language)

* new TransformerInputAnswerTest.php (fix array levels)

* new Transformer test for null values

* new Transformer tests on selected entities handled by TransformerInputQuestionAggregate

* new Transformer tests on selected entities handled by TransformerInputQuestionAggregate (fix)

* new Transformer tests on selected entities handled by TransformerInputQuestionAggregate (fix tempId)

* new Transformer tests on selected entities handled by TransformerInputQuestionAggregate (fix language)

* dev: composer update

* dev: TransformerOutputSurveyGroup::transform signature

---------

Co-authored-by: twilligls <tim.willig@limesurvey.org>
  • Loading branch information
kevin-foster-uk and twilligls committed Jan 18, 2024
1 parent bb4112e commit 2aa2d63
Show file tree
Hide file tree
Showing 78 changed files with 2,300 additions and 1,409 deletions.
2 changes: 1 addition & 1 deletion application/libraries/Api/Command/Exception.php
Expand Up @@ -2,6 +2,6 @@

namespace LimeSurvey\Api\Command;

class Exception extends \LimeSurvey\Api\Exception
class Exception extends \LimeSurvey\Api\ApiException
{
}
Expand Up @@ -2,43 +2,42 @@

namespace LimeSurvey\Api\Command\V1\SurveyPatch;

use LimeSurvey\Api\Command\V1\Transformer\{
Input\TransformerInputAnswer,
Input\TransformerInputAnswerL10ns,
};
use LimeSurvey\Api\Command\V1\Transformer\Input\TransformerInputAnswer;
use LimeSurvey\Api\Command\V1\SurveyPatch\Traits\{
OpHandlerQuestionTrait,
OpHandlerSurveyTrait,
OpHandlerQuestionTrait,
OpHandlerExceptionTrait
};
use LimeSurvey\Models\Services\QuestionAggregateService\QuestionService;
use LimeSurvey\ObjectPatch\{Op\OpInterface,
use LimeSurvey\ObjectPatch\{
Op\OpInterface,
OpType\OpTypeCreate,
OpHandler\OpHandlerException,
OpHandler\OpHandlerInterface,
OpType\OpTypeUpdate
};
use LimeSurvey\Models\Services\QuestionAggregateService\AnswersService;
use LimeSurvey\Models\Services\QuestionAggregateService\{
QuestionService,
AnswersService
};

class OpHandlerAnswer implements OpHandlerInterface
{
use OpHandlerSurveyTrait;
use OpHandlerQuestionTrait;
use OpHandlerExceptionTrait;

protected string $entity;
protected TransformerInputAnswer $transformerAnswer;
protected TransformerInputAnswerL10ns $transformerAnswerL10n;
protected TransformerInputAnswer $transformer;
protected AnswersService $answersService;
protected QuestionService $questionService;

public function __construct(
TransformerInputAnswer $transformerAnswer,
TransformerInputAnswerL10ns $transformerAnswerL10n,
TransformerInputAnswer $transformer,
AnswersService $answersService,
QuestionService $questionService
) {
$this->entity = 'answer';
$this->transformerAnswer = $transformerAnswer;
$this->transformerAnswerL10n = $transformerAnswerL10n;
$this->transformer = $transformer;
$this->answersService = $answersService;
$this->questionService = $questionService;
}
Expand Down Expand Up @@ -159,33 +158,37 @@ public function canHandle(OpInterface $op): bool
* @param OpInterface $op
* @return array
* @throws OpHandlerException
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
* @throws \LimeSurvey\Models\Services\Exception\NotFoundException
* @throws \LimeSurvey\Models\Services\Exception\PermissionDeniedException
* @throws \LimeSurvey\Models\Services\Exception\PersistErrorException
*/
public function handle(OpInterface $op): array
{
$this->throwTransformerValidationErrors(
$this->transformer->validateAll(
$op->getProps(),
['operation' => $op->getType()->getId()]
),
$op
);

$question = $this->questionService->getQuestionBySidAndQid(
$this->getSurveyIdFromContext($op),
$op->getEntityId()
);
$preparedData = $this->prepareAnswers(
$op,

$data = $this->transformer->transformAll(
$op->getProps(),
$this->transformerAnswer,
$this->transformerAnswerL10n,
['answer', 'answerL10n']
['operation' => $op->getType()->getId()]
);
$this->answersService->save(
$question,
$preparedData
$data
);

return $this->getSubQuestionNewIdMapping(
$question,
$preparedData,
$data,
true
);
}
Expand Down
Expand Up @@ -4,6 +4,7 @@

use DI\DependencyException;
use DI\NotFoundException;
use LimeSurvey\Api\Command\V1\SurveyPatch\Traits\OpHandlerExceptionTrait;
use SurveyLanguageSetting;
use LimeSurvey\Api\Command\V1\Transformer\{
Input\TransformerInputSurveyLanguageSettings,
Expand All @@ -25,6 +26,7 @@
class OpHandlerLanguageSettingsUpdate implements OpHandlerInterface
{
use OpHandlerSurveyTrait;
use OpHandlerExceptionTrait;

protected string $entity;
protected SurveyLanguageSetting $model;
Expand All @@ -43,7 +45,6 @@ public function canHandle(OpInterface $op): bool
{
$isUpdateOperation = $op->getType()->getId() === OpTypeUpdate::ID;
$isLanguageSettingEntity = $op->getEntityType() === 'languageSetting';

return $isUpdateOperation && $isLanguageSettingEntity;
}

Expand All @@ -55,10 +56,7 @@ public function canHandle(OpInterface $op): bool
* {
* "entity": "languageSetting",
* "op": "update",
* "id": {
* "sid": 123456, //todo do we still need the sid here?
* "language": "de"
* },
* "id": "de",
* "props": {
* "title": "Beispielfragebogen"
* }
Expand All @@ -71,9 +69,7 @@ public function canHandle(OpInterface $op): bool
* {
* "entity": "languageSetting",
* "op": "update",
* "id": {
* "sid": 123456 //todo do we still need the sid here?
* },
* "id": null,
* "props": {
* "de": {
* "title": "Beispielfragebogen"
Expand All @@ -98,52 +94,45 @@ public function handle(OpInterface $op): void
$languageSettings = $diContainer->get(
LanguageSettings::class
);

$languageSettings->update(
$this->getSurveyIdFromContext($op),
$this->getLanguageSettingsData($op)
$this->getLanguageSettingsData($op) ?? []
);
}

/**
* Analyzes the patch, builds and returns the correct data structure
* @param OpInterface $op
* @return array
* @return ?mixed
* @throws OpHandlerException
*/
public function getLanguageSettingsData(OpInterface $op)
{
$data = [];
$transformOps = ['operation' => $op->getType()->getId()];
$props = [];
$entityId = $op->getEntityId();
if (is_array($entityId) && array_key_exists('language', $entityId)) {
if (!empty($entityId)) {
// indicator for variant 1
$data[$entityId['language']] = $this->getTransformedProps(
$op,
$op->getProps()
);
$props[$entityId] = $op->getProps();
} else {
// variant 2
foreach ($op->getProps() as $language => $props) {
$data[$language] = $this->getTransformedProps($op, $props);
}
$props = $op->getProps();
}

return $data;
}

/**
* @param OpInterface $op
* @param array|null $props
* @return mixed
* @throws OpHandlerException
*/
private function getTransformedProps(OpInterface $op, ?array $props)
{
$transformedProps = $this->transformer->transform($props);
if ($props === null || $transformedProps === null) {
$this->throwNoValuesException($op);
foreach (array_keys($props) as $language) {
$props[$language]['sid'] = $this->getSurveyIdFromContext($op);
$props[$language]['language'] = $language;
}
return $transformedProps;
$this->throwTransformerValidationErrors(
$this->transformer->validateAll(
$props,
$transformOps
),
$op
);
return $this->transformer->transformAll(
$props,
$transformOps
);
}

/**
Expand Down
Expand Up @@ -4,7 +4,6 @@

use LimeSurvey\Api\Command\V1\Transformer\Input\TransformerInputQuestionAttribute;
use LimeSurvey\Api\Command\V1\SurveyPatch\Traits\{
OpHandlerQuestionTrait,
OpHandlerSurveyTrait
};
use LimeSurvey\Models\Services\{
Expand All @@ -23,7 +22,6 @@
class OpHandlerQuestionAttributeUpdate implements OpHandlerInterface
{
use OpHandlerSurveyTrait;
use OpHandlerQuestionTrait;

protected string $entity;
protected AttributesService $attributesService;
Expand Down Expand Up @@ -98,12 +96,7 @@ public function handle(OpInterface $op): void
{
$surveyId = $this->getSurveyIdFromContext($op);
$this->questionAggregateService->checkUpdatePermission($surveyId);
$preparedData = $this->prepareAdvancedSettings(
$op,
$this->transformer,
$op->getProps(),
['attributes']
);
$preparedData = $this->transformer->transformAll($op->getProps());
$questionId = $op->getEntityId();
$this->attributesService->saveAdvanced(
$this->questionService->getQuestionBySidAndQid(
Expand All @@ -121,7 +114,7 @@ public function handle(OpInterface $op): void
*/
public function isValidPatch(OpInterface $op): bool
{
// prepareAdvancedSettings is taking care of validation
// transformAll is taking care of validation
return true;
}
}

0 comments on commit 2aa2d63

Please sign in to comment.