Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ActionMetadata
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromRow
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
 toArray
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Action\Domain\Model;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Action Metadata Value Object.
13 *
14 * Encapsulates immutable metadata configuration for system and module-specific actions
15 * (e.g. bulk actions, row actions, toolbar buttons).
16 *
17 * @package App\Core\Action\Domain\Model
18 */
19final readonly class ActionMetadata
20{
21    /**
22     * ActionMetadata constructor.
23     *
24     * @param int         $id                   Action primary identifier.
25     * @param int|null    $moduleId             Module ID (null for global actions).
26     * @param string      $name                 Unique action machine name.
27     * @param string      $label                Human-readable localized label.
28     * @param string      $category             Action category (grid_bulk, grid_toolbar, record_row, record_detail).
29     * @param string      $targetType           Action target scope (single, bulk).
30     * @param string      $handlerKey           Handler identifier (e.g. bulk_edit, bulk_archive).
31     * @param string|null $iconClass            Bootstrap icon class.
32     * @param string      $buttonClass          CSS button class.
33     * @param bool        $confirmationRequired Whether user confirmation is needed.
34     * @param string|null $confirmationMessage Custom confirmation dialog text.
35     * @param int         $sortOrder            Display sorting order.
36     * @param bool        $isActive             Active status.
37     * @param bool        $isSystem             System immutable action flag.
38     */
39    public function __construct(
40        public int $id,
41        public ?int $moduleId,
42        public string $name,
43        public string $label,
44        public string $category = 'grid_bulk',
45        public string $targetType = 'bulk',
46        public string $handlerKey = '',
47        public ?string $iconClass = null,
48        public string $buttonClass = 'btn-outline-secondary',
49        public bool $confirmationRequired = false,
50        public ?string $confirmationMessage = null,
51        public int $sortOrder = 10,
52        public bool $isActive = true,
53        public bool $isSystem = true,
54    ) {
55    }
56
57    /**
58     * Creates an ActionMetadata instance from a database row.
59     *
60     * @param array<string, mixed> $row Database associative record row.
61     * @return self Reconstituted action metadata value object.
62     */
63    public static function fromRow(array $row): self
64    {
65        return new self(
66            id:                   (int) $row['id'],
67            moduleId:             isset($row['module_id']) ? (int) $row['module_id'] : null,
68            name:                 (string) $row['name'],
69            label:                (string) $row['label'],
70            category:             (string) ($row['category'] ?? 'grid_bulk'),
71            targetType:           (string) ($row['target_type'] ?? 'bulk'),
72            handlerKey:           (string) ($row['handler_key'] ?? (string) $row['name']),
73            iconClass:            isset($row['icon_class']) ? (string) $row['icon_class'] : null,
74            buttonClass:          (string) ($row['button_class'] ?? 'btn-outline-secondary'),
75            confirmationRequired: (bool) ($row['confirmation_required'] ?? false),
76            confirmationMessage:  isset($row['confirmation_message']) ? (string) $row['confirmation_message'] : null,
77            sortOrder:            (int) ($row['sort_order'] ?? 10),
78            isActive:             (bool) ($row['is_active'] ?? true),
79            isSystem:             (bool) ($row['is_system'] ?? true),
80        );
81    }
82
83    /**
84     * Serializes action metadata to an array representation for JSON API responses.
85     *
86     * @return array<string, mixed> Serialized action dictionary.
87     */
88    public function toArray(): array
89    {
90        return [
91            'id'                    => $this->id,
92            'module_id'             => $this->moduleId,
93            'name'                  => $this->name,
94            'label'                 => $this->label,
95            'category'              => $this->category,
96            'target_type'           => $this->targetType,
97            'handler_key'           => $this->handlerKey,
98            'icon_class'            => $this->iconClass,
99            'button_class'          => $this->buttonClass,
100            'confirmation_required' => $this->confirmationRequired,
101            'confirmation_message'  => $this->confirmationMessage,
102            'sort_order'            => $this->sortOrder,
103            'is_active'             => $this->isActive,
104            'is_system'             => $this->isSystem,
105        ];
106    }
107}