Factory::getStorage()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0957

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 28
ccs 14
cts 16
cp 0.875
crap 7.0957
rs 8.8333
1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\TemporaryStorage\Storage;
4
5
6
use kalanis\kw_files\Access\CompositeAdapter;
7
use kalanis\kw_paths\ArrayPath;
8
use kalanis\kw_paths\PathsException;
9
use kalanis\kw_storage\Interfaces\IStorage;
10
use kalanis\UploadPerPartes\Interfaces\ITemporaryStorage;
11
use kalanis\UploadPerPartes\Traits\TLangInit;
12
use kalanis\UploadPerPartes\Uploader\Config;
13
use kalanis\UploadPerPartes\UploadException;
14
15
16
/**
17
 * Class Factory
18
 * @package kalanis\UploadPerPartes\Target\Local\TemporaryStorage\Storage
19
 * Content data storage - Factory to get where to store main data
20
 */
21
class Factory
22
{
23
    use TLangInit;
24
25
    /**
26
     * @param Config $config
27
     * @throws UploadException
28
     * @return ITemporaryStorage
29
     */
30 26
    public function getStorage(Config $config): ITemporaryStorage
31
    {
32 26
        if ($config->temporaryStorage instanceof ITemporaryStorage) {
33 5
            return $config->temporaryStorage;
34
        }
35 21
        if ($config->temporaryStorage instanceof IStorage) {
36 16
            return new Storage($config->temporaryStorage, $config->tempDir, $this->getUppLang());
37
        }
38 5
        if ($config->temporaryStorage instanceof CompositeAdapter) {
39
            try {
40 1
                $ap = new ArrayPath();
41 1
                return new Files($config->temporaryStorage, $ap->setString($config->tempDir)->getArray(), $ap, $this->getUppLang());
42
                // @codeCoverageIgnoreStart
43
            } catch (PathsException $ex) {
44
                throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
45
            }
46
            // @codeCoverageIgnoreEnd
47
        }
48
49 4
        switch ($config->temporaryStorage) {
50 4
            case 'volume':
51 1
                return new Volume($config->tempDir, $this->getUppLang());
52
            default:
53 3
                if (is_string($config->temporaryStorage)) {
54 2
                    return new Volume($config->temporaryStorage, $this->getUppLang());
55
                }
56
57 1
                throw new UploadException($this->getUppLang()->uppTempStorageNotSet());
58
        }
59
    }
60
}
61