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

TVolume   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 37
ccs 10
cts 10
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A openFile() 0 7 2
A saveFile() 0 6 2
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