Passed
Push — master ( f1fb72...be1df6 )
by Petr
08:10
created

Factory::whichParser()   C

Complexity

Conditions 12
Paths 11

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 16
nc 11
nop 1
dl 0
loc 26
ccs 17
cts 17
cp 1
crap 12
rs 6.9666
c 0
b 0
f 0

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_auth_sources\Access;
4
5
6
use kalanis\kw_auth_sources\AuthSourcesException;
7
use kalanis\kw_auth_sources\ExtraParsers;
8
use kalanis\kw_auth_sources\Hashes;
9
use kalanis\kw_auth_sources\Interfaces;
10
use kalanis\kw_auth_sources\Statuses;
11
use kalanis\kw_auth_sources\Sources;
12
use kalanis\kw_auth_sources\Traits\TLang;
13
use kalanis\kw_files\Access\CompositeAdapter;
14
use kalanis\kw_files\Interfaces\IFLTranslations;
15
use kalanis\kw_locks\Interfaces\IKLTranslations;
16
use kalanis\kw_locks\Interfaces\ILock;
17
use kalanis\kw_locks\LockException;
18
use kalanis\kw_locks\Methods as lock_methods;
19
use kalanis\kw_storage\Interfaces\IStorage;
20
21
22
/**
23
 * Class Factory
24
 * @package kalanis\kw_auth_sources\Access
25
 */
26
class Factory
27
{
28
    use TLang;
29
30 38
    public function __construct(?Interfaces\IKAusTranslations $lang = null)
31
    {
32 38
        $this->setAusLang($lang);
33 38
    }
34
35
    /**
36
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>>|string|object $params
37
     * @throws AuthSourcesException
38
     * @throws LockException
39
     * @return CompositeSources
40
     */
41 38
    public function getSources($params): CompositeSources
42
    {
43 38
        if (is_object($params)) {
44 4
            if ($params instanceof CompositeSources) {
45 1
                return $params;
46
            }
47 3
            if ($params instanceof IStorage) {
48 1
                $storage = new Sources\Files\Storages\Storage($params, $this->getAusLang());
49 1
                $lock = new lock_methods\StorageLock($params);
50 1
                $accounts = new Sources\Files\AccountsSingleFile(
51 1
                    $storage,
52 1
                    new Hashes\CoreLib(),
53 1
                    new Statuses\Always(),
54 1
                    new ExtraParsers\Serialize(),
55
                    $lock,
56 1
                    [],
57 1
                    $this->getAusLang()
58
                );
59 1
                return new CompositeSources(
60 1
                    $accounts,
61
                    $accounts,
62 1
                    new Sources\Files\Groups(
63 1
                        $storage,
64
                        $accounts,
65 1
                        new ExtraParsers\Serialize(),
66
                        $lock,
67 1
                        [],
68 1
                        $this->getAusLang()
69
                    ),
70 1
                    new Sources\Classes()
71
                );
72
            }
73 2
            if ($params instanceof CompositeAdapter) {
74 1
                $storage = new Sources\Files\Storages\Files($params, $this->getAusLang());
75 1
                $lock = new lock_methods\FilesLock($params);
76 1
                $accounts = new Sources\Files\AccountsSingleFile(
77 1
                    $storage,
78 1
                    new Hashes\CoreLib(),
79 1
                    new Statuses\Always(),
80 1
                    new ExtraParsers\Serialize(),
81
                    $lock,
82 1
                    [],
83 1
                    $this->getAusLang()
84
                );
85 1
                return new CompositeSources(
86 1
                    $accounts,
87
                    $accounts,
88 1
                    new Sources\Files\Groups(
89 1
                        $storage,
90
                        $accounts,
91 1
                        new ExtraParsers\Serialize(),
92
                        $lock,
93 1
                        [],
94 1
                        $this->getAusLang()
95
                    ),
96 2
                    new Sources\Classes()
97
                );
98
            }
99 34
        } elseif (is_string($params)) {
100 4
            if ('ldap' == $params) {
101 1
                return new CompositeSources(
102 1
                    new Sources\Mapper\AuthLdap(),
103 1
                    new Sources\Dummy\Accounts(),
104 1
                    new Sources\Dummy\Groups(),
105 1
                    new Sources\Classes()
106
                );
107
            }
108 3
            if ('db' == $params) {
109 1
                $auth = new Sources\Mapper\AccountsDatabase(new Hashes\CoreLib());
110 1
                return new CompositeSources(
111 1
                    $auth,
112
                    $auth,
113 1
                    new Sources\Mapper\GroupsDatabase(),
114 1
                    new Sources\Classes()
115
                );
116
            }
117 2
            if (($dir = realpath($params)) && is_dir($params)) {
118 1
                $storage = new Sources\Files\Storages\Volume($dir . DIRECTORY_SEPARATOR, $this->getAusLang());
119 1
                $lock = new lock_methods\FileLock($dir . DIRECTORY_SEPARATOR . 'sources.lock');
120 1
                $accounts = new Sources\Files\AccountsSingleFile(
121 1
                    $storage,
122 1
                    new Hashes\CoreLib(),
123 1
                    new Statuses\Always(),
124 1
                    new ExtraParsers\Serialize(),
125
                    $lock,
126 1
                    [],
127 1
                    $this->getAusLang()
128
                );
129 1
                return new CompositeSources(
130 1
                    $accounts,
131
                    $accounts,
132 1
                    new Sources\Files\Groups(
133 1
                        $storage,
134
                        $accounts,
135 1
                        new ExtraParsers\Serialize(),
136
                        $lock,
137 1
                        [],
138 1
                        $this->getAusLang()
139
                    ),
140 2
                    new Sources\Classes()
141
                );
142
            }
143 30
        } elseif (is_array($params)) {
0 ignored issues
show
introduced by
The condition is_array($params) is always true.
Loading history...
144
            // now it became a bit complicated...
145 27
            if (isset($params['path']) && is_string($params['path'])) {
146 6
                $params['storage'] = $params['path'];
147 6
                $params['status'] = true;
148 6
                unset($params['path']);
149
            }
150 27
            if (isset($params['source']) && is_string($params['source'])) {
151 1
                $params['storage'] = $params['source'];
152 1
                $params['status'] = true;
153 1
                unset($params['source']);
154
            }
155 27
            $storage = $this->whichStorage($params);
156 19
            $hash = $this->whichHash($params);
157 19
            $status = $this->whichStatus($params);
158 18
            $lock = $this->whichLocks($params);
159 17
            $extraParser = $this->whichParser($params);
160 17
            $accounts = $this->getAccounts($params, $storage, $hash, $status, $extraParser, $lock);
161 17
            $auth = ($accounts instanceof Interfaces\IAuth) ? $accounts : $this->getAuth($params, $storage, $hash, $status, $extraParser, $lock);
162 17
            return new CompositeSources(
163 17
                $auth,
164
                $accounts,
165 17
                $this->getGroups($params, $storage, $accounts, $extraParser, $lock),
166 17
                $this->getClasses($params)
167
            );
168
        }
169 5
        throw new AuthSourcesException($this->getAusLang()->kauCombinationUnavailable());
170
    }
171
172
    /**
173
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>> $params
174
     * @throws AuthSourcesException
175
     * @return Sources\Files\Storages\AStorage
176
     */
177 27
    protected function whichStorage(array $params): Sources\Files\Storages\AStorage
178
    {
179 27
        if (isset($params['storage'])) {
180 19
            if (is_object($params['storage']) && ($params['storage'] instanceof Sources\Files\Storages\AStorage)) {
181 1
                return $params['storage'];
182
            }
183 18
            if (is_object($params['storage']) && ($params['storage'] instanceof IStorage)) {
184 5
                return new Sources\Files\Storages\Storage($params['storage'], $this->getAusLang());
185
            }
186 13
            if (is_object($params['storage']) && ($params['storage'] instanceof CompositeAdapter)) {
187 3
                $storageLang = null;
188 3
                if (isset($params['storage_lang']) && ($params['storage_lang'] instanceof IFLTranslations)) {
189 1
                    $storageLang = $params['storage_lang'];
190
                }
191 3
                if (!$storageLang && (($localLang = $this->getAusLang()) instanceof IFLTranslations)) {
192 1
                    $storageLang = $localLang;
193
                }
194 3
                return new Sources\Files\Storages\Files($params['storage'], $this->getAusLang(), $storageLang);
195
            }
196 10
            if (is_string($params['storage']) && ($pt = realpath($params['storage']))) {
197 10
                return new Sources\Files\Storages\Volume($pt . DIRECTORY_SEPARATOR, $this->getAusLang());
198
            }
199
        }
200 8
        throw new AuthSourcesException($this->getAusLang()->kauCombinationUnavailable());
201
    }
202
203
    /**
204
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>> $params
205
     * @return Interfaces\IHashes
206
     */
207 19
    protected function whichHash(array $params): Interfaces\IHashes
208
    {
209 19
        if (isset($params['hash'])) {
210 2
            if (is_object($params['hash']) && $params['hash'] instanceof Interfaces\IHashes) {
211 1
                return $params['hash'];
212
            }
213 1
            if (is_string($params['hash'])) {
214 1
                return new Hashes\KwOrig($params['hash'], $this->getAusLang());
215
            }
216
        }
217 17
        return new Hashes\CoreLib();
218
    }
219
220
    /**
221
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>> $params
222
     * @throws AuthSourcesException
223
     * @return Interfaces\IStatus
224
     */
225 19
    protected function whichStatus(array $params): Interfaces\IStatus
226
    {
227 19
        if (isset($params['status'])) {
228 18
            if (is_object($params['status']) && $params['status'] instanceof Interfaces\IStatus) {
229 3
                return $params['status'];
230
            }
231 15
            if (is_string($params['status'])) {
232 1
                return ('always' == $params['status']) ? new Statuses\Always() : new Statuses\Checked();
233
            }
234 14
            if (is_numeric($params['status'])) {
235 3
                return boolval(intval($params['status'])) ? new Statuses\Always() : new Statuses\Checked();
236
            }
237 11
            if (is_bool($params['status'])) {
238 11
                return $params['status'] ? new Statuses\Always() : new Statuses\Checked();
239
            }
240
        }
241 1
        throw new AuthSourcesException($this->getAusLang()->kauCombinationUnavailable());
242
    }
243
244
    /**
245
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>> $params
246
     * @return Interfaces\IExtraParser
247
     */
248 17
    protected function whichParser(array $params): Interfaces\IExtraParser
249
    {
250 17
        if (isset($params['parser'])) {
251 15
            if (is_object($params['parser']) && $params['parser'] instanceof Interfaces\IExtraParser) {
252 2
                return $params['parser'];
253
            }
254 13
            if (is_string($params['parser'])) {
255 4
                if ('php' == $params['parser']) {
256 1
                    return new ExtraParsers\Serialize();
257
                }
258 3
                if ('serial' == $params['parser']) {
259 1
                    return new ExtraParsers\Serialize();
260
                }
261 2
                if ('none' == $params['parser']) {
262 1
                    return new ExtraParsers\None();
263
                }
264 1
                return new ExtraParsers\Json();
265
            }
266 9
            if (is_numeric($params['parser'])) {
267 6
                return boolval(intval($params['parser'])) ? new ExtraParsers\Serialize() : new ExtraParsers\Json();
268
            }
269 3
            if (is_bool($params['parser'])) {
270 3
                return $params['parser'] ? new ExtraParsers\Serialize() : new ExtraParsers\Json();
271
            }
272
        }
273 2
        return new ExtraParsers\None();
274
//        throw new AuthSourcesException($this->getAusLang()->kauCombinationUnavailable());
275
    }
276
277
    /**
278
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>> $params
279
     * @throws AuthSourcesException
280
     * @return ILock
281
     */
282 18
    protected function whichLocks(array $params): ILock
283
    {
284
        try {
285 18
            $lockLang = null;
286 18
            if (isset($params['lock_lang']) && ($params['lock_lang'] instanceof IKLTranslations)) {
287 1
                $lockLang = $params['lock_lang'];
288
            }
289 18
            if (!$lockLang && (($localLang = $this->getAusLang()) instanceof IKLTranslations)) {
290 1
                $lockLang = $localLang;
291
            }
292 18
            if (isset($params['lock'])) {
293 4
                if (is_object($params['lock']) && ($params['lock'] instanceof ILock)) {
294 1
                    return $params['lock'];
295
                }
296 3
                if (is_object($params['lock']) && ($params['lock'] instanceof IStorage)) {
297 1
                    return new lock_methods\StorageLock($params['lock'], $lockLang);
298
                }
299 2
                if (is_object($params['lock']) && ($params['lock'] instanceof CompositeAdapter)) {
300 1
                    return new lock_methods\FilesLock($params['lock'], $lockLang);
301
                }
302 1
                if (is_string($params['lock']) && ($pt = realpath($params['lock']))) {
303 1
                    return new lock_methods\FileLock($pt . DIRECTORY_SEPARATOR . 'auth.lock', $lockLang);
304
                }
305 14
            } elseif (isset($params['storage'])) {
306 14
                if (is_object($params['storage']) && ($params['storage'] instanceof ILock)) {
307 1
                    return $params['storage'];
308
                }
309 13
                if (is_object($params['storage']) && ($params['storage'] instanceof IStorage)) {
310 3
                    return new lock_methods\StorageLock($params['storage'], $lockLang);
311
                }
312 10
                if (is_object($params['storage']) && ($params['storage'] instanceof CompositeAdapter)) {
313 2
                    return new lock_methods\FilesLock($params['storage'], $lockLang);
314
                }
315 8
                if (is_string($params['storage']) && ($pt = realpath($params['storage']))) {
316 9
                    return new lock_methods\FileLock($pt . DIRECTORY_SEPARATOR . 'auth.lock', $lockLang);
317
                }
318
            }
319
            // @codeCoverageIgnoreStart
320
        } catch (LockException $ex) {
321
            // dies FileLock
322
            throw new AuthSourcesException($ex->getMessage(), $ex->getCode(), $ex);
323
        }
324
        // @codeCoverageIgnoreEnd
325 1
        throw new AuthSourcesException($this->getAusLang()->kauCombinationUnavailable());
326
    }
327
328
    /**
329
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>> $params
330
     * @param Sources\Files\Storages\AStorage $storage
331
     * @param Interfaces\IHashes $hash
332
     * @param Interfaces\IStatus $status
333
     * @param Interfaces\IExtraParser $parser
334
     * @param ILock $lock
335
     * @return Interfaces\IAuth
336
     */
337 3
    protected function getAuth(
338
        array $params,
339
        Sources\Files\Storages\AStorage $storage,
340
        Interfaces\IHashes $hash,
341
        Interfaces\IStatus $status,
342
        Interfaces\IExtraParser $parser,
343
        ILock $lock
344
    ): Interfaces\IAuth
345
    {
346 3
        if (isset($params['auth']) && ($params['auth'] instanceof Interfaces\IAuth)) {
347 1
            return $params['auth'];
348
        }
349 2
        if (isset($params['single_file'])) {
350 1
            return new Sources\Files\AccountsSingleFile($storage, $hash, $status, $parser, $lock, $this->clearedPath($params), $this->getAusLang());
351
        }
352 1
        return new Sources\Files\AccountsMultiFile($storage, $hash, $status, $parser, $lock, $this->clearedPath($params), $this->getAusLang());
353
    }
354
355
    /**
356
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>> $params
357
     * @param Sources\Files\Storages\AStorage $storage
358
     * @param Interfaces\IHashes $hash
359
     * @param Interfaces\IStatus $status
360
     * @param Interfaces\IExtraParser $parser
361
     * @param ILock $lock
362
     * @return Interfaces\IWorkAccounts
363
     */
364 17
    protected function getAccounts(
365
        array $params,
366
        Sources\Files\Storages\AStorage $storage,
367
        Interfaces\IHashes $hash,
368
        Interfaces\IStatus $status,
369
        Interfaces\IExtraParser $parser,
370
        ILock $lock
371
    ): Interfaces\IWorkAccounts
372
    {
373 17
        if (isset($params['accounts']) && ($params['accounts'] instanceof Interfaces\IWorkAccounts)) {
374 3
            return $params['accounts'];
375
        }
376 14
        if (isset($params['single_file'])) {
377 1
            return new Sources\Files\AccountsSingleFile($storage, $hash, $status, $parser, $lock, $this->clearedPath($params), $this->getAusLang());
378
        }
379 13
        return new Sources\Files\AccountsMultiFile($storage, $hash, $status, $parser, $lock, $this->clearedPath($params), $this->getAusLang());
380
    }
381
382
    /**
383
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>> $params
384
     * @param Sources\Files\Storages\AStorage $storage
385
     * @param Interfaces\IWorkAccounts $accounts
386
     * @param Interfaces\IExtraParser $parser
387
     * @param ILock $lock
388
     * @return Interfaces\IWorkGroups
389
     */
390 17
    protected function getGroups(
391
        array $params,
392
        Sources\Files\Storages\AStorage $storage,
393
        Interfaces\IWorkAccounts $accounts,
394
        Interfaces\IExtraParser $parser,
395
        ILock $lock
396
    ): Interfaces\IWorkGroups
397
    {
398 17
        if (isset($params['groups']) && ($params['groups'] instanceof Interfaces\IWorkGroups)) {
399 1
            return $params['groups'];
400
        }
401 16
        return new Sources\Files\Groups($storage, $accounts, $parser, $lock, $this->clearedPath($params), $this->getAusLang());
402
    }
403
404
    /**
405
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>> $params
406
     * @return Interfaces\IWorkClasses
407
     */
408 17
    protected function getClasses(array $params): Interfaces\IWorkClasses
409
    {
410 17
        if (isset($params['classes']) && ($params['classes'] instanceof Interfaces\IWorkClasses)) {
411 1
            return $params['classes'];
412
        }
413 16
        return new Sources\Classes();
414
    }
415
416
    /**
417
     * @param array<string|int, string|int|float|object|bool|array<string|int|float|object>> $params
418
     * @return string[]
419
     */
420 17
    protected function clearedPath(array $params): array
421
    {
422 17
        $path = [];
423 17
        if (isset($params['path']) && is_array($params['path'])) {
424 1
            $path = array_map('strval', $params['path']);
425
        }
426 17
        if (isset($params['source']) && is_array($params['source'])) {
427 2
            $path = array_map('strval', $params['source']);
428
        }
429 17
        return array_values($path);
430
    }
431
}
432