Test Failed
Pull Request — master (#12)
by
unknown
02:46
created

SeasStashTarget   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 26
c 1
b 0
f 1
dl 0
loc 50
rs 10
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
B export() 0 30 10
A __construct() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Seasx\SeasLogger\Targets;
5
6
use Exception;
7
use Seasx\SeasLogger\ArrayHelper;
8
use Seasx\SeasLogger\Kafka\Socket\Pool;
9
10
/**
11
 * Class SeasStashTarget
12
 * @package Seasx\SeasLogger\Targets
13
 */
14
class SeasStashTarget extends AbstractTarget
15
{
16
    /** @var Pool */
17
    private $clientPool;
18
19
    /**
20
     * SeasStashTarget constructor.
21
     * @param Pool $clientPool
22
     * @param array $levelList
23
     */
24
    public function __construct(Pool $clientPool, array $levelList = [])
25
    {
26
        $this->clientPool = $clientPool;
27
        $this->levelList = $levelList;
28
    }
29
30
    /**
31
     * @param array $messages
32
     * @throws Exception
33
     */
34
    public function export(array $messages): void
35
    {
36
        $connection = $this->clientPool->getConnection();
37
        foreach ($messages as $module => $message) {
38
            foreach ($message as $msg) {
39
                if (is_string($msg)) {
40
                    switch (ini_get('seaslog.appender')) {
41
                        case '2':
42
                        case '3':
43
                            $msg = trim(substr($msg, $this->str_n_pos($msg, ' ', 6)));
44
                            break;
45
                        case '1':
46
                        default:
47
                            $fileName = basename($module);
48
                            $module = substr($fileName, 0, strpos($fileName, '_', -1));
49
                    }
50
                    $msg = explode($this->split, trim($msg));
51
                    if (($diff = count($msg) - count($this->template)) > 0) {
52
                        array_splice($msg, -($diff + 1), $diff, [array_slice($msg, -($diff + 1), $diff)]);
53
                    }
54
                }
55
                if (!empty($this->levelList) && !in_array(strtolower($msg[$this->levelIndex]), $this->levelList)) {
56
                    continue;
57
                }
58
                ArrayHelper::remove($msg, '%c');
59
                $msg = $module . '@' . str_replace(PHP_EOL, '', implode($this->split, $msg)) . PHP_EOL;
60
                $connection->send($msg);
61
            }
62
        }
63
        $this->clientPool->release($connection);
64
    }
65
}