Passed
Push — master ( d309df...7ec489 )
by Petr
02:37
created

TVolume::openFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace kalanis\kw_auth\Sources\Files\Volume;
4
5
6
use kalanis\kw_auth\AuthException;
7
use kalanis\kw_auth\Interfaces\IFile;
8
use kalanis\kw_auth\TTranslate;
9
10
11
/**
12
 * Trait TFiles
13
 * @package kalanis\kw_auth\Sources\Files\Volume
14
 * Processing files with accounts
15
 */
16
trait TVolume
17
{
18
    use TTranslate;
19
20
    /**
21
     * @param string $path
22
     * @throws AuthException
23
     * @return array<int, array<int, string>>
24
     */
25 17
    protected function openFile(string $path): array
26
    {
27 17
        $content = @file($path);
28 17
        if (false === $content) {
29 1
            throw new AuthException($this->getLang()->kauPassFileNotFound($path));
30
        }
31 16
        return array_map([$this, 'explosion'], array_filter(array_map('trim', $content), [$this, 'filterEmptyLines']));
32
    }
33
34
    abstract public function explosion(string $input): array;
35
36
    abstract public function filterEmptyLines(string $input): bool;
37
38
    /**
39
     * @param string $path
40
     * @param array<int, array<int, string|int>> $lines
41
     * @throws AuthException
42
     */
43 5
    protected function saveFile(string $path, array $lines): void
44
    {
45 5
        $content = implode(IFile::CRLF, array_map([$this, 'implosion'], $lines)) . IFile::CRLF;
46 5
        $result = @file_put_contents($path, $content);
47 5
        if (false === $result) {
48 1
            throw new AuthException($this->getLang()->kauPassFileNotSave($path));
49
        }
50
    }
51
52
    abstract public function implosion(array $input): string;
53
}
54