Factory::__construct()   A
last analyzed

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