MessageBuilder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
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 70
ccs 32
cts 32
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A withUserName() 0 5 1
A withEntityId() 0 5 1
A __construct() 0 4 1
A withUserId() 0 5 1
A __clone() 0 3 1
A withEnv() 0 5 1
A withData() 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 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
}