ProcessFile   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 82
dl 0
loc 147
rs 10
c 1
b 0
f 0
ccs 80
cts 80
cp 1
wmc 28

7 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteFile() 0 10 3
A readFile() 0 19 5
A moveFile() 0 30 5
A __construct() 0 5 1
A copyFile() 0 14 3
A getLookupRecord() 0 3 1
B saveFile() 0 42 10
1
<?php
2
3
namespace kalanis\kw_files_mapper\Processing\Mapper;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_files\Interfaces\IFLTranslations;
8
use kalanis\kw_files\Interfaces\IProcessFiles;
9
use kalanis\kw_files\Traits\TLang;
10
use kalanis\kw_files_mapper\Support\Process;
11
use kalanis\kw_mapper\MapperException;
12
use kalanis\kw_mapper\Records\ARecord;
13
use kalanis\kw_paths\ArrayPath;
14
use kalanis\kw_paths\Stuff;
15
16
17
/**
18
 * Class ProcessFile
19
 * @package kalanis\kw_files_mapper\Processing\Mapper
20
 * Process files in many ways
21
 */
22
class ProcessFile implements IProcessFiles
23
{
24
    use TEntryLookup;
25
    use TLang;
26
27
    protected ARecord $record;
28
29 22
    public function __construct(ARecord $record, ?Process\Translate $translate = null, ?IFLTranslations $lang = null)
30
    {
31 22
        $this->setFlLang($lang);
32 22
        $this->record = $record;
33 22
        $this->setTranslation($translate);
34 22
    }
35
36 7
    public function readFile(array $entry, ?int $offset = null, ?int $length = null): string
37
    {
38
        try {
39 7
            $record = $this->getEntry($entry);
40 6
            if (is_null($record)) {
41 2
                throw new FilesException($this->getFlLang()->flCannotLoadFile(Stuff::arrayToPath($entry)));
42
            }
43
44 4
            $content = $record->__get($this->getTranslation()->getContentKey());
45
            // shit with substr... that needed undefined params was from some java dude?!
46 4
            if (!is_null($length)) {
47 1
                return strval(substr(strval($content), intval($offset), $length));
48
            }
49 4
            if (!is_null($offset)) {
50 1
                return strval(substr(strval($content), $offset));
51
            }
52 4
            return strval($content);
53 3
        } catch (MapperException $ex) {
54 1
            throw new FilesException($ex->getMessage(), $ex->getCode(), $ex);
55
        }
56
    }
57
58 8
    public function saveFile(array $entry, string $content, ?int $offset = null, int $mode = 0): bool
59
    {
60 8
        $path = Stuff::arrayToPath($entry);
61
        try {
62
63 8
            if (1 > count($entry)) {
64 1
                throw new FilesException($this->getFlLang()->flCannotSaveFile($path));
65
            }
66
67 7
            $tgtArr = new ArrayPath();
68 7
            $tgtArr->setArray($entry);
69
70 7
            $current = $this->getEntry($entry);
71 6
            $parent = $this->getEntry($tgtArr->getArrayDirectory());
72
73 6
            if (!empty($tgtArr->getArrayDirectory()) && empty($parent)) {
74 1
                throw new FilesException($this->getFlLang()->flCannotSaveFile($path));
75
            }
76
77 5
            $prepend = '';
78 5
            if (is_null($current)) {
79 5
                $current = $this->getLookupRecord();
80 5
                $current->__set($this->getTranslation()->getParentKey(), $parent ? strval($parent->__get($this->getTranslation()->getPrimaryKey())) : null);
81 5
                $current->__set($this->getTranslation()->getCurrentKey(), $tgtArr->getFileName());
82
            } else {
83 1
                if (FILE_APPEND == $mode) {
84 1
                    $prepend = strval($current->__get($this->getTranslation()->getContentKey()));
85
                }
86
            }
87
88 5
            if (!is_null($offset)) {
89 1
                $prepend = str_pad(strval(substr($prepend, 0, $offset)), $offset, chr(0));
90
            }
91
92 5
            $current->__set($this->getTranslation()->getContentKey(), $prepend . $content);
93
94 5
            if (false === $current->save()) {
95 1
                throw new FilesException($this->getFlLang()->flCannotSaveFile($path));
96
            }
97 4
            return true;
98 4
        } catch (MapperException $ex) {
99 1
            throw new FilesException($this->getFlLang()->flCannotSaveFile($path), $ex->getCode(), $ex);
100
        }
101
    }
102
103 3
    public function copyFile(array $source, array $dest): bool
104
    {
105
        // simplified run - no moving nodes, just use existing ones
106 3
        $src = Stuff::arrayToPath($source);
107 3
        $dst = Stuff::arrayToPath($dest);
108
        try {
109 3
            $dstRec = $this->getEntry($dest);
110 2
            if ($dstRec) {
111 1
                return false;
112
            }
113
114 1
            return $this->saveFile($dest, $this->readFile($source));
115 1
        } catch (MapperException $ex) {
116 1
            throw new FilesException($this->getFlLang()->flCannotCopyFile($src, $dst), $ex->getCode(), $ex);
117
        }
118
    }
119
120 5
    public function moveFile(array $source, array $dest): bool
121
    {
122
        try {
123 5
            $ptDst = new ArrayPath();
124 5
            $ptDst->setArray($dest);
125
126 5
            $src = $this->getEntry($source);
127 4
            if (!$src) {
128 1
                throw new FilesException($this->getFlLang()->flCannotProcessNode(Stuff::arrayToPath($source)));
129
            }
130
131 3
            $dst = $this->getEntry($ptDst->getArrayDirectory());
132 3
            if (!$dst) {
133 1
                return false;
134
            }
135
136 2
            $tgt = $this->getEntry([$ptDst->getFileName()], $dst);
137 2
            if ($tgt) {
0 ignored issues
show
introduced by
$tgt is of type kalanis\kw_mapper\Records\ARecord, thus it always evaluated to true.
Loading history...
138 1
                return false;
139
            }
140
141 1
            $src->__set($this->getTranslation()->getCurrentKey(), $ptDst->getFileName());
142 1
            $src->__set($this->getTranslation()->getParentKey(), $dst->__get($this->getTranslation()->getPrimaryKey()));
143 1
            return $src->save();
144
145 2
        } catch (MapperException $ex) {
146 1
            throw new FilesException($this->getFlLang()->flCannotMoveFile(
147 1
                Stuff::arrayToPath($source),
148 1
                Stuff::arrayToPath($dest)
149 1
            ), $ex->getCode(), $ex);
150
        }
151
    }
152
153 3
    public function deleteFile(array $entry): bool
154
    {
155
        try {
156 3
            $record = $this->getEntry($entry);
157 2
            if (is_null($record)) {
158 2
                return true;
159
            }
160 2
            return $record->delete();
161 1
        } catch (MapperException $ex) {
162 1
            throw new FilesException($this->getFlLang()->flCannotRemoveFile(Stuff::arrayToPath($entry)), $ex->getCode(), $ex);
163
        }
164
    }
165
166 14
    protected function getLookupRecord(): ARecord
167
    {
168 14
        return clone $this->record;
169
    }
170
}
171