Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.83% covered (success)
95.83%
23 / 24
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
StructureReassignRequiredException
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 forStructure
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
4
 forUser
100.00% covered (success)
100.00%
8 / 8
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\Modules\Structure\Domain\Exception;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use DomainException;
12
13/**
14 * Exception thrown when attempting to delete a structure node or user without required reassignment.
15 *
16 * Enforces Rule 2: Deleting organizational structure nodes or users requires an explicit
17 * target reassignment entity for all assigned records and child entities.
18 *
19 * @package App\Modules\Structure\Domain\Exception
20 */
21final class StructureReassignRequiredException extends DomainException
22{
23    /**
24     * Factory method for structure node deletion with existing dependencies.
25     *
26     * @param int $structureId Structure node ID.
27     * @param int $recordCount Number of owned records.
28     * @param int $userCount Number of assigned users.
29     * @param int $childNodeCount Number of child structure nodes.
30     * @return self
31     */
32    public static function forStructure(
33        int $structureId,
34        int $recordCount,
35        int $userCount,
36        int $childNodeCount
37    ): self {
38        $reasons = [];
39        if ($recordCount > 0) {
40            $reasons[] = sprintf('%d assigned record(s)', $recordCount);
41        }
42        if ($userCount > 0) {
43            $reasons[] = sprintf('%d assigned user(s)', $userCount);
44        }
45        if ($childNodeCount > 0) {
46            $reasons[] = sprintf('%d subordinate structure node(s)', $childNodeCount);
47        }
48
49        return new self(
50            sprintf(
51                'Cannot delete structure node (ID: %d) because it has active dependencies (%s). '
52                . 'You must reassign all dependencies to another target before deletion.',
53                $structureId,
54                implode(', ', $reasons)
55            )
56        );
57    }
58
59    /**
60     * Factory method for user deletion with existing ownership or memberships.
61     *
62     * @param int $userId Target user ID.
63     * @param int $recordCount Number of owned records.
64     * @return self
65     */
66    public static function forUser(int $userId, int $recordCount): self
67    {
68        return new self(
69            sprintf(
70                'Cannot delete user (ID: %d) because user owns %d record(s). '
71                . 'You must reassign record ownership before deletion.',
72                $userId,
73                $recordCount
74            )
75        );
76    }
77}