Passed
Pull Request — master (#190)
by Arman
04:05
created

CryptorFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createInstance() 0 9 2
A get() 0 7 2
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.5
13
 */
14
15
namespace Quantum\Libraries\Encryption\Factories;
16
17
use Quantum\Libraries\Encryption\Adapters\AsymmetricEncryptionAdapter;
18
use Quantum\Libraries\Encryption\Adapters\SymmetricEncryptionAdapter;
19
use Quantum\Libraries\Encryption\Exceptions\CryptorException;
20
use Quantum\Libraries\Encryption\Cryptor;
21
use Quantum\Exceptions\BaseException;
22
23
/**
24
 * Class Cryptor
25
 * @package Quantum\Libraries\Encryption
26
 */
27
class CryptorFactory
28
{
29
30
    /**
31
     * Supported adapters
32
     */
33
    const ADAPTERS = [
34
        Cryptor::SYMMETRIC => SymmetricEncryptionAdapter::class,
35
        Cryptor::ASYMMETRIC => AsymmetricEncryptionAdapter::class,
36
    ];
37
38
    /**
39
     * @var array
40
     */
41
    private static $instances = [];
42
43
    /**
44
     * @param string $type
45
     * @return Cryptor
46
     * @throws BaseException
47
     */
48
    public static function get(string $type = Cryptor::SYMMETRIC): Cryptor
49
    {
50
        if (!isset(self::$instances[$type])) {
51
            self::$instances[$type] = self::createInstance($type);
52
        }
53
54
        return self::$instances[$type];
55
    }
56
57
    /**
58
     * @param string $type
59
     * @return Cryptor
60
     * @throws BaseException
61
     */
62
    private static function createInstance(string $type): Cryptor
63
    {
64
        if (!isset(self::ADAPTERS[$type])) {
65
            throw CryptorException::adapterNotSupported($type);
66
        }
67
68
        $adapterClass = self::ADAPTERS[$type];
69
70
        return new Cryptor(new $adapterClass);
71
    }
72
}