Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
PreferenceScope
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
4 / 4
4
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
 forUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 forModule
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 forEntity
100.00% covered (success)
100.00%
1 / 1
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\Preference\Domain\Model;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Value Object representing the scope hierarchy for user preferences.
13 *
14 * Encapsulates user ID, device fingerprint, module name, and optional entity coordinates.
15 *
16 * @package App\Core\Preference\Domain\Model
17 */
18final readonly class PreferenceScope
19{
20    /**
21     * PreferenceScope constructor.
22     *
23     * @param int         $userId            Unique user identifier.
24     * @param string|null $deviceFingerprint Device/browser fingerprint or null for cross-device.
25     * @param string|null $moduleName        Associated module name or null for global.
26     * @param string|null $entityType        Specific entity type or null.
27     * @param int|null    $entityId          Specific entity ID or null.
28     */
29    public function __construct(
30        public int $userId,
31        public ?string $deviceFingerprint = null,
32        public ?string $moduleName = null,
33        public ?string $entityType = null,
34        public ?int $entityId = null,
35    ) {
36    }
37
38    /**
39     * Creates a global scope for a specific user across all devices and modules.
40     *
41     * @param int $userId Target user ID.
42     * @return self New global user scope.
43     */
44    public static function forUser(int $userId): self
45    {
46        return new self($userId);
47    }
48
49    /**
50     * Creates a module-specific scope for a user.
51     *
52     * @param int         $userId            Target user ID.
53     * @param string      $moduleName        Target module name.
54     * @param string|null $deviceFingerprint Optional device fingerprint.
55     * @return self New module-scoped instance.
56     */
57    public static function forModule(int $userId, string $moduleName, ?string $deviceFingerprint = null): self
58    {
59        return new self($userId, $deviceFingerprint, $moduleName);
60    }
61
62    /**
63     * Creates an entity-specific scope for a user.
64     *
65     * @param int         $userId            Target user ID.
66     * @param string      $moduleName        Target module name.
67     * @param string      $entityType        Entity type name.
68     * @param int         $entityId          Entity ID.
69     * @param string|null $deviceFingerprint Optional device fingerprint.
70     * @return self New entity-scoped instance.
71     */
72    public static function forEntity(
73        int $userId,
74        string $moduleName,
75        string $entityType,
76        int $entityId,
77        ?string $deviceFingerprint = null,
78    ): self {
79        return new self($userId, $deviceFingerprint, $moduleName, $entityType, $entityId);
80    }
81}