Passed
Push — master ( 90353c...f03d23 )
by Petr
02:32
created

TVolume::saveFile()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 12
ccs 7
cts 8
cp 0.875
crap 3.0175
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\Traits\TLang;
9
use kalanis\kw_paths\PathsException;
10
use kalanis\kw_paths\Stuff;
11
12
13
/**
14
 * Trait TVolume
15
 * @package kalanis\kw_auth\Sources\Files\Volume
16
 * Processing files with accounts
17
 */
18
trait TVolume
19
{
20
    use TLang;
21
22
    /**
23
     * @param string[] $path
24
     * @throws AuthException
25
     * @return array<int, array<int, string>>
26
     */
27 17
    protected function openFile(array $path): array
28
    {
29
        try {
30 17
            $pt = Stuff::arrayToPath($path);
31 17
            $content = @file($pt);
32 17
            if (false === $content) {
33 2
                throw new AuthException($this->getAuLang()->kauPassFileNotFound($pt));
34
            }
35 15
            return array_map([$this, 'explosion'], array_filter(array_map('trim', $content), [$this, 'filterEmptyLines']));
36 2
        } catch (PathsException $ex) {
37
            // @codeCoverageIgnoreStart
38
            throw new AuthException($ex->getMessage(), $ex->getCode(), $ex);
39
        }
40
        // @codeCoverageIgnoreEnd
41
    }
42
43
    abstract public function explosion(string $input): array;
44
45
    abstract public function filterEmptyLines(string $input): bool;
46
47
    /**
48
     * @param string[] $path
49
     * @param array<int, array<int, string|int>> $lines
50
     * @throws AuthException
51
     */
52 5
    protected function saveFile(array $path, array $lines): void
53
    {
54
        try {
55 5
            $content = implode(IFile::CRLF, array_map([$this, 'implosion'], $lines)) . IFile::CRLF;
56 5
            $pt = Stuff::arrayToPath($path);
57 5
            $result = @file_put_contents($pt, $content);
58 5
            if (false === $result) {
59 5
                throw new AuthException($this->getAuLang()->kauPassFileNotSave($pt));
60
            }
61 1
        } catch (PathsException $ex) {
62
            // @codeCoverageIgnoreStart
63
            throw new AuthException($ex->getMessage(), $ex->getCode(), $ex);
64
        }
65
        // @codeCoverageIgnoreEnd
66 5
    }
67
68
    abstract public function implosion(array $input): string;
69
}
70