Passed
Push — master ( a57c10...658945 )
by Petr
07:51
created

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