Passed
Push — master ( 79545d...728467 )
by Petr
02:28
created

Basic::isWritable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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