Test Failed
Pull Request — master (#12)
by wujunze
03:09
created

SeasStashTarget::export()   B

Complexity

Conditions 10
Paths 20

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 22
c 1
b 0
f 1
nc 20
nop 1
dl 0
loc 30
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}