Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
PermissionProfile
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
8
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
 fromArray
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
7
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\Modules\Profiles\Domain\Model;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Permission Profile Aggregate / Entity.
13 *
14 * Represents a functional permission profile record from a_mod_profiles_records.
15 *
16 * @package App\Modules\Profiles\Domain\Model
17 */
18final readonly class PermissionProfile
19{
20    /**
21     * PermissionProfile constructor.
22     *
23     * @param int                  $id           Profile primary key ID.
24     * @param string               $name         Human-readable profile name.
25     * @param string|null          $code         Unique machine identifier code.
26     * @param string|null          $description  Optional textual description.
27     * @param bool                 $isActive     Whether profile is currently active.
28     * @param int                  $recordStatus Record lifecycle status.
29     * @param string               $createdAt    Creation timestamp (ISO-8601).
30     * @param string               $updatedAt    Last modification timestamp (ISO-8601).
31     * @param int                  $createdBy    Creator user ID.
32     * @param int                  $owner        Record owner user ID.
33     * @param array<int, int>|null $coOwners     Optional array of co-owner IDs.
34     */
35    public function __construct(
36        public int     $id,
37        public string  $name,
38        public ?string $code = null,
39        public ?string $description = null,
40        public bool    $isActive = true,
41        public int     $recordStatus = 1,
42        public string  $createdAt = '',
43        public string  $updatedAt = '',
44        public int     $createdBy = 1,
45        public int     $owner = 1,
46        public ?array  $coOwners = null,
47    ) {
48    }
49
50    /**
51     * Creates an instance from database row array.
52     *
53     * @param array<string, mixed> $row Raw database row.
54     * @return self Hydrated entity.
55     */
56    public static function fromArray(array $row): self
57    {
58        $coOwners = null;
59        if (!empty($row['co_owners'])) {
60            $decoded = json_decode((string) $row['co_owners'], true);
61            if (is_array($decoded)) {
62                $coOwners = array_map('intval', $decoded);
63            }
64        }
65
66        $isActive = isset($row['is_active'])
67            ? (bool) $row['is_active']
68            : (($row['status'] ?? 'active') === 'active' && ((int) ($row['special_access'] ?? 1) === 1));
69        $specialAccess = (int) ($row['special_access'] ?? $row['record_status'] ?? 1);
70
71        return new self(
72            id:           (int) ($row['id'] ?? 0),
73            name:         (string) ($row['name'] ?? ''),
74            code:         isset($row['code']) ? (string) $row['code'] : null,
75            description:  isset($row['description']) ? (string) $row['description'] : null,
76            isActive:     $isActive,
77            recordStatus: $specialAccess,
78            createdAt:    (string) ($row['created_at'] ?? ''),
79            updatedAt:    (string) ($row['updated_at'] ?? ''),
80            createdBy:    (int) ($row['created_by'] ?? 1),
81            owner:        (int) ($row['owner'] ?? 1),
82            coOwners:     $coOwners,
83        );
84    }
85}