-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathAddLabelAction.php
More file actions
66 lines (55 loc) · 1.96 KB
/
Copy pathAddLabelAction.php
File metadata and controls
66 lines (55 loc) · 1.96 KB
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
<?php
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Deck\Automation\Actions;
use OCA\Deck\Automation\ActionInterface;
use OCA\Deck\Automation\AutomationEvent;
use OCA\Deck\Db\Card;
use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\LabelMapper;
use Psr\Log\LoggerInterface;
class AddLabelAction implements ActionInterface {
public function __construct(
private CardMapper $cardMapper,
private LabelMapper $labelMapper,
private LoggerInterface $logger,
) {
}
public function execute(Card $card, AutomationEvent $event, array $config = []): void {
$labelIds = $config['labelIds'] ?? [];
if (empty($labelIds) || !is_array($labelIds)) {
$this->logger->warning('AddLabelAction: Missing or invalid labelIds in configuration');
return;
}
try {
// Get currently assigned labels
$assignedLabels = $this->labelMapper->findAssignedLabelsForCard($card->getId());
$assignedLabelIds = array_map(fn($label) => $label->getId(), $assignedLabels);
foreach ($labelIds as $labelId) {
// Only add if not already assigned
if (!in_array((int)$labelId, $assignedLabelIds, true)) {
$this->cardMapper->assignLabel($card->getId(), (int)$labelId);
}
}
} catch (\Exception $e) {
$this->logger->error('AddLabelAction failed: ' . $e->getMessage(), ['exception' => $e]);
throw $e;
}
}
public function validateConfig(array $config): bool {
return isset($config['labelIds'])
&& is_array($config['labelIds'])
&& !empty($config['labelIds'])
&& array_reduce($config['labelIds'], fn($valid, $id) => $valid && is_numeric($id), true);
}
public function isApplicableForEvent(AutomationEvent $event): bool {
return $event->getEventName() !== AutomationEvent::EVENT_DELETE;
}
public function getDescription(array $config): string {
$labelIds = $config['labelIds'] ?? [];
$count = count($labelIds);
return $count > 0 ? "Add {$count} label(s)" : "Add labels";
}
}