1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/lav45/yii2-activity-logger |
4
|
|
|
* @copyright Copyright (c) 2017 LAV45 |
5
|
|
|
* @author Alexey Loban <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace lav45\activityLogger; |
10
|
|
|
|
11
|
|
|
use lav45\activityLogger\storage\MessageData; |
12
|
|
|
|
13
|
|
|
final class MessageBuilder implements MessageBuilderInterface |
14
|
|
|
{ |
15
|
|
|
private MessageData $message; |
16
|
|
|
|
17
|
33 |
|
public function __construct(string $entityName) |
18
|
|
|
{ |
19
|
33 |
|
$this->message = new MessageData(); |
20
|
33 |
|
$this->message->entityName = $entityName; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string|int $id |
25
|
|
|
*/ |
26
|
29 |
|
public function withEntityId($id): self |
27
|
|
|
{ |
28
|
29 |
|
$new = clone $this; |
29
|
29 |
|
$new->message->entityId = (string)$id; |
30
|
29 |
|
return $new; |
31
|
|
|
} |
32
|
|
|
|
33
|
28 |
|
public function withUserId(string $id): self |
34
|
|
|
{ |
35
|
28 |
|
$new = clone $this; |
36
|
28 |
|
$new->message->userId = $id; |
37
|
28 |
|
return $new; |
38
|
|
|
} |
39
|
|
|
|
40
|
28 |
|
public function withUserName(string $name): self |
41
|
|
|
{ |
42
|
28 |
|
$new = clone $this; |
43
|
28 |
|
$new->message->userName = $name; |
44
|
28 |
|
return $new; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string|null $action |
49
|
|
|
*/ |
50
|
28 |
|
public function withAction($action): self |
51
|
|
|
{ |
52
|
28 |
|
$new = clone $this; |
53
|
28 |
|
$new->message->action = $action; |
54
|
28 |
|
return $new; |
55
|
|
|
} |
56
|
|
|
|
57
|
28 |
|
public function withEnv(string $env): self |
58
|
|
|
{ |
59
|
28 |
|
$new = clone $this; |
60
|
28 |
|
$new->message->env = $env; |
61
|
28 |
|
return $new; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array|string $data |
66
|
|
|
*/ |
67
|
32 |
|
public function withData($data): self |
68
|
|
|
{ |
69
|
32 |
|
$new = clone $this; |
70
|
32 |
|
$new->message->data = $data; |
71
|
32 |
|
return $new; |
72
|
|
|
} |
73
|
|
|
|
74
|
33 |
|
public function build(int $now): MessageData |
75
|
|
|
{ |
76
|
33 |
|
$this->message->createdAt = $now; |
77
|
33 |
|
return $this->message; |
78
|
|
|
} |
79
|
|
|
|
80
|
33 |
|
public function __clone() |
81
|
|
|
{ |
82
|
33 |
|
$this->message = clone $this->message; |
83
|
|
|
} |
84
|
|
|
} |