Passed
Push — master ( fda971...e37793 )
by Enjoys
02:02
created

Parse::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Config;
6
7
use Enjoys\Traits\Options;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\NullLogger;
10
11
/**
12
 * Description of Parse
13
 * @psalm-suppress PropertyNotSetInConstructor
14
 * @author Enjoys
15
 */
16
abstract class Parse implements ParseInterface
17
{
18
    use Options;
19
20
    private ?string $configSource = null;
21
    protected LoggerInterface $logger;
22
23 19
    public function __construct()
24
    {
25 19
        $this->logger = new NullLogger();
26 19
    }
27
28 11
    public function setLogger(LoggerInterface $logger)
29
    {
30 11
        $this->logger = $logger;
31 11
    }
32
33 18
    public function addConfigSource(string $source): void
34
    {
35 18
        $this->configSource = $source;
36 18
    }
37
38 19
    public function parse()
39
    {
40 19
        if (is_null($this->configSource)) {
41 1
            $this->logger->error('Добавьте данные для парсинга');
42 1
            return null;
43
        }
44
45 18
        if (!is_file($this->configSource)) {
46 13
            return $this->parseString($this->configSource);
47
        }
48
49 5
        return $this->parseFile($this->configSource);
50
    }
51
52
    /**
53
     * @param string $input
54
     * @return mixed
55
     */
56
    abstract protected function parseString(string $input);
57
58
    /**
59
     * @param string $filename
60
     * @return mixed
61
     */
62
    abstract protected function parseFile(string $filename);
63
}
64