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

Logpeek::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\logpeek\Config;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\Configuration;
9
use SimpleSAML\Utils;
10
use Symfony\Component\Yaml\Yaml;
11
use Symfony\Component\Yaml\Exception\ParseException;
12
13
/**
14
 * Configuration for the module logpeek.
15
 */
16
17
class Logpeek {
18
    /** @var string */
19
    public const DEFAULT_CONFIGFILE = 'module_logpeek.yml';
20
21
    /** @var int */
22
    public const MAX_BLOCKSIZE = 8192;
23
24
    /** @var int */
25
    public const DEFAULT_BLOCKSIZE = 8192;
26
27
    /** @var int */
28
    public const DEFAULT_LINES = 500;
29
30
    /** @var string */
31
    private string $logFile;
32
33
    /** @var int */
34
    private int $lines;
35
36
    /** @var int */
37
    private int $blockSize;
38
39
40
    /**
41
     * @param string|null $logFile
42
     * @param int $blockSize
43
     * @param int $lines
44
     */
45
    public function __construct(?string $logFile, ?int $blockSize = null, ?int $lines = null)
46
    {
47
        if ($logFile === null) {
48
            $config = Configuration::getInstance();
49
            $loggingDir = $config->getPathValue('loggingdir', 'log/');
50
            $logFile = $loggingDir . $config->getString('logging.logfile', 'simplesamlphp.log');
51
        }
52
53
        $this->setLogFile($logFile);
54
        $this->setBlockSize($blockSize ?? self::DEFAULT_BLOCKSIZE);
55
        $this->setLines($lines ?? self::DEFAULT_LINES);
56
    }
57
58
59
    /**
60
     * @return string
61
     */
62
    public function getLogFile(): string
63
    {
64
        return $this->logFile;
65
    }
66
67
68
    /**
69
     * @param string $logFile
70
     * @return void
71
     */
72
    protected function setLogFile(string $logFile): void
73
    {
74
        $this->logFile = $logFile;
75
    }
76
77
78
    /**
79
     * @return int
80
     */
81
    public function getLines(): int
82
    {
83
        return $this->lines;
84
    }
85
86
87
    /**
88
     * @param int $lines
89
     * @return void
90
     */
91
    protected function setLines(int $lines): void
92
    {
93
        Assert::positiveInteger($lines);
94
        $this->lines = $lines;
95
    }
96
97
98
    /**
99
     * @return int
100
     */
101
    public function getBlockSize(): int
102
    {
103
        return $this->blockSize;
104
    }
105
106
107
    /**
108
     * @param int $blockSize
109
     * @return void
110
     */
111
    protected function setBlockSize(int $blockSize): void
112
    {
113
        Assert::range($blockSize, 0, self::MAX_BLOCKSIZE);
114
        $this->blockSize = $blockSize;
115
    }
116
117
118
    /**
119
     * @param string $file
120
     * @return self
121
     */
122
    public static function fromPhp(string $configFile = 'module_logpeek.php'): self
123
    {
124
        $configUtils = new Utils\Config();
125
        $configDir = $configUtils->getConfigDir();
126
        include($configDir . '/' . $configFile);
127
128
        return static::fromArray($config ?? []);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config does not exist. Did you maybe mean $configDir?
Loading history...
129
    }
130
131
132
    /**
133
     * @param string $file
134
     * @return self
135
     */
136
    public static function fromYaml(string $configFile = self::DEFAULT_CONFIGFILE): self
137
    {
138
        $configUtils = new Utils\Config();
139
        $configDir = $configUtils->getConfigDir();
140
        $yamlConfig = Yaml::parse(file_get_contents($configDir . '/' . $configFile));
141
142
        return static::fromArray($yamlConfig ?? []);
143
    }
144
145
146
    /**
147
     * @param array $config
148
     * @return self
149
     */
150
    public static function fromArray(array $config): self
151
    {
152
        return new self($config['logFile'], $config['blockSize'], $config['lines']);
153
    }
154
};
155