Completed
Push — master ( 8f4253...b7b8cb )
by Tony Karavasilev (Тони
18:47
created

AsymmetricCipherFactory::createInstance()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 10
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
/**
4
 * Factory for object asymmetric encryption/signature algorithm object instancing.
5
 */
6
7
namespace CryptoManana\Factories;
8
9
use \CryptoManana\Core\Abstractions\DesignPatterns\AbstractFactory as FactoryPattern;
10
use \CryptoManana\Core\Abstractions\MessageEncryption\AbstractAsymmetricEncryptionAlgorithm as EncryptionAlgorithm;
11
use \CryptoManana\AsymmetricEncryption\Rsa1024 as Rsa1024;
12
use \CryptoManana\AsymmetricEncryption\Rsa2048 as Rsa2048;
13
use \CryptoManana\AsymmetricEncryption\Rsa3072 as Rsa3072;
14
use \CryptoManana\AsymmetricEncryption\Rsa4096 as Rsa4096;
15
use \CryptoManana\AsymmetricEncryption\Dsa1024 as Dsa1024;
16
use \CryptoManana\AsymmetricEncryption\Dsa2048 as Dsa2048;
17
use \CryptoManana\AsymmetricEncryption\Dsa3072 as Dsa3072;
18
use \CryptoManana\AsymmetricEncryption\Dsa4096 as Dsa4096;
19
20
/**
21
 * Class AsymmetricCipherFactory - Factory for object asymmetric encryption/signature algorithm object instancing.
22
 *
23
 * @package CryptoManana\Factories
24
 */
25
class AsymmetricCipherFactory extends FactoryPattern
26
{
27
    /**
28
     * The RSA-1024 type.
29
     */
30
    const RSA_1024 = Rsa1024::class;
31
32
    /**
33
     * The RSA-2048 type.
34
     */
35
    const RSA_2048 = Rsa2048::class;
36
37
    /**
38
     * The RSA-3072 type.
39
     */
40
    const RSA_3072 = Rsa3072::class;
41
42
    /**
43
     * The RSA-4096 type.
44
     */
45
    const RSA_4096 = Rsa4096::class;
46
47
    /**
48
     * The DSA-1024 type.
49
     */
50
    const DSA_1024 = Dsa1024::class;
51
52
    /**
53
     * The DSA-2048 type.
54
     */
55
    const DSA_2048 = Dsa2048::class;
56
57
    /**
58
     * The DSA-3072 type.
59
     */
60
    const DSA_3072 = Dsa3072::class;
61
62
    /**
63
     * The DSA-4096 type.
64
     */
65
    const DSA_4096 = Dsa4096::class;
66
67
    /**
68
     * Create an encryption or digital signature algorithm object
69
     *
70
     * @param string|null $type The algorithm class name as type for creation.
71
     *
72
     * @return EncryptionAlgorithm|object|null An encryption/signature algorithm object or null.
73
     */
74
    public function create($type)
75
    {
76
        return self::createInstance($type);
77
    }
78
79
    /**
80
     * Create an encryption or digital signature algorithm object
81
     *
82
     * @param string|null $type The algorithm class name as type for creation.
83
     *
84
     * @return EncryptionAlgorithm|object|null An encryption/signature algorithm object or null.
85
     */
86
    public static function createInstance($type)
87
    {
88
        /**
89
         * Check if class exists and has a correct base class
90
         *
91
         * @var EncryptionAlgorithm|null $exception Object instance.
92
         */
93
        if (class_exists($type) && is_subclass_of($type, EncryptionAlgorithm::class)) {
94
            $exception = new $type();
95
        } else {
96
            $exception = null; // Invalid type given
97
        }
98
99
        return $exception;
100
    }
101
102
    /**
103
     * Get debug information for the class instance.
104
     *
105
     * @return array Debug information.
106
     */
107
    public function __debugInfo()
108
    {
109
        return [
110
            self::class . '::RSA_1024' => self::RSA_1024,
111
            self::class . '::RSA_2048' => self::RSA_2048,
112
            self::class . '::RSA_3072' => self::RSA_3072,
113
            self::class . '::RSA_4096' => self::RSA_4096,
114
            self::class . '::DSA_1024' => self::DSA_1024,
115
            self::class . '::DSA_2048' => self::DSA_2048,
116
            self::class . '::DSA_3072' => self::DSA_3072,
117
            self::class . '::DSA_4096' => self::DSA_4096,
118
        ];
119
    }
120
}
121