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

Basic   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
dl 0
loc 95
ccs 40
cts 44
cp 0.9091
rs 10
c 1
b 0
f 0
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isDir() 0 7 3
A isFile() 0 7 3
A isReadable() 0 3 1
A exists() 0 7 2
A noDirectoryDelimiterSet() 0 3 1
A size() 0 29 6
A isWritable() 0 3 1
A __construct() 0 4 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_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 12
    public function __construct(IStorage $storage, ?IFLTranslations $lang = null)
23
    {
24 12
        $this->storage = $storage;
25 12
        $this->setLang($lang);
26 12
    }
27
28 7
    public function exists(array $entry): bool
29
    {
30 7
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
31
        try {
32 7
            return $this->storage->exists($path);
33 1
        } catch (StorageException $ex) {
34 1
            throw new FilesException($this->getLang()->flCannotProcessNode($path), $ex->getCode(), $ex);
35
        }
36
    }
37
38 1
    public function isReadable(array $entry): bool
39
    {
40 1
        return true;
41
    }
42
43 1
    public function isWritable(array $entry): bool
44
    {
45 1
        return true;
46
    }
47
48 7
    public function isDir(array $entry): bool
49
    {
50 7
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
51
        try {
52 7
            return $this->storage->exists($path) && static::STORAGE_NODE_KEY === $this->storage->read($path);
53 1
        } catch (StorageException $ex) {
54 1
            throw new FilesException($this->getLang()->flCannotProcessNode($path), $ex->getCode(), $ex);
55
        }
56
    }
57
58 3
    public function isFile(array $entry): bool
59
    {
60 3
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
61
        try {
62 3
            return $this->storage->exists($path) && static::STORAGE_NODE_KEY !== $this->storage->read($path);
63 1
        } catch (StorageException $ex) {
64 1
            throw new FilesException($this->getLang()->flCannotProcessNode($path), $ex->getCode(), $ex);
65
        }
66
    }
67
68 2
    public function size(array $entry): ?int
69
    {
70 2
        $path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator());
71
        try {
72 2
            if (!$this->storage->exists($path)) {
73 1
                return null;
74
            }
75 1
            $content = $this->storage->read($path);
76 1
            if (is_resource($content)) {
77
                // a bit workaround
78 1
                $tempStream = fopen("php://temp", "w+b");
79 1
                if (false === $tempStream) {
80
                    // @codeCoverageIgnoreStart
81
                    throw new FilesException($this->getLang()->flCannotLoadFile($path));
82
                }
83
                // @codeCoverageIgnoreEnd
84 1
                rewind($content);
85 1
                $size = stream_copy_to_stream($content, $tempStream, -1, 0);
86 1
                if (false === $size) {
87
                    // @codeCoverageIgnoreStart
88
                    throw new FilesException($this->getLang()->flCannotGetSize($path));
89
                }
90
                // @codeCoverageIgnoreEnd
91 1
                return intval($size);
92
            } else {
93 1
                return strlen(strval($content));
94
            }
95 1
        } catch (StorageException $ex) {
96 1
            throw new FilesException($this->getLang()->flCannotProcessNode($path), $ex->getCode(), $ex);
97
        }
98
    }
99
100 1
    public function created(array $entry): ?int
101
    {
102 1
        return null;
103
    }
104
105
    /**
106
     * @return string
107
     * @codeCoverageIgnore only when path fails
108
     */
109
    protected function noDirectoryDelimiterSet(): string
110
    {
111
        return $this->getLang()->flNoDirectoryDelimiterSet();
112
    }
113
}
114