Passed
Pull Request — master (#3)
by Tim
02:47
created

Logpeek::fromYaml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\logpeek\Config;
6
7
use E_USER_NOTICE;
0 ignored issues
show
Bug introduced by
The type E_USER_NOTICE was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Utils;
11
use Symfony\Component\Yaml\Yaml;
12
use Symfony\Component\Yaml\Exception\ParseException;
13
14
/**
15
 * Configuration for the module logpeek.
16
 */
17
18
class Logpeek {
19
    /** @var string */
20
    public const DEFAULT_CONFIGFILE = 'module_logpeek.yml';
21
22
    /** @var int */
23
    public const MAX_BLOCKSIZE = 8192;
24
25
    /** @var int */
26
    public const DEFAULT_BLOCKSIZE = 8192;
27
28
    /** @var int */
29
    public const DEFAULT_LINES = 500;
30
31
    /** @var string */
32
    private string $logFile;
33
34
    /** @var int */
35
    private int $lines;
36
37
    /** @var int */
38
    private int $blockSize;
39
40
41
    /**
42
     * @param string|null $logFile
43
     * @param int $blockSize
44
     * @param int $lines
45
     */
46
    public function __construct(?string $logFile = null, int $blockSize = self::DEFAULT_BLOCKSIZE, int $lines = self::DEFAULT_LINES)
47
    {
48
        if ($logFile === null) {
49
            $config = Configuration::getInstance();
50
            $loggingDir = $config->getPathValue('loggingdir', 'log/');
51
            $logFile = $loggingDir . $config->getString('logging.logfile', 'simplesamlphp.log');
52
        }
53
54
        $this->setLogFile($logFile);
55
        $this->setBlockSize($blockSize);
56
        $this->setLines($lines);
57
    }
58
59
60
    /**
61
     * @return string
62
     */
63
    public function getLogFile(): string
64
    {
65
        return $this->logFile;
66
    }
67
68
69
    /**
70
     * @param string $logFile
71
     * @return void
72
     */
73
    protected function setLogFile(string $logFile): void
74
    {
75
        $this->logFile = $logFile;
76
    }
77
78
79
    /**
80
     * @return int
81
     */
82
    public function getLines(): int
83
    {
84
        return $this->lines;
85
    }
86
87
88
    /**
89
     * @param int $lines
90
     * @return void
91
     */
92
    protected function setLines(int $lines): void
93
    {
94
        Assert::positiveInteger($lines);
95
        $this->lines = $lines;
96
    }
97
98
99
    /**
100
     * @return int
101
     */
102
    public function getBlockSize(): int
103
    {
104
        return $this->blockSize;
105
    }
106
107
108
    /**
109
     * @param int $blockSize
110
     * @return void
111
     */
112
    protected function setBlockSize(int $blockSize): void
113
    {
114
        Assert::range($blockSize, 0, self::MAX_BLOCKSIZE);
115
        $this->blockSize = $blockSize;
116
    }
117
118
119
    /**
120
     * @param string $file
121
     * @return self
122
     */
123
    public static function fromYaml(string $configFile = 'module_logpeek.yml'): self
124
    {
125
        $configUtils = new Utils\Config();
126
        $configDir = $configUtils->getConfigDir();
127
        $yamlConfig = Yaml::parse(file_get_contents($configDir . '/' . $configFile)) ?? [];
128
129
        return static::fromArray($yamlConfig);
130
    }
131
132
133
    /**
134
     * @param array $config
135
     * @return self
136
     */
137
    public static function fromArray(array $config): self
138
    {
139
        return new self($config['logFile'], $config['blockSize'], $config['lines']);
140
    }
141
};
142