Passed
Push — master ( d467ff...321125 )
by Petr
08:04
created

Basic   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 90
ccs 40
cts 42
cp 0.9524
rs 10
wmc 18

8 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 7 2
A __construct() 0 4 1
A isDir() 0 7 3
A isFile() 0 7 3
A isReadable() 0 3 1
A size() 0 29 6
A isWritable() 0 3 1
A created() 0 3 1
1
<?php
2
3
namespace kalanis\kw_files\Processing\Storage\Nodes;
4
5
6
use kalanis\kw_files\FilesException;
7
use kalanis\kw_files\Interfaces\IFLTranslations;
8
use kalanis\kw_files\Translations;
9
use kalanis\kw_storage\Interfaces\IStorage;
10
use kalanis\kw_storage\StorageException;
11
12
13
/**
14
 * Class Basic
15
 * @package kalanis\kw_files\Processing\Storage\Nodes
16
 * Process dirs via lookup
17
 */
18
class Basic extends ANodes
19
{
20
    const STORAGE_NODE_KEY = "\eNODE\e";
21
22
    /** @var IFLTranslations */
23
    protected $lang = null;
24
    /** @var IStorage */
25
    protected $storage = null;
26
27 10
    public function __construct(IStorage $storage, ?IFLTranslations $lang = null)
28
    {
29 10
        $this->storage = $storage;
30 10
        $this->lang = $lang ?? new Translations();
31 10
    }
32
33 7
    public function exists(array $entry): bool
34
    {
35 7
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
36
        try {
37 7
            return $this->storage->exists($path);
38 1
        } catch (StorageException $ex) {
39 1
            throw new FilesException($this->lang->flCannotProcessNode($path), $ex->getCode(), $ex);
40
        }
41
    }
42
43 1
    public function isReadable(array $entry): bool
44
    {
45 1
        return true;
46
    }
47
48 1
    public function isWritable(array $entry): bool
49
    {
50 1
        return true;
51
    }
52
53 7
    public function isDir(array $entry): bool
54
    {
55 7
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
56
        try {
57 7
            return $this->storage->exists($path) && static::STORAGE_NODE_KEY === $this->storage->read($path);
58 1
        } catch (StorageException $ex) {
59 1
            throw new FilesException($this->lang->flCannotProcessNode($path), $ex->getCode(), $ex);
60
        }
61
    }
62
63 3
    public function isFile(array $entry): bool
64
    {
65 3
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
66
        try {
67 3
            return $this->storage->exists($path) && static::STORAGE_NODE_KEY !== $this->storage->read($path);
68 1
        } catch (StorageException $ex) {
69 1
            throw new FilesException($this->lang->flCannotProcessNode($path), $ex->getCode(), $ex);
70
        }
71
    }
72
73 2
    public function size(array $entry): ?int
74
    {
75 2
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
76
        try {
77 2
            if (!$this->storage->exists($path)) {
78 1
                return null;
79
            }
80 1
            $content = $this->storage->read($path);
81 1
            if (is_resource($content)) {
82
                // a bit workaround
83 1
                $tempStream = fopen("php://temp", "w+b");
84 1
                if (false === $tempStream) {
85
                    // @codeCoverageIgnoreStart
86
                    throw new FilesException($this->lang->flCannotLoadFile($path));
87
                }
88
                // @codeCoverageIgnoreEnd
89 1
                rewind($content);
90 1
                $size = stream_copy_to_stream($content, $tempStream, -1, 0);
91 1
                if (false === $size) {
92
                    // @codeCoverageIgnoreStart
93
                    throw new FilesException($this->lang->flCannotGetSize($path));
94
                }
95
                // @codeCoverageIgnoreEnd
96 1
                return intval($size);
97
            } else {
98 1
                return strlen(strval($content));
99
            }
100 1
        } catch (StorageException $ex) {
101 1
            throw new FilesException($this->lang->flCannotProcessNode($path), $ex->getCode(), $ex);
102
        }
103
    }
104
105 1
    public function created(array $entry): ?int
106
    {
107 1
        return null;
108
    }
109
}
110