Passed
Push — master ( 27c83c...3b806d )
by Loban
03:03
created

MessageBuilder   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 25
c 1
b 0
f 0
dl 0
loc 67
ccs 32
cts 32
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 3 1
A withUserName() 0 5 1
A withEnv() 0 5 1
A withEntityId() 0 5 1
A withData() 0 5 1
A __construct() 0 4 1
A withUserId() 0 5 1
A build() 0 4 1
A withAction() 0 5 1
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 32
    public function __construct(string $entityName)
18
    {
19 32
        $this->message = new MessageData();
20 32
        $this->message->entityName = $entityName;
21
    }
22
23
    /**
24
     * @param string|int $id
25
     */
26 28
    public function withEntityId($id): self
27
    {
28 28
        $new = clone $this;
29 28
        $new->message->entityId = (string)$id;
30 28
        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 27
    public function withAction(string $action): self
48
    {
49 27
        $new = clone $this;
50 27
        $new->message->action = $action;
51 27
        return $new;
52
    }
53
54 28
    public function withEnv(string $env): self
55
    {
56 28
        $new = clone $this;
57 28
        $new->message->env = $env;
58 28
        return $new;
59
    }
60
61
    /**
62
     * @param array|string $data
63
     */
64 31
    public function withData($data): self
65
    {
66 31
        $new = clone $this;
67 31
        $new->message->data = $data;
68 31
        return $new;
69
    }
70
71 32
    public function build(int $now): MessageData
72
    {
73 32
        $this->message->createdAt = $now;
74 32
        return $this->message;
75
    }
76
77 32
    public function __clone()
78
    {
79 32
        $this->message = clone $this->message;
80
    }
81
}