Factory::getLoader()   C
last analyzed

Complexity

Conditions 17
Paths 55

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 17
eloc 33
c 1
b 0
f 0
nc 55
nop 1
dl 0
loc 48
ccs 32
cts 32
cp 1
crap 17
rs 5.2166

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\kw_modules\Access;
4
5
6
use kalanis\kw_files\Access\Factory as files_factory;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_files\Interfaces\IFLTranslations;
9
use kalanis\kw_files\Interfaces\IProcessFiles;
10
use kalanis\kw_modules\Interfaces\ILoader;
11
use kalanis\kw_modules\Interfaces\IMdTranslations;
12
use kalanis\kw_modules\Interfaces\Lists\File\IParamFormat;
13
use kalanis\kw_modules\Interfaces\Lists\IModulesList;
14
use kalanis\kw_modules\ModuleException;
15
use kalanis\kw_modules\ModulesLists\File;
16
use kalanis\kw_modules\ModulesLists\ParamsFormat;
17
use kalanis\kw_modules\Loaders;
18
use kalanis\kw_modules\Mixer\Processor;
19
use kalanis\kw_modules\Parser\GetModules;
20
use kalanis\kw_modules\Traits\TMdLang;
21
use kalanis\kw_paths\PathsException;
22
use kalanis\kw_storage\Access\Factory as storage_factory;
23
use kalanis\kw_storage\Interfaces\IStorage;
24
use kalanis\kw_storage\Interfaces\IStTranslations;
25
use kalanis\kw_storage\StorageException;
26
27
28
/**
29
 * Class Factory
30
 * @package kalanis\kw_modules\Access
31
 * Factory to get instances to run
32
 */
33
class Factory
34
{
35
    use TMdLang;
36
37
    protected ?GetModules $parser;
38
39 24
    public function __construct(?GetModules $parser = null, ?IMdTranslations $lang = null)
40
    {
41 24
        $this->parser = $parser;
42 24
        $this->setMdLang($lang);
43 24
    }
44
45
    /**
46
     * @param mixed $params
47
     * @throws ModuleException
48
     * @return Processor
49
     */
50 24
    public function getProcessor($params): Processor
51
    {
52 24
        return new Processor($this->getLoader($params), $this->getModulesList($params), $this->getMdLang(), $this->parser);
53
    }
54
55
    /**
56
     * @param mixed $params
57
     * @throws ModuleException
58
     * @return ILoader
59
     */
60 24
    public function getLoader($params): ILoader
61
    {
62 24
        if (is_object($params)) {
63 18
            if ($params instanceof ILoader) {
64 17
                return $params;
65
            }
66
        }
67 24
        if (is_array($params)) {
68 18
            if (isset($params['modules_loaders'])) {
69 18
                return $this->getLoader($params['modules_loaders']);
70
            }
71 1
            $what = [];
72 1
            foreach (array_values($params) as $item) {
73
                try {
74 1
                    $what[] = $this->getLoader($item);
75 1
                } catch (ModuleException $ex) {
76
                    // not found - pass
77
                }
78
            }
79 1
            return new Loaders\ClassLoader($what);
80
        }
81 7
        if (is_string($params)) {
82
            try {
83
                /** @var class-string $params */
84 2
                $ref = new \ReflectionClass($params);
85 1
                $class = $ref->newInstance();
86 1
                if ($class && $class instanceof ILoader) {
87 1
                    return $class;
88
                }
89 2
            } catch (\ReflectionException $ex) {
90
                // nothing
91
            }
92 2
            switch ($params) {
93 2
                case 'admin':
94 1
                    return new Loaders\Kw\AdminLoader($this->getMdLang());
95 2
                case 'api':
96 1
                    return new Loaders\Kw\ApiLoader($this->getMdLang());
97 2
                case 'web':
98 1
                    return new Loaders\Kw\Loader($this->getMdLang());
99 2
                case 'di-admin':
100 1
                    return new Loaders\KwDi\AdminLoader($this->getMdLang());
101 2
                case 'di-api':
102 1
                    return new Loaders\KwDi\ApiLoader($this->getMdLang());
103 2
                case 'di-web':
104 1
                    return new Loaders\KwDi\Loader($this->getMdLang());
105
            }
106
        }
107 7
        throw new ModuleException($this->getMdLang()->mdNoLoaderSet());
108
    }
109
110
    /**
111
     * @param mixed $params
112
     * @throws ModuleException
113
     * @return IModulesList
114
     */
115 18
    public function getModulesList($params): IModulesList
116
    {
117 18
        if (is_object($params)) {
118 4
            if ($params instanceof IModulesList) {
119 2
                return $params;
120
            }
121 2
            if ($params instanceof IStorage) {
122 1
                return new File(new File\Storage($params, '', $this->getMdLang()), new ParamsFormat\Http());
123
            }
124 1
            if ($params instanceof IProcessFiles) {
125 1
                return new File(new File\Files($params, [], $this->getMdLang()), new ParamsFormat\Http());
126
            }
127
        }
128 18
        if (is_array($params)) {
129 18
            if (isset($params['modules_source'])) {
130 7
                return $this->getModulesList($params['modules_source']);
131
            }
132 11
            $mdLang = isset($params['modules_lang']) && is_object($params['modules_lang']) && ($params['modules_lang'] instanceof IMdTranslations) ? $params['modules_lang'] : $this->getMdLang();
133 11
            $format = isset($params['modules_param_format']) ? $this->getFormat($params['modules_param_format']) : new ParamsFormat\Http();
134
            try {
135 11
                if (isset($params['storage_path'])) {
136 4
                    $lang = isset($params['lang']) && is_object($params['lang']) && ($params['lang'] instanceof IStTranslations) ? $params['lang'] : null;
137 4
                    return new File(new File\Storage((new storage_factory($lang))->getStorage($params), $params['storage_path'], $mdLang), $format);
138
                }
139 7
                if (isset($params['files_path'])) {
140 1
                    $lang = isset($params['lang']) && is_object($params['lang']) && ($params['lang'] instanceof IFLTranslations) ? $params['lang'] : null;
141 1
                    return new File(new File\Files((new files_factory($lang))->getClass($params), $params['files_path'], $mdLang), $format);
142
                }
143 6
                if (isset($params['volume_path'])) {
144 6
                    return new File(new File\Volume($params['volume_path'], $mdLang), $format);
145
                }
146 2
            } catch (FilesException | PathsException | StorageException $ex) {
147 2
                throw new ModuleException($ex->getMessage(), $ex->getCode(), $ex);
148
            }
149
        }
150 3
        if (is_string($params)) {
151
            try {
152
                /** @var class-string $params */
153 3
                $ref = new \ReflectionClass($params);
154 2
                $class = $ref->newInstance();
155 2
                if ($class && $class instanceof IModulesList) {
156 2
                    return $class;
157
                }
158 1
            } catch (\ReflectionException $ex) {
159
                // nothing
160
            }
161
        }
162
163 2
        throw new ModuleException($this->getMdLang()->mdNoSourceSet());
164
    }
165
166 11
    protected function getFormat(string $format): IParamFormat
167
    {
168 11
        switch ($format) {
169 11
            case 'serialize':
170 9
            case 'serial':
171 8
            case 's':
172 4
                return new ParamsFormat\Serialize();
173 7
            case 'json':
174 5
            case 'js':
175 4
            case 'j':
176 4
                return new ParamsFormat\Json();
177 3
            case 'http':
178 2
            case 'web':
179 1
            case 'w':
180
            default:
181 3
                return new ParamsFormat\Http();
182
        }
183
    }
184
}
185