Passed
Push — master ( 53c725...a8eb1b )
by Petr
07:45
created

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