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

LogCollection::flushMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * @link https://github.com/lav45/yii2-activity-logger
4
 * @copyright Copyright (c) 2017 LAV45
5
 * @author Aleksey Loban <[email protected]>
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace lav45\activityLogger;
10
11
class LogCollection
12
{
13
    private ManagerInterface $logger;
14
15
    private MessageBuilderInterface $builder;
16
    /** @var string[] */
17
    private array $data = [];
18
19 3
    public function __construct(ManagerInterface $logger, string $entityName)
20
    {
21 3
        $this->logger = $logger;
22 3
        $this->builder = $logger->createMessageBuilder($entityName);
23
    }
24
25
    /**
26
     * @param string|int $value
27
     */
28 1
    public function setEntityId($value): self
29
    {
30 1
        $this->builder = $this->builder->withEntityId($value);
31 1
        return $this;
32
    }
33
34 1
    public function setAction(string $value): self
35
    {
36 1
        $this->builder = $this->builder->withAction($value);
37 1
        return $this;
38
    }
39
40 3
    public function addMessage(string $value): void
41
    {
42 3
        $this->data[] = $value;
43
    }
44
45
    /**
46
     * @return string[]
47
     */
48 3
    private function flushData(): array
49
    {
50 3
        $data = $this->data;
51 3
        $this->data = [];
52 3
        return $data;
53
    }
54
55 3
    public function push(): bool
56
    {
57 3
        $data = $this->flushData();
58 3
        if (empty($data)) {
59 1
            return false;
60
        }
61
62 3
        $message = $this->builder
63 3
            ->withData($data)
64 3
            ->build(time());
65
66 3
        return $this->logger->log($message);
67
    }
68
}