Passed
Push — master ( 4ac21f...90353c )
by Petr
02:27
created

TFiles   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 47
ccs 15
cts 15
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A openFile() 0 9 2
A saveFile() 0 10 3
1
<?php
2
3
namespace kalanis\kw_auth\Sources\Files\Files;
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_files\Access\CompositeAdapter;
10
use kalanis\kw_files\FilesException;
11
use kalanis\kw_paths\PathsException;
12
use kalanis\kw_paths\Stuff;
13
14
15
/**
16
 * Trait TFiles
17
 * @package kalanis\kw_auth\Sources\Files\Files
18
 * Processing files with accounts
19
 */
20
trait TFiles
21
{
22
    use TLang;
23
24
    /** @var CompositeAdapter */
25
    protected $files = null;
26
27
    /**
28
     * @param string $path
29
     * @throws AuthException
30
     * @return array<int, array<int, string>>
31
     */
32 42
    protected function openFile(string $path): array
33
    {
34
        try {
35 42
            $pt = Stuff::pathToArray($path);
36 42
            $content = $this->files->readFile($pt);
37 23
            $lines = explode(IFile::CRLF, strval($content));
38 23
            return array_map([$this, 'explosion'], array_filter(array_map('trim', $lines), [$this, 'filterEmptyLines']));
39 23
        } catch (FilesException | PathsException $ex) {
40 23
            throw new AuthException($ex->getMessage(), $ex->getCode(), $ex);
41
        }
42
    }
43
44
    abstract public function explosion(string $input): array;
45
46
    abstract public function filterEmptyLines(string $input): bool;
47
48
    /**
49
     * @param string $path
50
     * @param array<int, array<int, string|int>> $lines
51
     * @throws AuthException
52
     */
53 13
    protected function saveFile(string $path, array $lines): void
54
    {
55 13
        $content = implode(IFile::CRLF, array_map([$this, 'implosion'], $lines)) . IFile::CRLF;
56
        try {
57 13
            $pt = Stuff::pathToArray($path);
58 13
            if (false === $this->files->saveFile($pt, $content)) {
59 7
                throw new AuthException($this->getAuLang()->kauPassFileNotSave($path));
60
            }
61 6
        } catch (FilesException | PathsException $ex) {
62 6
            throw new AuthException($ex->getMessage(), $ex->getCode(), $ex);
63
        }
64 7
    }
65
66
    abstract public function implosion(array $input): string;
67
}
68