Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Workflow
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
12
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%
17 / 17
100.00% covered (success)
100.00%
1 / 1
11
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\Automation\Domain\Model;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11/**
12 * Workflow Domain Aggregate Root.
13 *
14 * Represents a business process graph (DAG) with triggers, conditions, and actions.
15 *
16 * @package App\Modules\Automation\Domain\Model
17 */
18final readonly class Workflow
19{
20    /**
21     * Workflow constructor.
22     *
23     * @param int|null    $id                 Primary key identifier.
24     * @param string      $name               Workflow title.
25     * @param string      $targetModule       Target module machine name.
26     * @param string      $triggerType        Primary trigger stage or event.
27     * @param string      $executionFrequency Frequency rule ('always', 'once', 'on_change').
28     * @param bool        $isActive           Whether workflow is currently enabled.
29     * @param string|null $description        Optional workflow explanation.
30     * @param string|null $graphData          Raw Drawflow graph JSON.
31     * @param string|null $compiledFlow       Optimized execution DAG JSON.
32     * @param int         $totalRuns          Total execution counter.
33     * @param string|null $lastRunAt          Last execution timestamp.
34     * @param int         $owner              Owner user ID.
35     * @param int         $createdBy          Creator user ID.
36     */
37    public function __construct(
38        public ?int    $id,
39        public string  $name,
40        public string  $targetModule,
41        public string  $triggerType = 'after_create',
42        public string  $executionFrequency = 'always',
43        public bool    $isActive = true,
44        public ?string $description = null,
45        public ?string $graphData = null,
46        public ?string $compiledFlow = null,
47        public int     $totalRuns = 0,
48        public ?string $lastRunAt = null,
49        public int     $owner = 1,
50        public int     $createdBy = 1
51    ) {
52    }
53
54    /**
55     * Creates an instance from a database associative array.
56     *
57     * @param array<string, mixed> $row Database record.
58     * @return self Hydrated Workflow entity.
59     */
60    public static function fromRow(array $row): self
61    {
62        return new self(
63            id: isset($row['id']) ? (int) $row['id'] : null,
64            name: (string) ($row['name'] ?? ''),
65            targetModule: (string) ($row['target_module'] ?? ''),
66            triggerType: (string) ($row['trigger_type'] ?? 'after_create'),
67            executionFrequency: (string) ($row['execution_frequency'] ?? 'always'),
68            isActive: isset($row['is_active'])
69                ? (bool) ($row['is_active'])
70                : (($row['status'] ?? 'active') === 'active' && ((int) ($row['special_access'] ?? 1) === 1)),
71            description: isset($row['description']) ? (string) $row['description'] : null,
72            graphData: isset($row['graph_data']) ? (string) $row['graph_data'] : null,
73            compiledFlow: isset($row['compiled_flow']) ? (string) $row['compiled_flow'] : null,
74            totalRuns: isset($row['total_runs']) ? (int) $row['total_runs'] : 0,
75            lastRunAt: isset($row['last_run_at']) ? (string) $row['last_run_at'] : null,
76            owner: isset($row['owner']) ? (int) $row['owner'] : 1,
77            createdBy: isset($row['created_by']) ? (int) $row['created_by'] : 1
78        );
79    }
80}