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

TStorage   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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