Issues (2551)

src/Impl/Cmd/AddIdentityLinkCmd.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Jabe\Impl\Cmd;
4
5
use Jabe\ProcessEngineException;
6
use Jabe\Impl\Interceptor\{
7
    CommandInterface,
8
    CommandContext
9
};
10
use Jabe\Impl\Persistence\Entity\{
11
    TaskEntity,
12
    TaskManager
13
};
14
use Jabe\Impl\Util\EnsureUtil;
15
use Jabe\Task\IdentityLinkType;
16
17
abstract class AddIdentityLinkCmd implements CommandInterface, \Serializable
18
{
19
    protected $userId;
20
21
    protected $groupId;
22
23
    protected $type;
24
25
    protected $taskId;
26
27
    protected $task;
28
29
    public function __construct(string $taskId, string $userId, string $groupId, string $type)
30
    {
31
        $this->validateParams($userId, $groupId, $type, $taskId);
32
        $this->taskId = $taskId;
33
        $this->userId = $userId;
34
        $this->groupId = $groupId;
35
        $this->type = $type;
36
    }
37
38
    public function serialize()
39
    {
40
        return json_encode([
41
            'taskId' => $this->taskId,
42
            'userId' => $this->userId,
43
            'groupId' => $this->groupId,
44
            'type' => $this->type
45
        ]);
46
    }
47
48
    public function unserialize($data)
49
    {
50
        $json = json_decode($data);
51
        $this->taskId = $json->taskId;
52
        $this->userId = $json->userId;
53
        $this->groupId = $json->groupId;
54
        $this->type = $json->type;
55
    }
56
57
    protected function validateParams(?string $userId, ?string $groupId, string $type, string $taskId): void
58
    {
59
        EnsureUtil::ensureNotNull("taskId", "taskId", $taskId);
60
        EnsureUtil::ensureNotNull("type is required when adding a new task identity link", "type", $type);
61
62
        // Special treatment for assignee, group cannot be used an userId may be null
63
        if (IdentityLinkType::ASSIGNEE == $type) {
64
            if ($groupId !== null) {
65
                throw new ProcessEngineException("Incompatible usage: cannot use ASSIGNEE"
66
                    . " together with a groupId");
67
            }
68
        } else {
69
            if ($userId === null && $groupId === null) {
70
                throw new ProcessEngineException("userId and groupId cannot both be null");
71
            }
72
        }
73
    }
74
75
    public function execute(CommandContext $commandContext)
76
    {
77
        EnsureUtil::ensureNotNull("taskId", $this->taskId);
78
79
        $taskManager = $commandContext->getTaskManager();
80
        $task = $taskManager->findTaskById($this->taskId);
81
        EnsureUtil::ensureNotNull("Cannot find task with id " . $this->taskId, "task", $task);
82
83
        $this->checkAddIdentityLink($task, $commandContext);
84
85
        if (IdentityLinkType::ASSIGNEE == $this->type) {
86
            $task->setAssignee($this->userId);
87
        } elseif (IdentityLinkType::OWNER == $this->type) {
88
            $task->setOwner($this->userId);
89
        } else {
90
            $task->addIdentityLink($this->userId, $this->groupId, $this->type);
91
        }
92
        $task->triggerUpdateEvent();
93
94
        return null;
95
    }
96
97
    protected function checkAddIdentityLink(TaskEntity $task, CommandContext $commandContext): void
98
    {
99
        foreach ($commandContext->getProcessEngineConfiguration()->getCommandCheckers() as $checker) {
0 ignored issues
show
The method getCommandCheckers() does not exist on Jabe\Impl\Cfg\ProcessEngineConfigurationImpl. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        foreach ($commandContext->getProcessEngineConfiguration()->/** @scrutinizer ignore-call */ getCommandCheckers() as $checker) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
            $checker->checkTaskAssign($task);
101
        }
102
    }
103
}
104