AsymmetricCipherFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 24
c 1
b 0
f 0
dl 0
loc 94
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createInstance() 0 14 3
A create() 0 3 1
A __debugInfo() 0 11 1
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
     * Get debug information for the class instance.
69
     *
70
     * @return array Debug information.
71
     */
72 1
    public function __debugInfo()
73
    {
74 1
        return [
75 1
            self::class . '::RSA_1024' => self::RSA_1024,
76 1
            self::class . '::RSA_2048' => self::RSA_2048,
77 1
            self::class . '::RSA_3072' => self::RSA_3072,
78 1
            self::class . '::RSA_4096' => self::RSA_4096,
79 1
            self::class . '::DSA_1024' => self::DSA_1024,
80 1
            self::class . '::DSA_2048' => self::DSA_2048,
81 1
            self::class . '::DSA_3072' => self::DSA_3072,
82 1
            self::class . '::DSA_4096' => self::DSA_4096,
83 1
        ];
84
    }
85
86
    /**
87
     * Create an encryption or digital signature algorithm object
88
     *
89
     * @param string|null $type The algorithm class name as type for creation.
90
     *
91
     * @return EncryptionAlgorithm|object|null An encryption/signature algorithm object or null.
92
     */
93 3
    public function create($type)
94
    {
95 3
        return self::createInstance($type);
96
    }
97
98
    /**
99
     * Create an encryption or digital signature algorithm object
100
     *
101
     * @param string|null $type The algorithm class name as type for creation.
102
     *
103
     * @return EncryptionAlgorithm|object|null An encryption/signature algorithm object or null.
104
     */
105 4
    public static function createInstance($type)
106
    {
107
        /**
108
         * Check if class exists and has a correct base class
109
         *
110
         * @var EncryptionAlgorithm|null $exception Object instance.
111
         */
112 4
        if (class_exists($type) && is_subclass_of($type, EncryptionAlgorithm::class)) {
113 4
            $exception = new $type();
114
        } else {
115 2
            $exception = null; // Invalid type given
116
        }
117
118 4
        return $exception;
119
    }
120
}
121