LogIO   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 25
c 1
b 0
f 1
dl 0
loc 78
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 3 1
A send() 0 21 3
A __construct() 0 8 1
1
<?php
2
/**
3
 * Replicating to log.io
4
 * User: moyo
5
 * Date: 2018/5/2
6
 * Time: 11:59 AM
7
 */
8
9
namespace Carno\Log\Replicator;
10
11
use Carno\Log\Connections;
12
use Carno\Log\Contracts\Outputter;
13
use Carno\Log\Contracts\Replicated;
14
use Carno\Log\Environment;
15
use Carno\Log\Outputter\TCP;
16
use Carno\Net\Address;
17
18
class LogIO implements Replicated
19
{
20
    /**
21
     * @var Environment
22
     */
23
    private $env = null;
24
25
    /**
26
     * @var Connections
27
     */
28
    private $cmg = null;
29
30
    /**
31
     * @var Address
32
     */
33
    private $recv = null;
34
35
    /**
36
     * @var Outputter
37
     */
38
    private $pipe = null;
39
40
    /**
41
     * @var array
42
     */
43
    private $assets = [];
44
45
    /**
46
     * LogIO constructor.
47
     * @param Environment $env
48
     * @param Connections $cmg
49
     * @param Address $recv
50
     */
51
    public function __construct(Environment $env, Connections $cmg, Address $recv)
52
    {
53
        $this->env = $env;
54
        $this->cmg = $cmg;
55
        $this->recv = $recv;
56
57
        $this->pipe = $cmg->hosting($recv, static function (Address $recv) {
58
            return new TCP($recv);
59
        });
60
    }
61
62
    /**
63
     */
64
    public function __destruct()
65
    {
66
        $this->cmg->close($this->recv);
67
    }
68
69
    /**
70
     * @param string $scene
71
     * @param string $level
72
     * @param string $message
73
     * @param array $context
74
     */
75
    public function send(string $scene, string $level, string $message, array $context) : void
76
    {
77
        $buf = '';
78
79
        $node = $this->env->app() . (($t = $this->env->tagged()) ? sprintf(':%s', $t) : '');
80
        $stream = $scene;
81
82
        if (!isset($this->assets[$node][$stream])) {
83
            $this->assets[$node][$stream] = true;
84
            $buf .= sprintf("+node|%s|%s\r\n", $node, $stream);
85
        }
86
87
        $buf .= sprintf(
88
            "+log|%s|%s|%s|%s\r\n",
89
            $stream,
90
            $node,
91
            $level,
92
            sprintf('%s > %s', $message, json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE))
93
        );
94
95
        $this->pipe->write($buf);
96
    }
97
}
98