forked from cleverage/processuibundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessExecution.php
132 lines (108 loc) · 3.31 KB
/
ProcessExecution.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/UiProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\UiProcessBundle\Entity;
use CleverAge\UiProcessBundle\Entity\Enum\ProcessExecutionStatus;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\String\UnicodeString;
#[ORM\Entity]
#[ORM\Index(name: 'idx_process_execution_code', columns: ['code'])]
#[ORM\Index(name: 'idx_process_execution_start_date', columns: ['start_date'])]
class ProcessExecution implements \Stringable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: Types::STRING, length: 255)]
public readonly string $code;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
public readonly \DateTimeImmutable $startDate;
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
public ?\DateTimeImmutable $endDate = null;
#[ORM\Column(type: Types::STRING, enumType: ProcessExecutionStatus::class)]
public ProcessExecutionStatus $status = ProcessExecutionStatus::Started;
/**
* @var array<string, mixed>
*/
#[ORM\Column(type: Types::JSON)]
private array $report = [];
/**
* @var array<string|int, mixed>
*/
#[ORM\Column(type: Types::JSON, nullable: true)]
private ?array $context = [];
public function getId(): ?int
{
return $this->id;
}
/**
* @param array<string|int, mixed> $context
*/
public function __construct(
string $code,
#[ORM\Column(type: Types::STRING, length: 255)] public readonly string $logFilename,
?array $context = [],
) {
$this->code = (string) (new UnicodeString($code))->truncate(255);
$this->startDate = \DateTimeImmutable::createFromMutable(new \DateTime());
$this->context = $context ?? [];
}
public function __toString(): string
{
return \sprintf('%s (%s)', $this->id, $this->code);
}
public function setStatus(ProcessExecutionStatus $status): void
{
$this->status = $status;
}
public function end(): void
{
$this->endDate = \DateTimeImmutable::createFromMutable(new \DateTime());
}
public function addReport(string $key, mixed $value): void
{
$this->report[$key] = $value;
}
public function getReport(?string $key = null, mixed $default = null): mixed
{
if (null === $key) {
return $this->report;
}
return $this->report[$key] ?? $default;
}
public function duration(string $format = '%H hour(s) %I min(s) %S s'): ?string
{
if (!$this->endDate instanceof \DateTimeImmutable) {
return null;
}
$diff = $this->endDate->diff($this->startDate);
return $diff->format($format);
}
public function getCode(): string
{
return $this->code;
}
/**
* @return array<string|int, mixed>
*/
public function getContext(): ?array
{
return $this->context;
}
/**
* @param array<string|int, mixed> $context
*/
public function setContext(array $context): void
{
$this->context = $context;
}
}