/
OpHandlerAnswerDelete.php
84 lines (75 loc) · 2.18 KB
/
OpHandlerAnswerDelete.php
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
<?php
namespace LimeSurvey\Api\Command\V1\SurveyPatch;
use LimeSurvey\Api\Command\V1\SurveyPatch\Traits\OpHandlerSurveyTrait;
use LimeSurvey\Models\Services\{
Exception\PermissionDeniedException,
QuestionAggregateService
};
use LimeSurvey\Api\Command\V1\SurveyPatch\Traits\OpHandlerValidationTrait;
use LimeSurvey\ObjectPatch\{
Op\OpInterface,
OpHandler\OpHandlerException,
OpHandler\OpHandlerInterface,
OpType\OpTypeDelete
};
class OpHandlerAnswerDelete implements OpHandlerInterface
{
use OpHandlerSurveyTrait;
use OpHandlerValidationTrait;
protected QuestionAggregateService $questionAggregateService;
/**
* @param QuestionAggregateService $questionAggregateService
*/
public function __construct(
QuestionAggregateService $questionAggregateService
) {
$this->questionAggregateService = $questionAggregateService;
}
/**
* @param OpInterface $op
* @return bool
*/
public function canHandle(OpInterface $op): bool
{
$isDeleteOperation = $op->getType()->getId() === OpTypeDelete::ID;
$isAnswerEntity = $op->getEntityType() === 'answer';
return $isAnswerEntity && $isDeleteOperation;
}
/**
* Deletes an answer from the question.
* This is the expected structure:
* "patch": [
* {
* "entity": "answer",
* "op": "delete",
* "id": "12345",
* }
* ]
*
* @param OpInterface $op
* @return void
* @throws PermissionDeniedException
* @throws OpHandlerException
*/
public function handle(OpInterface $op)
{
$this->questionAggregateService->deleteAnswer(
$this->getSurveyIdFromContext($op),
$op->getEntityId()
);
}
/**
* Checks if patch is valid for this operation.
* @param OpInterface $op
* @return array
*/
public function validateOperation(OpInterface $op): array
{
$validationData = $this->validateEntityId($op, []);
return $this->getValidationReturn(
gt('Could not delete answer option'),
$validationData,
$op
);
}
}