Factory::getStorage()   B
last analyzed

Complexity

Conditions 10
Paths 11

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10.1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 21
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 32
ccs 18
cts 20
cp 0.9
crap 10.1
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace kalanis\UploadPerPartes\Target\Local\DrivingFile\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\IDrivingFile;
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\DrivingFile\Storage
19
 * Key data storage - Factory to get where to store info data
20
 */
21
class Factory
22
{
23
    use TLangInit;
24
25
    public const FORMAT_VOLUME = 1;
26
    public const FORMAT_STORAGE = 2;
27
    public const FORMAT_FILES = 3;
28
    public const FORMAT_REDIS = 4;
29
    public const FORMAT_PREDIS = 5;
30
    public const FORMAT_CLIENT = 6;
31
32
    /**
33
     * @param Config $config
34
     * @throws UploadException
35
     * @return IDrivingFile
36
     */
37 27
    public function getStorage(Config $config): IDrivingFile
38
    {
39 27
        if ($config->drivingFileStorage instanceof IDrivingFile) {
40 1
            return $config->drivingFileStorage;
41
        }
42 26
        if ($config->drivingFileStorage instanceof IStorage) {
43 19
            return new Storage($config->drivingFileStorage, $config->tempDir, $this->getUppLang());
44
        }
45 7
        if ($config->drivingFileStorage instanceof CompositeAdapter) {
46
            try {
47 1
                $ap = new ArrayPath();
48 1
                return new Files($config->drivingFileStorage, $ap->setString($config->tempDir)->getArray(), $ap, $this->getUppLang());
49
                // @codeCoverageIgnoreStart
50
            } catch (PathsException $ex) {
51
                throw new UploadException($ex->getMessage(), $ex->getCode(), $ex);
52
            }
53
            // @codeCoverageIgnoreEnd
54
        }
55
56 6
        switch ($config->drivingFileStorage) {
57 6
            case 'volume':
58 5
            case self::FORMAT_VOLUME:
59 2
                return new Volume($config->tempDir, $this->getUppLang());
60 4
            case 'client':
61 2
            case self::FORMAT_CLIENT:
62 2
                return new Client($this->getUppLang());
63
            default:
64 2
                if (is_string($config->drivingFileStorage)) {
65 1
                    return new Volume($config->drivingFileStorage, $this->getUppLang());
66
                }
67
68 1
                throw new UploadException($this->getUppLang()->uppDriveFileStorageNotSet());
69
        }
70
    }
71
}
72