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

AbstractTarget   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A str_n_pos() 0 9 2
A setTemplate() 0 4 1
A setSplit() 0 4 1
A __construct() 0 3 1
A setCustomerFieldType() 0 10 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Seasx\SeasLogger\Targets;
5
6
use Seasx\SeasLogger\AbstractConfig;
7
use Seasx\SeasLogger\Exceptions\NotSupportedException;
8
9
/**
10
 * Class AbstractTarget
11
 * @package Seasx\SeasLogger\Targets
12
 */
13
abstract class AbstractTarget
14
{
15
    /** @var string */
16
    protected $split = ' | ';
17
    /** @var array */
18
    protected $levelList = [];
19
    /** @var int */
20
    protected $levelIndex = 1;
21
    /** @var string */
22
    protected $customerType;
23
    /** @var array */
24
    protected $template = [];
25
26
    /**
27
     * AbstractTarget constructor.
28
     * @param array $levelList
29
     */
30
    public function __construct(array $levelList = [])
31
    {
32
        $this->levelList = $levelList;
33
    }
34
35
    /**
36
     * @param string $split
37
     * @return AbstractTarget
38
     */
39
    public function setSplit(string $split): self
40
    {
41
        $this->split = $split;
42
        return $this;
43
    }
44
45
    /**
46
     * @param array $template
47
     * @return AbstractTarget
48
     */
49
    public function setTemplate(array $template): self
50
    {
51
        $this->template = $template;
52
        return $this;
53
    }
54
55
    /**
56
     * @param string $type
57
     * @return AbstractTarget
58
     */
59
    public function setCustomerFieldType(string $type): self
60
    {
61
        if (!in_array($type, AbstractConfig::getSupportFieldType())) {
62
            throw new NotSupportedException("The field type not support $type");
63
        }
64
        if ($this->customerType === null) {
65
            $this->customerType = $type;
66
        }
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param array $messages
73
     */
74
    abstract public function export(array $messages): void;
75
76
    /**
77
     * @param string $str
78
     * @param string $find
79
     * @param int $n
80
     * @return int
81
     */
82
    protected function str_n_pos(string $str, string $find, int $n): int
83
    {
84
        $pos_val = 0;
85
        for ($i = 1; $i <= $n; $i++) {
86
            $pos = strpos($str, $find);
87
            $str = substr($str, $pos + 1);
88
            $pos_val = $pos + $pos_val + 1;
89
        }
90
        return $pos_val - 1;
91
    }
92
}