File::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 3
rs 10
1
<?php
2
3
namespace kalanis\kw_bans\Sources;
4
5
6
use kalanis\kw_bans\BanException;
7
use kalanis\kw_bans\Interfaces\IKBTranslations;
8
use kalanis\kw_bans\Translations;
9
10
11
/**
12
 * Class File
13
 * @package kalanis\kw_bans\Sources
14
 * Bans source is file
15
 */
16
class File extends ASources
17
{
18
    /**
19
     * @param string $file
20
     * @param IKBTranslations|null $lang
21
     * @throws BanException
22
     */
23 3
    public function __construct(string $file, ?IKBTranslations $lang = null)
24
    {
25 3
        $lang = $lang ?: new Translations();
26 3
        $rows = @file($file);
27 3
        if (false === $rows) {
28 1
            throw new BanException($lang->ikbDefinedFileNotFound($file));
29
        }
30
31
        // remove empty records
32 2
        $rows = array_filter($rows);
33
34
        // sort them, better for lookup
35 2
        sort($rows);
36
37
        // last clearing
38 2
        $this->knownRecords = array_map([$this, 'noCrLf'], $rows);
39 2
    }
40
41 2
    public function noCrLf(string $line): string
42
    {
43 2
        return strtr($line, ["\r" => '', "\n" => '']);
44
    }
45
}
46