Passed
Push — master ( cb2d88...e8a992 )
by Petr
08:25
created

CanDir   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 61
dl 0
loc 113
ccs 60
cts 62
cp 0.9677
rs 10
c 1
b 0
f 0
wmc 23

7 Methods

Rating   Name   Duplication   Size   Complexity  
A moveDir() 0 8 2
A __construct() 0 4 1
A noDirectoryDelimiterSet() 0 3 1
C readDir() 0 46 12
A deleteDir() 0 11 3
A copyDir() 0 8 2
A createDir() 0 7 2
1
<?php
2
3
namespace kalanis\kw_files\Processing\Storage\Dirs;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_files\Interfaces\IFLTranslations;
8
use kalanis\kw_files\Interfaces\ITypes;
9
use kalanis\kw_files\Node;
10
use kalanis\kw_storage\Interfaces\IPassDirs;
11
use kalanis\kw_storage\StorageException;
12
13
14
/**
15
 * Class CanDir
16
 * @package kalanis\kw_files\Processing\Storage\Dirs
17
 * Process dirs via predefined api
18
 */
19
class CanDir extends ADirs
20
{
21
    /** @var IPassDirs */
22
    protected $storage = null;
23
24 11
    public function __construct(IPassDirs $storage, ?IFLTranslations $lang = null)
25
    {
26 11
        $this->storage = $storage;
27 11
        $this->setLang($lang);
28 11
    }
29
30 2
    public function createDir(array $entry, bool $deep = false): bool
31
    {
32 2
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
33
        try {
34 2
            return $this->storage->mkDir($path, $deep);
35 1
        } catch (StorageException $ex) {
36 1
            throw new FilesException($this->getLang()->flCannotCreateDir($path), $ex->getCode(), $ex);
37
        }
38
    }
39
40 5
    public function readDir(array $entry, bool $loadRecursive = false, bool $wantSize = false, bool $wantFirst = true): array
41
    {
42 5
        $entryPath = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
43
        try {
44 5
            if (!$this->storage->isDir($entryPath)) {
45 1
                throw new FilesException($this->getLang()->flCannotReadDir($entryPath));
46
            }
47 3
            $files = [];
48 3
            foreach ($this->storage->lookup($entryPath) as $item) {
49 3
                if ('..' == $item) {
50 3
                    continue;
51
                }
52 3
                $currentPath = $this->compactName(array_merge($entry, [$item]), $this->getStorageSeparator());
53 3
                $sub = new Node();
54 3
                if ('.' == $item) {
55 3
                    if (!$wantFirst) {
56 1
                        continue;
57
                    }
58
59 3
                    $sub->setData(
60 3
                        $entry,
61 3
                        0,
62 3
                        ITypes::TYPE_DIR
63
                    );
64 3
                } elseif ($this->storage->isDir($this->getStorageSeparator() . $currentPath)) {
65 2
                    $sub->setData(
66 2
                        $this->expandName($this->getStorageSeparator() . $currentPath),
67 2
                        0,
68 2
                        ITypes::TYPE_DIR
69
                    );
70
                } else {
71
                    // normal node - file
72 3
                    $sub->setData(
73 3
                        $this->expandName($this->getStorageSeparator() . $currentPath),
74 3
                        $wantSize ? intval($this->storage->size($currentPath)) : 0,
75 3
                        ITypes::TYPE_FILE
76
                    );
77
                }
78 3
                $files[] = $sub;
79 3
                if ($loadRecursive && $sub->isDir() && ('.' !== $item)) {
80 1
                    $files = array_merge($files, $this->readDir(array_merge($entry, [$item]), $loadRecursive, $wantSize, false));
81
                }
82
            }
83 3
            return $files;
84 2
        } catch (StorageException $ex) {
85 1
            throw new FilesException($this->getLang()->flCannotReadDir($entryPath), $ex->getCode(), $ex);
86
        }
87
    }
88
89 2
    public function copyDir(array $source, array $dest): bool
90
    {
91 2
        $src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator());
92 2
        $dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator());
93
        try {
94 2
            return $this->storage->copy($src, $dst);
95 1
        } catch (StorageException $ex) {
96 1
            throw new FilesException($this->getLang()->flCannotCopyDir($src, $dst), $ex->getCode(), $ex);
97
        }
98
    }
99
100 2
    public function moveDir(array $source, array $dest): bool
101
    {
102 2
        $src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator());
103 2
        $dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator());
104
        try {
105 2
            return $this->storage->move($src, $dst);
106 1
        } catch (StorageException $ex) {
107 1
            throw new FilesException($this->getLang()->flCannotMoveDir($src, $dst), $ex->getCode(), $ex);
108
        }
109
    }
110
111 2
    public function deleteDir(array $entry, bool $deep = false): bool
112
    {
113 2
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
114
        try {
115 2
            if ($this->storage->isDir($path)) {
116 1
                return $this->storage->rmDir($path, $deep);
117
            } else {
118 1
                return false;
119
            }
120 1
        } catch (StorageException $ex) {
121 1
            throw new FilesException($this->getLang()->flCannotRemoveDir($path), $ex->getCode(), $ex);
122
        }
123
    }
124
125
    /**
126
     * @return string
127
     * @codeCoverageIgnore only when path fails
128
     */
129
    protected function noDirectoryDelimiterSet(): string
130
    {
131
        return $this->getLang()->flNoDirectoryDelimiterSet();
132
    }
133
}
134